Sub topic 9
Proving a Graph Is Not Bipartite: Finding an Odd Cycle
This chapter assumes you already know the BFS two-colouring algorithm and the odd cycle theorem, which says a graph is bipartite exactly when it has no odd-length cycle. You should also be able to read Python that uses collections.deque.
Why return a certificate
A tester that returns None for "not bipartite" gives you no way to check its answer. A certificate is a small piece of evidence that anyone can check quickly without rerunning the algorithm. If the graph is bipartite, the colouring is the certificate: check every edge and confirm its endpoints differ. If the graph is not bipartite, the odd cycle theorem tells us an odd cycle must exist, so we can return that cycle as the certificate. Checking it takes time proportional to its length.
Certificates help when debugging and when another system has to trust the result. They also help users. "Tasks 2, 1, 0, 4, 3 form a ring of conflicts" is more useful than "impossible."
Where the cycle comes from
BFS processes vertices in order of depth, so the endpoints of any edge have depths that differ by at most 1. BFS colours each vertex by the parity of its depth. If an edge (u, v) joins two vertices of the same colour, their depths have the same parity, and because they differ by at most 1 they must be equal.
Follow parent pointers up from u and from v together, one step at a time. Both walks are the same length, so they reach their lowest common ancestor (the deepest vertex that is an ancestor of both) on the same step, say after k steps. Each tree path from u or v up to that ancestor has k edges. The conflicting edge adds one more, so the cycle has 2k + 1 edges, which is odd.
The algorithm
Change the BFS tester so that it records parent and depth for each vertex. When it finds a conflict, it walks both endpoints up to their common ancestor.
from collections import deque
def find_odd_cycle(n, adj):
"""Return a list of vertices forming an odd cycle, or None if bipartite."""
colour = [-1] * n
parent = [-1] * n
depth = [0] * n
for s in range(n):
if colour[s] != -1:
continue
colour[s] = 0
queue = deque([s])
while queue:
u = queue.popleft()
for v in adj[u]:
if colour[v] == -1:
colour[v] = 1 - colour[u]
parent[v] = u
depth[v] = depth[u] + 1
queue.append(v)
elif colour[v] == colour[u]:
# u and v have equal BFS depth; walk both up to their common ancestor.
path_u, path_v = [u], [v]
a, b = u, v
while a != b:
a = parent[a]
b = parent[b]
path_u.append(a)
path_v.append(b)
# u .. ancestor, then down to v; the edge (v, u) closes the cycle.
return path_u + path_v[-2::-1]
return None
def is_odd_cycle(cycle, adj):
"""Check a certificate: odd length, distinct vertices, consecutive vertices adjacent."""
k = len(cycle)
if k % 2 == 0 or len(set(cycle)) != k:
return False
return all(cycle[(i + 1) % k] in adj[cycle[i]] for i in range(k))
pentagon = [[1, 4], [0, 2], [1, 3], [2, 4], [3, 0]]
cycle = find_odd_cycle(5, pentagon)
print(cycle) # [2, 1, 0, 4, 3]
print(is_odd_cycle(cycle, pentagon)) # TrueThe slice path_v[-2::-1] reverses path_v and drops the ancestor, which is already the last entry of path_u. The returned list is therefore a closed walk with no repeated vertex.
Common errors
| Mistake | Effect |
|---|---|
| Using DFS parents instead of BFS parents | Two same-coloured endpoints of an edge can have different depths, so walking both up in lockstep can step past the root and never meet. |
| Keeping the ancestor in both paths | The ancestor appears twice and the certificate fails the distinctness check. |
| Not resetting the walk per component | Not a problem in practice. Parent pointers never cross components, so the walk stays inside one BFS tree. |
Exercise
Run findoddcycle on the triangle adj = [[1, 2], [0, 2], [0, 1]]. Which edge causes the conflict, what is the common ancestor, and what list is returned?
Show answer
BFS from 0 colours 1 and 2 with colour 1. When it processes vertex 1, it finds neighbour 2 with the same colour, so the conflict edge is (1, 2). Walking up gives pathu = [1, 0] and pathv = [2, 0], so the common ancestor is 0. The function returns [1, 0] + [2] = [1, 0, 2], a cycle of 3 edges (k = 1, 2k + 1 = 3).
Exercise
Why does the lockstep walk need u and v to have equal depth, and why does BFS guarantee this when it finds a conflict?
Show answer
Moving both pointers one step at a time reaches the common ancestor at the same moment only if both start the same distance below it, which requires equal depth. In BFS, the endpoints of any edge differ in depth by at most 1. Same colour means same depth parity, so the depths must be equal.