0
0
DSA Typescriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - DFS Depth First Search on Graph
Start at root node
Mark node as visited
Explore first unvisited neighbor
Recursive DFS call on neighbor
All neighbors visited?
NoRepeat for next neighbor
Yes
Backtrack to previous node
All nodes visited?
NoContinue
Yes
End
Start from a node, visit it, then recursively visit each unvisited neighbor deeply before backtracking.
Execution Sample
DSA Typescript
function dfs(node, visited) {
  visited.add(node);
  for (const neighbor of graph[node]) {
    if (!visited.has(neighbor)) dfs(neighbor, visited);
  }
}
This code visits a node, marks it visited, then recursively visits all unvisited neighbors.
Execution Table
StepOperationCurrent NodeVisited SetStack (Call Trace)Action TakenGraph State
1Start DFSA{}[]Visit AA: [B, C], B: [D], C: [E], D: [], E: []
2Visit NodeA{A}[A]Mark A visitedSame
3Explore neighborA{A}[A]Check neighbor BSame
4Visit NodeB{A}[A, B]Mark B visitedSame
5Explore neighborB{A, B}[A, B]Check neighbor DSame
6Visit NodeD{A, B}[A, B, D]Mark D visitedSame
7Explore neighborD{A, B, D}[A, B, D]No neighbors to visitSame
8BacktrackD{A, B, D}[A, B]Return to BSame
9All neighbors visited?B{A, B, D}[A, B]Yes, backtrackSame
10BacktrackB{A, B, D}[A]Return to ASame
11Explore neighborA{A, B, D}[A]Check neighbor CSame
12Visit NodeC{A, B, D}[A, C]Mark C visitedSame
13Explore neighborC{A, B, D, C}[A, C]Check neighbor ESame
14Visit NodeE{A, B, D, C}[A, C, E]Mark E visitedSame
15Explore neighborE{A, B, D, C, E}[A, C, E]No neighbors to visitSame
16BacktrackE{A, B, D, C, E}[A, C]Return to CSame
17All neighbors visited?C{A, B, D, C, E}[A, C]Yes, backtrackSame
18BacktrackC{A, B, D, C, E}[A]Return to ASame
19All neighbors visited?A{A, B, D, C, E}[A]Yes, DFS completeSame
20End DFS-{A, B, D, C, E}[]All nodes visitedSame
💡 All nodes visited, no unvisited neighbors remain, DFS ends
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 12After Step 14Final
visited{}{A}{A, B}{A, B, D}{A, B, D, C}{A, B, D, C, E}{A, B, D, C, E}
stack[][A][A, B][A, B, D][A, C][A, C, E][]
current_node-ABDCE-
Key Moments - 3 Insights
Why do we mark a node as visited before exploring its neighbors?
Marking a node visited early (see Step 2) prevents revisiting it during neighbor checks (Step 3), avoiding infinite loops.
Why do we backtrack after visiting all neighbors?
Backtracking (Steps 8, 10, 16, 18) happens when no unvisited neighbors remain, so DFS returns to previous nodes to explore other paths.
Why does the stack grow and shrink during DFS?
The stack (call trace) grows when DFS goes deeper (Steps 4, 6, 12, 14) and shrinks when backtracking (Steps 8, 10, 16, 18), reflecting recursion depth.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the visited set after Step 6?
A{A, B}
B{A, B, D}
C{A, B, C}
D{A, B, D, C}
💡 Hint
Check the 'Visited Set' column at Step 6 in the execution table.
At which step does DFS first backtrack from node D?
AStep 8
BStep 7
CStep 9
DStep 10
💡 Hint
Look for 'Backtrack' operation with current node D in the execution table.
If node E had a neighbor F not visited yet, what would happen at Step 15?
ADFS would skip F and finish
BDFS would backtrack immediately
CDFS would visit F next, stack grows
DDFS would restart from A
💡 Hint
At Step 15, DFS explores neighbors; unvisited neighbors cause recursive calls and stack growth.
Concept Snapshot
DFS visits nodes deeply before backtracking.
Start at a node, mark visited.
Recursively visit unvisited neighbors.
Use stack (call trace) to track path.
Backtrack when no neighbors left.
Ends when all reachable nodes visited.
Full Transcript
Depth First Search (DFS) starts at a chosen node, marks it visited, then explores each neighbor deeply before backtracking. The process uses recursion, which acts like a stack to remember the path. Each step marks nodes visited to avoid repeats. When a node has no unvisited neighbors, DFS backtracks to explore other paths. This continues until all nodes reachable from the start are visited. The execution table shows each step, the current node, visited nodes, and the call stack. Key moments include marking visited early to prevent loops, backtracking when no neighbors remain, and stack changes reflecting recursion depth.