Graph Theory37 sections · 1633 units
Open in Course

DFS for Detection

Recursive approach

DFS also works for bipartite detection. The idea is the same: color the current node, then recursively color all neighbors with the opposite color. The recursive structure is clean. At each node, you check neighbors. If a neighbor is uncolored, recurse with the opposite color.

If already colored, verify it is the opposite. DFS and BFS find the same answer. The choice depends on style preference and stack depth concerns. For deep graphs, BFS avoids recursion limits.