A Bit of History
Bipartiteness testing traces back to Dénes Kőnig's foundational 1936 graph theory textbook, which formally characterized bipartite graphs as exactly those containing no odd-length cycle — a theorem that translates directly into a linear-time algorithmic test via BFS or DFS 2-coloring. The general $k$-colorability problem's difficulty was cemented in 1972 by Richard Karp, who proved 3-Colorability NP-complete in the same paper that established Hamiltonian Cycle's hardness — meaning the jump from "2 colors" to "3 colors" isn't a small step up in difficulty, but a leap across one of computer science's most fundamental complexity boundaries.
2-Colorability: BFS/DFS Bipartiteness Test
A graph is 2-colorable (equivalently, bipartite) if and only if it contains no odd-length cycle. This can be tested in a single linear-time traversal:
- Assign the starting vertex of each connected component color 0.
- Run BFS or DFS; whenever you traverse an edge $(u, v)$, assign $v$ the opposite color from $u$.
- If you ever encounter an edge $(u, v)$ where both endpoints are already colored the same, the graph is not bipartite — reject immediately.
- If the traversal completes without conflict across every component, the graph is bipartite, and the 2-coloring is a valid partition into two independent sets.
Why This Works
BFS/DFS naturally alternates colors by distance parity from the root — vertices at even distance get one color, odd distance the other. A same-color conflict on edge $(u,v)$ means $u$ and $v$ are at the same parity distance from the root, which (combined with the tree-path back to their common ancestor) forms an odd cycle — exactly the forbidden structure Kőnig's theorem rules out.
k-Colorability: Backtracking Search
For $k \geq 3$, no known polynomial-time test exists. The standard approach is backtracking over vertex-color assignments:
- Process vertices in a fixed order (e.g., vertex index, or a heuristic order like most-constrained-first).
- For each vertex, try each of the $k$ colors in turn, but only if no already-colored neighbor already has that color.
- If a color choice succeeds, recurse to the next vertex. If all $k$ colors fail for the current vertex, backtrack and try the previous vertex's next color option.
- If every vertex gets successfully colored, a valid $k$-coloring exists; if backtracking exhausts all options at the root, no valid $k$-coloring exists.
Worked Example
2-Colorability: square graph 0-1-2-3-0 (a 4-cycle, even length). BFS from 0: color(0)=A, color(1)=B, color(2)=A, color(3)=B. Check closing edge 3-0: colors B and A differ — no conflict, graph is bipartite.
3-Colorability: triangle graph 0-1-2-0 plus an isolated vertex 3 connected to all three (a "wheel" $W_3$, which is $K_4$). Try color(0)=1: color(1) must differ, try 2. color(2) must differ from both 0 and 1, try 3. color(3) must differ from 0, 1, AND 2 — but only 3 colors exist and all three are already used by its neighbors! Backtrack exhausted — $K_4$ requires 4 colors, confirming it is NOT 3-colorable.
Complexity Analysis
| Problem | Time Complexity | Complexity Class |
|---|---|---|
| 2-Colorability (Bipartiteness) | $O(V + E)$ | P (polynomial) |
| $k$-Colorability, $k \geq 3$ | $O(k^V)$ worst case | NP-complete |
Implementation
Real-World Applications
Bipartite Matching Preprocessing & SAT Solver Reductions
Bipartiteness testing is a standard preprocessing step before applying bipartite-specific algorithms like Hopcroft-Karp or Hungarian Algorithm — verifying the graph actually qualifies. 3-Colorability's NP-completeness makes it a canonical target for reduction proofs; many SAT solvers and constraint satisfaction engines are benchmarked by translating 3-coloring instances into Boolean satisfiability problems and back.
Exercises
- Prove Kőnig's theorem in one direction: show that any graph containing an odd cycle cannot be 2-colored.
- Determine the chromatic number of the Petersen graph via backtracking and compare it to its known value (3).
- Add most-constrained-vertex ordering (color the highest-degree vertex first) to the backtracking search and measure the practical speedup.
- Challenge: Implement a SAT-based 3-colorability solver by encoding "vertex $v$ has color $c$" as boolean variables and constraints, then compare its performance to direct backtracking.
Limitations
The P vs. NP Cliff at k=3
There is no smooth complexity gradient here — 2-colorability is solvable in linear time, but 3-colorability (and every $k \geq 3$) is NP-complete, with no known sub-exponential algorithm. Backtracking with good heuristics (DSatur-style ordering, constraint propagation) helps in practice but offers no worst-case guarantee.