0
0
DSA Cprogramming~5 mins

DFS Depth First Search on Graph in DSA C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is Depth First Search (DFS) in graph traversal?
DFS is a method to explore a graph by starting at a node and exploring as far as possible along each branch before backtracking.
Click to reveal answer
beginner
In DFS, what data structure is commonly used to keep track of nodes to visit next?
A stack is used to keep track of nodes to visit next, either explicitly or via recursion call stack.
Click to reveal answer
beginner
Why do we mark nodes as visited in DFS?
Marking nodes as visited prevents revisiting the same node, avoiding infinite loops and repeated work.
Click to reveal answer
intermediate
What is the order of node visits in DFS on a graph with nodes 1 connected to 2 and 3, and 2 connected to 4?
Starting at 1, DFS visits 1 → 2 → 4 → 3 (assuming neighbors are visited in numerical order).
Click to reveal answer
intermediate
What is the time complexity of DFS on a graph with V vertices and E edges?
The time complexity is O(V + E) because each vertex and edge is visited once.
Click to reveal answer
Which data structure is naturally used by recursive DFS?
AHeap
BQueue
CStack
DHash Table
What happens if you do not mark nodes as visited in DFS on a graph with cycles?
ADFS will terminate normally
BDFS will skip some nodes
CDFS will visit nodes in breadth order
DDFS may enter an infinite loop
In DFS, when do you backtrack?
AWhen all nodes are visited
BWhen no unvisited neighbors remain
CBefore visiting any neighbor
DAfter visiting the first neighbor
What is the first node visited in DFS starting from node 1 in a graph?
ANode 1
BNode 2
CNode 0
DNode with highest degree
Which of these is NOT a use case of DFS?
AFinding shortest path in unweighted graph
BFinding connected components
CTopological sorting
DDetecting cycles
Explain how DFS explores a graph starting from a node. Include how it uses the stack and visited nodes.
Think about going deep first before going wide.
You got /5 concepts.
    Describe the time complexity of DFS and why it is O(V + E) for a graph with V vertices and E edges.
    Consider how many times nodes and edges are processed.
    You got /4 concepts.