Sub topic 11
Checking Bipartiteness as Edges Arrive with Parity Union-Find
This chapter assumes you know proper two-colourings, the odd cycle theorem, and how BFS two-colouring works. It also assumes basic union-find (disjoint set union, or DSU): find returns a set's representative, and union merges two sets. Path compression should be familiar.
The online problem
BFS two-colouring needs the whole graph up front. Sometimes edges arrive one at a time, and after each edge you must answer one question: is the graph still bipartite? Examples include a stream of "these two people must be on different teams" constraints, or edges added while a puzzle is being built.
You could rerun BFS after every edge. That costs O(V + E) per edge, which adds up to O(E · (V + E)) over the whole stream. A parity union-find answers each query in nearly constant amortised time.
The working model
Inside one connected component, a two-colouring is fixed once you pick the colour of a single vertex. Flipping every colour gives the only other valid colouring. So you don't need absolute colours. You only need each vertex's colour relative to a reference vertex, and the DSU root of the component works as that reference.
Each node stores one extra bit:
parity[x] = 0means x has the same colour as its parent.parity[x] = 1means x has the opposite colour.
A vertex's colour relative to its root is the XOR of the parity bits along its path to the root. During path compression, find folds those bits into one, so each vertex points straight at the root with the correct relative colour.
When the edge (u, v) arrives, there are two cases:
| Situation | Meaning | Action |
|---|---|---|
| Different roots | The edge joins two components | Merge them, choosing the parity so that u and v end up with different colours |
| Same root, different parity | u and v already differ in colour | Nothing to do. The graph is still bipartite |
| Same root, same parity | The edge closes an odd cycle | Report that the graph is not bipartite |
Why the merge parity is `pu ^ pv ^ 1`
After the merge, u's colour relative to the new root rv is pu ^ parity[ru]. We want that to equal pv ^ 1, the opposite of v's colour. Solving pu ^ parity[ru] = pv ^ 1 gives parity[ru] = pu ^ pv ^ 1. No other vertex in u's old component needs updating, because each one still reaches rv through ru.
The implementation
class ParityDSU:
"""Union-find where parity[x] is x's colour relative to its parent."""
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 # now relative to root
self.parent[x] = root # path compression
return root, self.parity[x]
def add_edge(self, u, v):
"""Record that u and v must differ; return False if this breaks bipartiteness."""
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 triangleTrace the triangle. After the first two edges, 0 points to 1 with parity 1, and 1 points to 2 with parity 1. Calling find(0) gives relative colour 1 XOR 1 = 0, the same colour as root 2. The edge (2, 0) joins two vertices in the same set with equal parity, so it closes an odd cycle.
Once the graph stops being bipartite, it stays that way, because adding edges never removes a cycle. Most applications record the index of the first bad edge and stop.
Common errors
- Forgetting to update parity during compression. If you re-point
parent[x]to the root but leaveparity[x]unchanged, the bit still describes x's relation to its old parent, so later answers can be wrong. - Setting the merge parity to 1 every time. That is correct only when u and v are both roots. In general, use
pu ^ pv ^ 1. - Expecting deletions to work. Union-find cannot split sets. If edges can be removed, you need an offline or dynamic-connectivity approach.
Exercise
Edges arrive in the order (0,1), (2,3), (1,2), (3,0), (0,2) on four vertices. After which edge does add_edge first return False, and why?
Show answer
It first returns False on the fifth edge, (0,2). The first four edges form the 4-cycle 0-1-2-3-0, which has even length, so each of them returns True. The colours alternate around the cycle, so 0 and 2 get the same colour. The edge (0,2) joins two vertices in the same set with equal parity and closes the odd cycle 0-1-2-0.
Exercise
Why does a merge change the parity of only ru, and not of every vertex in u's component?
Show answer
Every vertex in u's old component stores its colour relative to ru, either directly or through a chain that ends at ru. Giving ru the correct parity relative to rv shifts every one of those vertices correctly the next time find walks through ru.