Sub topic 3
A DFS Alternative
This chapter assumes you already know the BFS two-colouring from the previous chapter, union-find basics, and Big-O notation.
Swapping the Queue for a Stack
Correctness does not depend on the order in which vertices are visited. Each edge only requires its two endpoints to have opposite colours. If you replace the BFS queue with a stack (stack.pop() instead of queue.popleft()), you get a depth-first two-colouring that gives the same answer in O(V + E) time. An iterative stack is better than recursion in Python, because a long path will hit the recursion limit.
Returning an Odd Cycle as Proof
A plain "no" is hard to check. BFS can return evidence instead. Suppose BFS finds an edge (u, v) whose endpoints have the same colour. Then u and v are at the same BFS depth. Walk both up their parent pointers until they meet at a common ancestor. The two paths have equal length k, so together with the edge (u, v) they form a cycle of length 2k + 1. That cycle is odd, which by the odd cycle theorem proves the graph is not bipartite. To support this, store parent and depth while colouring.
Modelling Problems
| Problem | Vertices | Edge means |
|---|---|---|
| Split rivals into two teams | People | "Cannot share a team" |
| Assign workers to jobs | Workers and jobs | "Worker can do job" |
| Exam scheduling in two slots | Exams | "Share a student" |
Edges That Arrive Over Time
Re-running BFS after every new edge costs O(V + E) per edge. A parity union-find instead stores each vertex's colour relative to its root:
class ParityDSU:
def __init__(self, n):
self.parent = list(range(n))
self.parity = [0] * n
def find(self, x):
if self.parent[x] == x:
return x, 0
root, p = self.find(self.parent[x])
self.parity[x] ^= p
self.parent[x] = root
return root, self.parity[x]
def add_edge(self, u, v):
ru, pu = self.find(u)
rv, pv = self.find(v)
if ru == rv:
return pu != pv
self.parent[ru] = rv
self.parity[ru] = pu ^ pv ^ 1
return True
dsu = ParityDSU(3)
print(dsu.add_edge(0, 1)) # True
print(dsu.add_edge(1, 2)) # True
print(dsu.add_edge(2, 0)) # False: closes a triangleMatching
A matching is a set of edges in which no two edges share a vertex. In bipartite graphs, augmenting-path algorithms such as Hopcroft–Karp find a maximum matching in O(E√V) time. This solves assignment problems like the workers-to-jobs row in the table above.
Why Three Colours Is Hard
With two colours, choosing one vertex's colour forces the colour of every other vertex in its component. That forcing is why the problem is linear. With three colours, each neighbour still has two options, so nothing is forced. Deciding whether a graph is three-colourable is NP-complete.
Exercise
In BFS two-colouring, a same-colour edge is found between u and v, both at depth 3, and their lowest common ancestor is at depth 1. How long is the extracted odd cycle?
Show answer
Each tree path has length 3 − 1 = 2, so the cycle has length 2 + 2 + 1 = 5.
Exercise
After calls addedge(0, 1) and addedge(1, 2), what does add_edge(0, 2) return, and why?
Show answer
True. Vertices 0 and 2 both differ from 1, so they share a colour, and the edge (0, 2) would put two same-coloured vertices together. Correction: it returns False, because the endpoints have equal parity relative to their shared root.