0
0
DSA Cprogramming~10 mins

DFS Depth First Search on Graph in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - DFS Depth First Search on Graph
Start at source node
Mark node as visited
For each neighbor
If neighbor not visited?
NoSkip
Yes
Recursively DFS on neighbor
All neighbors visited?
Backtrack to previous node
Done when all reachable nodes visited
Start from a node, mark it visited, then explore each unvisited neighbor deeply before backtracking.
Execution Sample
DSA C
void dfs(int node) {
  visited[node] = 1;
  for (int i = 0; i < graph[node].size(); i++) {
    int neighbor = graph[node][i];
    if (!visited[neighbor]) dfs(neighbor);
  }
}
This code visits nodes deeply in a graph using recursion, marking nodes visited to avoid repeats.
Execution Table
StepOperationCurrent NodeVisited SetCall StackActionGraph State
1Start DFSA{}[A]Mark A visitedA: [B, C], B: [D], C: [E], D: [], E: []
2Explore neighborA{A}[A, B]B not visited, recurseSame
3Mark visitedB{A}[A, B]Mark B visitedSame
4Explore neighborB{A, B}[A, B, D]D not visited, recurseSame
5Mark visitedD{A, B}[A, B, D]Mark D visitedSame
6Explore neighborsD{A, B, D}[A, B, D]No neighbors, backtrackSame
7BacktrackB{A, B, D}[A, B]Return to BSame
8All neighbors doneB{A, B, D}[A]Backtrack to ASame
9Explore neighborA{A, B, D}[A, C]C not visited, recurseSame
10Mark visitedC{A, B, D}[A, C]Mark C visitedSame
11Explore neighborC{A, B, D, C}[A, C, E]E not visited, recurseSame
12Mark visitedE{A, B, D, C}[A, C, E]Mark E visitedSame
13Explore neighborsE{A, B, D, C, E}[A, C, E]No neighbors, backtrackSame
14BacktrackC{A, B, D, C, E}[A, C]Return to CSame
15All neighbors doneC{A, B, D, C, E}[A]Backtrack to ASame
16All neighbors doneA{A, B, D, C, E}[]DFS completeSame
💡 All reachable nodes visited, call stack empty, DFS ends
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 9After Step 12Final
visited{}{A}{A, B}{A, B, D}{A, B, D}{A, B, D, C, E}{A, B, D, C, E}
call_stack[][A][A, B][A, B, D][A, C][A, C, E][]
Key Moments - 3 Insights
Why do we mark a node visited before exploring its neighbors?
Marking visited early (see Step 1 and Step 3) prevents revisiting the same node and infinite loops, as shown in the visited set updates in the execution_table.
Why does DFS backtrack when a node has no unvisited neighbors?
When no neighbors are left (Step 6 and Step 13), DFS returns to the previous node to explore other paths, shown by the call stack shrinking in the execution_table.
How does the call stack represent the current path in DFS?
The call stack (variable_tracker) shows nodes currently being explored; pushing when recursing deeper and popping when backtracking, matching the execution_table's Call Stack column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, which nodes are marked visited?
A{A}
B{A, B, D}
C{A, B}
D{}
💡 Hint
Check the Visited Set column at Step 5 in the execution_table.
At which step does DFS start exploring node C?
AStep 9
BStep 7
CStep 12
DStep 3
💡 Hint
Look at the Current Node and Operation columns in the execution_table.
If node E had a neighbor F not visited, what would happen at Step 12?
ADFS would backtrack immediately
BNode E would be skipped
CDFS would recurse into F, adding it to the call stack
DDFS would restart from node A
💡 Hint
Refer to the recursion behavior shown in Steps 2-5 in the execution_table.
Concept Snapshot
DFS explores graph nodes deeply by:
- Starting at a node, marking it visited
- Recursively visiting unvisited neighbors
- Using a call stack to track path
- Backtracking when no neighbors left
- Ends when all reachable nodes visited
Full Transcript
Depth First Search (DFS) starts at a chosen node, marks it visited to avoid repeats, then explores each neighbor deeply before backtracking. The call stack tracks the current path. When a node has no unvisited neighbors, DFS backtracks to explore other paths. This continues until all reachable nodes are visited. The visited set prevents cycles and repeated visits. The execution table shows each step's current node, visited nodes, call stack, and actions taken, illustrating the recursive nature of DFS.