Unit 3 of 9 · about 4 hours · derive, implement

DFS, cycle detection and bipartite testing

DFS supplies the recursion structure BFS cannot: finish times, edge classification and back-edge witnesses. Cycles and two-coloring are its first two customers.

You'll be able to

  • Implement recursive and iterative DFS with pre-order, post-order and on-stack hooks, and derive the bound.
  • Detect directed cycles with three-color DFS and undirected cycles with parent-tracking DFS, and exhibit the false positive each wrong pairing produces.
  • Test bipartiteness with two-color BFS or DFS and explain the odd-cycle obstruction.

Covers

DFS state, timestamps and recursive versus iterative traversalCycle detection in directed and undirected graphsBipartite testing and two-color invariants
1

Read

In this order, only the parts listed

0 of 4 done

  1. Depth-first searchDSA Handbook - graphs

    The recursive form; Three flavors: pre-order, post-order, on-stack; Worked example: LC 130 Surrounded Regions; Two pitfalls that bite

  2. Cycle detection in graphsDSA Handbook - graphs

    Why the obvious approach reports cycles that are not there (diamond); The other half: undirected graphs need a parent argument; Worked example: LC 1361

    Skim the Floyd tortoise-and-hare pointer; it belongs to linked lists, not graphs.

  3. Bipartite checkDSA Handbook - graphs

    Why the obvious approach wastes most of its work; The pattern (LC 785); Worked example on a 4-cycle and a triangle

  4. Depth-first search through cycle detectionAlgorithms - graph chapters

    Sections 6.1 Preorder and Postorder (with Classifying Vertices and Edges) and 6.2 Detecting Cycles (printed pp. 227-232)

2

Focus on

  • Mark before recursing, on entry. Recursing first lets the neighbor immediately recurse back, which loops until RecursionError.
  • GRAY means on the current stack; BLACK means finished. The diamond edge into a BLACK vertex is a benign forward edge; only a GRAY neighbor is a back edge and a cycle witness.
  • Undirected DFS needs one boolean plus the parent argument: the neighbor you arrived from does not count. Three colors are unnecessary there because undirected DFS has no forward or cross edges.
  • Bipartite colors are 1 and -1 with 0 for unvisited, flipped by negation across each edge, inside an outer loop over components. A same-color edge is the odd-cycle witness.
Common mistakes · 3
  • Two-color directed DFS: the diamond reports a cycle because one visited bit conflates on-stack with finished. The fix is the third color, not a second visited set.
  • Parentless undirected DFS: a single edge reports a cycle because each endpoint sees the other as visited. The fix is the parent argument, not more colors.
  • Recursive DFS on deep grids or chains: CPython caps the stack at 1000 frames by default (check with sys.getrecursionlimit), so a snake region or a 2000-chain crashes. Set the limit explicitly or ship the iterative form.
3

Try it yourself

Closed-book, before checking anything

From a blank file with the course Python (3.13.14), run three-color DFS on the diamond [(0,1),(0,2),(1,3),(2,3)] and report no cycle, and on [(0,1),(1,2),(2,0)] report a cycle. Then run two-color BFS on [[1,3],[0,2],[1,3],[0,2]] (True) and on [[1,2],[0,2],[0,1]] (False).
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Prove a directed graph is acyclic iff DFS finds no back edge.Answer

    If a back edge exists, is an ancestor of , so the tree path from to plus the edge closes a cycle. Conversely, if a cycle exists, let be its first vertex DFS discovers. Every other cycle vertex is still white then, so each becomes a descendant of , and the cycle edge from 's predecessor back to reaches while is still GRAY: a back edge.

  2. 2.Prove a graph is bipartite iff it has no odd cycle.Answer

    If 2-colored, any cycle alternates colors and must return to its start color after an even number of steps, so all cycles are even. If no odd cycle exists, BFS two-coloring never assigns the same color to both ends of an edge: a conflict would close an odd cycle through the BFS tree, contradiction.

  3. 3.Why does the bipartite outer loop cost nothing on connected graphs?Answer

    Every later start is already colored, so each trips the continue immediately: checks total. Skipping the loop misses odd cycles hiding in unvisited components.

  4. 4.LC 130 floods from the border instead of testing each region. Why is the inversion sound?Answer

    Border-connected regions are exactly the ones to keep; DFS marks all of them, so every unmarked O is surrounded by construction. Testing from inside cannot see the border without the same outward walk.

5

Practice

  • LeetCode 130: Surrounded Regions

    The two listed examples ( board with two interior O cells flipping; single-cell board)

    Pre-order DFS from every border O, marking first and recursing second. After-attempt reference is the DSA Handbook 8.2 worked example.

  • LeetCode 785: Is Graph Bipartite?

    Examples 1 (triangle inside, False) and 2 (4-cycle, True)

    Two-color BFS with the outer component loop. After-attempt reference is the DSA Handbook 8.7 worked example.

You're done when

Your blank-file script prints the four reproduce verdicts (no cycle, cycle, True, False), and LC 130 plus LC 785 pass with the visited discipline stated up front.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 3 more in the section
  • DSA Handbook - graphsHandbook Academy · primary

    Study all of Part 8 in order. For each algorithm, write the representation, invariant, forbidden input assumptions and independently checkable output before reading the implementation.

  • Algorithms - graph chaptersJeff Erickson · required

    Use the basic graph, depth-first search, minimum spanning tree, shortest path and flow chapters, and lecture note 11 Disjoint Sets for union-find, for correctness proofs and counterexamples to tempting wrong algorithms.

Also in this section

  • AlgoMaster DSA - graph sliceAlgoMaster · required

    Attempt Number of Islands, Course Schedule II and Network Delay Time cold. They cover traversal, dependency modeling and weighted shortest paths; add one narrative problem only if graph modeling remains weak.

  • Hypothesis - generated graph testsHypothesis · support

    Generate small sparse, dense, disconnected, cyclic, parallel-edge and weighted graphs. Compare optimized results with exhaustive or deliberately simple references and retain shrunk failures.

  • NeetCode - unseen graph problemsNeetCode · optional

    Use two unseen story problems without graph tags. Grade the model, assumptions, certificate and tests as well as the final answer.