Sub topic 4
The Odd Cycle Theorem
The previous chapter defined a proper two-colouring: an assignment of one of two colours to every vertex so that no edge joins two vertices of the same colour. That definition is easy to check once someone hands you a colouring. It gives no direct way to decide whether a colouring exists. Trying all 2^V assignments is not an option for real graphs.
The odd cycle theorem fixes this. It describes bipartiteness in terms of the graph's structure rather than in terms of colourings:
> A graph is bipartite if and only if it contains no cycle of odd length.
This chapter proves both directions. The proof also builds the algorithm used in the next chapter, so it is worth following each step closely.
This chapter assumes you know what paths, cycles, and shortest-path distance mean in an unweighted graph. It also assumes you have seen proof by contradiction and simple induction.
Walks, Paths, and Cycles
The proof depends on three related terms, so it helps to define them precisely:
- A walk is a sequence of vertices v0, v1, ..., vk where each consecutive pair is joined by an edge. Vertices and edges may repeat. Its length is k, the number of edges traversed.
- A closed walk is a walk that ends where it started (vk = v0).
- A cycle is a closed walk of length at least 3 in which no vertex repeats except that the first and last are the same. In a graph with self-loops, a loop counts as a cycle of length 1.
Cycles are the objects the theorem talks about, but closed walks are much easier to build in a proof. You can glue two paths and an edge together and get a closed walk without checking that the pieces avoid each other. The proof builds closed walks and then uses a lemma to turn them into cycles.
The Easy Direction: Bipartite Implies No Odd Cycle
Suppose the graph has a proper two-colouring with colours 0 and 1. Take any cycle v0, v1, ..., vk = v0.
Every edge joins vertices of different colours, so the colour flips at every step along the cycle. After i steps the colour of vi equals the colour of v0 if i is even, and the opposite colour if i is odd. The cycle returns to v0 after k steps, and v0 has the same colour as itself, so k must be even.
Every cycle in a bipartite graph therefore has even length. The argument never used the fact that the cycle has no repeated vertices, so it proves a stronger statement: every closed walk in a bipartite graph has even length.
The Key Lemma: Odd Closed Walks Contain Odd Cycles
The hard direction needs one tool.
Lemma. Every closed walk of odd length contains an odd cycle. The cycle uses only vertices and edges of the walk.
Proof by induction on the length L of the walk.
Let W = v0, v1, ..., vL = v0 be a closed walk with L odd.
- If no vertex repeats in W apart from v0 = vL, then W is already a cycle. In a simple graph, L cannot be 1 because there are no loops, and an odd number other than 1 is at least 3. If loops are allowed and L = 1, the loop is the odd cycle.
- Otherwise some vertex repeats: vi = vj for some 0 ≤ i < j < L, with (i, j) ≠ (0, L). Split W at that vertex into two closed walks:
- W1 = vi, vi+1, ..., vj, of length j − i.
- W2 = vj, vj+1, ..., vL = v0, v1, ..., vi, of length L − (j − i).
- Both lengths are positive and both are smaller than L. Their sum is L, which is odd, so exactly one of them is odd.
- By the induction hypothesis, the odd one contains an odd cycle, and so does W.
The lemma fails for even walks, and the failure explains why the theorem talks about odd cycles only. The walk u, v, u has length 2 and contains no cycle at all. It just crosses one edge and comes back. Even closed walks can come from back-and-forth movement. Odd closed walks cannot, because walking an edge out and back always adds an even amount.
The Hard Direction: No Odd Cycle Implies Bipartite
Assume the graph is connected and has no odd cycle. (A disconnected graph is handled one component at a time. A later chapter covers that case.) We need to produce a proper two-colouring.
Construction. Fix any vertex r as the root. For every vertex v let d(v) be the shortest-path distance from r to v. Colour v with d(v) mod 2.
Claim. This colouring is proper.
Proof by contradiction. Suppose some edge uv has both endpoints of the same colour, so d(u) and d(v) have the same parity. Build a closed walk:
- Follow a shortest path from r to u. Its length is d(u).
- Cross the edge from u to v. Its length is 1.
- Follow a shortest path from v back to r, reversed. Its length is d(v).
The total length is d(u) + d(v) + 1. Two numbers of equal parity add up to an even number, so the total is odd. By the lemma, this odd closed walk contains an odd cycle. That contradicts the assumption, so no such edge exists and the colouring is proper.
A sharper observation
For any edge uv, the distances d(u) and d(v) differ by at most 1, because a shortest path to u followed by the edge reaches v. So "same parity" for an edge really means d(u) = d(v): the edge joins two vertices on the same BFS level. Breadth-first search computes exactly these levels, which is why it is the natural algorithm for this problem. The chapter on extracting an odd cycle reuses the same fact to recover the actual cycle.
Why the choice of root does not matter
The construction works for every root r. Different roots can give different colourings, but in a connected bipartite graph they differ only by swapping the two colours. Once one vertex's colour is fixed, every neighbour's colour is forced, then every neighbour's neighbour, and so on through the whole component. A connected bipartite graph has exactly two proper two-colourings. A graph with c components has 2^c.
Examples and Edge Cases
| Graph | Cycle lengths present | Bipartite? | Reason |
|---|---|---|---|
| Any tree or forest | none | yes | No cycles at all, so no odd cycles |
| Cycle C_n, n even | n | yes | Alternate colours around the cycle |
| Cycle C_n, n odd | n | no | The whole graph is an odd cycle |
| Complete graph K_n, n ≥ 3 | 3 to n | no | Any three vertices form a triangle |
| Grid graph | 4, 6, 8, ... | yes | Colour by (row + column) mod 2 |
| Hypercube Q_d | even only | yes | Colour by number of 1 bits mod 2 |
| Graph with a self-loop | includes 1 | no | A loop is an odd cycle of length 1 |
| Two parallel edges between u and v | 2 | can be | A 2-cycle is even and does not break bipartiteness |
The theorem does not require every cycle to be short or every cycle to be checked. One odd cycle anywhere is enough to rule out bipartiteness. Even cycles never cause harm. A graph can have many even cycles and remain bipartite, as grids and hypercubes do.
Checking the Theorem by Brute Force
The script below checks the theorem on every simple graph with 5 labelled vertices. It decides two-colourability by trying all 2^5 colourings. It detects odd cycles independently using powers of the adjacency matrix. By the lemma, an odd cycle exists exactly when some vertex has an odd closed walk back to itself, and the shortest such walk is a cycle, so its length is at most n.
from itertools import combinations, product
def is_two_colourable(n, edges):
"""Brute force: try every assignment of colours 0/1."""
for colours in product((0, 1), repeat=n):
if all(colours[u] != colours[v] for u, v in edges):
return True
return False
def has_odd_cycle(n, edges):
"""True if some vertex has a closed walk of odd length <= n."""
adj = [[False] * n for _ in range(n)]
for u, v in edges:
adj[u][v] = adj[v][u] = True
walk = [row[:] for row in adj] # walk[i][j]: walk of length k from i to j
for k in range(1, n + 1):
if k % 2 == 1 and any(walk[i][i] for i in range(n)):
return True
walk = [[any(walk[i][m] and adj[m][j] for m in range(n))
for j in range(n)]
for i in range(n)]
return False
n = 5
all_pairs = list(combinations(range(n), 2))
checked = 0
for mask in range(1 << len(all_pairs)):
edges = [all_pairs[b] for b in range(len(all_pairs)) if mask >> b & 1]
assert is_two_colourable(n, edges) == (not has_odd_cycle(n, edges)), edges
checked += 1
print(f"Theorem holds on all {checked} graphs with {n} vertices")Running it prints Theorem holds on all 1024 graphs with 5 vertices. This does not replace the proof, but it is a cheap way to catch a misunderstanding. If you change has_odd_cycle to look for any closed walk, including even ones, the assertion fails at once on the single-edge graph.
What the Theorem Gives the Algorithm
The proof of the hard direction is constructive. It names a specific colouring, distance parity from a root, and shows that this colouring is proper whenever any proper colouring exists. That changes the search problem into a computation:
- Compute BFS levels from a root.
- Colour by level parity.
- Check every edge. If an edge joins two vertices on the same level, the proof's closed walk gives an odd cycle, so the graph is not bipartite.
There is no backtracking and no guessing, and each vertex and edge is handled a constant number of times. The next chapter turns these steps into code, and a later chapter recovers the odd cycle from the conflicting edge.
Exercise
The Petersen graph has 10 vertices, every vertex has degree 3, and its shortest cycle has length 5. Is it bipartite? Justify your answer using the theorem, without trying colourings.
Show answer
No. Its shortest cycle has length 5, which is odd. By the easy direction of the theorem, a bipartite graph has no odd cycles, so the Petersen graph cannot be bipartite.
Exercise
In the proof of the lemma, a closed walk of odd length L is split at a repeated vertex into two closed walks of lengths a and b with a + b = L. Explain why the same splitting argument cannot prove that every closed walk of even length contains an even cycle, and give a counterexample to that claim.
Show answer
When L is even, a and b are both even or both odd, so splitting does not force an even piece to keep its parity in a useful way, and the pieces can shrink down to length-2 walks that go back and forth over one edge, which are not cycles. A counterexample is the walk u, v, u in a graph with a single edge uv: it is a closed walk of length 2 but the graph has no cycle at all. Another is a triangle walked twice (length 6), which contains only the odd cycle of length 3.
Exercise
Root the 5-cycle 0-1-2-3-4-0 at vertex 0 and colour each vertex by its distance from 0 mod 2. Which edge breaks the colouring, and what odd closed walk does the proof build from it?
Show answer
The distances are d(0)=0, d(1)=1, d(4)=1, d(2)=2, d(3)=2, so the colours are 0,1,0,0,1 for vertices 0 to 4. Edge 2-3 joins two vertices with distance 2, both coloured 0. The proof builds the path 0-1-2, the edge 2-3, then the path 3-4-0, a closed walk of length 2 + 1 + 2 = 5. Here that walk is the whole 5-cycle.