3.5.5 · D3Graphs

Worked examples — DFS — algorithm, stack - recursion, O(V+E), visited array

2,590 words12 min readBack to topic

The scenario matrix

Every graph DFS can face falls into one of these case classes. Think of it as a checklist — if we cover every row, we have covered DFS.

# Case class What makes it tricky Covered by
A Empty / isolated vertices, zero edges edges : is still right? Ex 1
B Simple tree (no cycles, one root) pure "go deep, backtrack" — the clean case Ex 2
C Cycle present (undirected or directed) visited array must stop the infinite loop Ex 3
D Disconnected (many components) one DFS launch is not enough Ex 4
E Directed graph, edge direction matters you can reach a node but not come back Ex 5
F Complete graph (densest possible) limiting behaviour: cost Ex 6
G Neighbour-order sensitivity (same graph, different lists) proves DFS order isn't unique Ex 7
H Real-world word problem model a maze / network as a graph Ex 8
I Exam twist — recursive vs iterative give different orders iterative-stack reverses neighbour scan Ex 9

Cell A (zero-edge) and cell F (max-edge) are the two limiting extremes — everything real lives between them.


Case A — the zero-edge extreme

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case B — the clean tree

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case C — a cycle, where visited earns its keep

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case D — disconnected graph, multiple launches

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case E — direction matters

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case F — the dense extreme

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case G — order isn't unique


Case H — a real-world maze

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

Case I — the exam twist


Recall checkpoints

Recall Did you internalise every cell?

Cover the matrix mentally and answer:

  1. Why is a zero-edge graph the reason we write not ? ::: Because with the work is still ; would wrongly say constant.
  2. In a graph with a cycle, at which step does the visited array first prevent a loop? ::: The moment a neighbour edge points to an already-marked vertex — that vertex is skipped instead of re-entered.
  3. How many DFS launches does a graph with connected components need to cover all vertices? ::: Exactly — one per component.
  4. Does DFS order change if you reorder a vertex's neighbour list? ::: Yes — the set visited is fixed but the sequence depends on neighbour order.
  5. Why do recursive and explicit-stack DFS sometimes visit in different orders? ::: The stack is LIFO, so pushing neighbours left-to-right pops them right-to-left, reversing each batch.

Connections

  • 3.5.05 DFS — algorithm, stack - recursion, O(V+E), visited array (Hinglish) — the parent, in Hinglish.
  • BFS — Breadth-First Search — compare orders (Ex 2, Ex 9).
  • Stack data structure · Recursion and the Call Stack — the LIFO twist in Ex 9.
  • Cycle Detection — built on Ex 3's machinery.
  • Topological Sort — needs the directed structure of Ex 5.
  • Connected Components — the multi-launch idea of Ex 4.
  • Handshake Lemma — the used in Ex 3, 6.