Algorithm Search⌘K

Proofs: Bipartiteness testing

These are the human proofs of Bipartiteness testing, at full length.

Bipartiteness testing returns a 2-coloring or an odd cycle full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be an undirected graph, with self-loops and parallel edges allowed. A self-loop at vv puts vv in its own adjacency list. For a search started at ss in line L3, let δ(s,v)\delta(s, v) be the number of edges of a shortest path from ss to vv.

Lemma 1 (layers). Consider the search from ss, lines L3 to L8, and suppose it runs to the end without a return. Then each vertex vv reachable from ss gets a color, color[v]=δ(s,v) mod 2\mathrm{color}[v] = \delta(s, v) \bmod 2, and for v≠sv \ne s, parent[v]\mathrm{parent}[v] is a neighbor of vv with δ(s,parent[v])=δ(s,v)−1\delta(s, \mathrm{parent}[v]) = \delta(s, v) - 1. The same holds, up to the moment of a return at line L8, for every vertex colored so far.

Proof. Lines L3 to L8 are the breadth-first search of the page on breadth-first search, with color[v]≠NIL\mathrm{color}[v] \ne \mathrm{NIL} in the role of "discovered", and with an extra test at line L8 that only adds a return. That page proves that BFS discovers each reachable vertex, sets dist[w]=dist[u]+1\mathrm{dist}[w] = \mathrm{dist}[u] + 1 when it discovers ww from uu, and ends with dist[v]=δ(s,v)\mathrm{dist}[v] = \delta(s, v); the values set before any time are already final. Line L7 sets color[w]=1−color[u]\mathrm{color}[w] = 1 - \mathrm{color}[u] at the same moment, so by induction on the order of discovery, color[v]=dist[v] mod 2=δ(s,v) mod 2\mathrm{color}[v] = \mathrm{dist}[v] \bmod 2 = \delta(s, v) \bmod 2, and parent[w]=u\mathrm{parent}[w] = u is a neighbor with distance one less. □\square

Lemma 2 (edges). For each edge {u,w}\{u, w\} with uu reachable from ss, ∣δ(s,u)−δ(s,w)∣≤1|\delta(s, u) - \delta(s, w)| \le 1.

Proof. A shortest path to uu followed by the edge is a walk to ww, so δ(s,w)≤δ(s,u)+1\delta(s, w) \le \delta(s, u) + 1; by symmetry δ(s,u)≤δ(s,w)+1\delta(s, u) \le \delta(s, w) + 1. □\square

Theorem. BIPARTITE-TEST stops. If it returns at line L9, color\mathrm{color} is a proper 2-coloring. If it returns at line L8 with the vertices uu and ww, the output CC is a list of vertices of odd length in which each two consecutive vertices, and the last and the first, are adjacent; when u≠wu \ne w the vertices of CC are distinct, so CC is a simple cycle of odd length. So line L9 is reached if and only if GG is bipartite.

Proof.

Termination. Each vertex is enqueued at most once, because line L7 enqueues only a vertex with color NIL and gives it a color at once, and line L3 starts a search only from a vertex with color NIL. So the loops of lines L4 and L6 make at most nn and 2m+(number of loops)2m + (\text{number of loops}) steps in total. The climb of line L10 stops by the argument below.

Return at line L9. Let {u,w}\{u, w\} be an edge. Both ends are in the same component, so they get colors in the same search. When the later of the two, say uu, is dequeued at line L5, line L6 scans ww, which already has a color (or gets one at line L7 from uu, which is the opposite color). If ww had the color of uu, line L8 would have returned. Colors never change after they are set. So at the end color[u]≠color[w]\mathrm{color}[u] \ne \mathrm{color}[w] for each edge, and each vertex has a color because the loop of line L2 starts a search at each uncolored vertex. So color\mathrm{color} is a proper 2-coloring.

Return at line L8. The return happens while the search from some ss runs, with uu dequeued, ww a neighbor of uu with a color, and color[w]=color[u]\mathrm{color}[w] = \mathrm{color}[u]. Both uu and ww were colored in this search, as ww is adjacent to uu. By Lemma 1 their colors are the parities of δ(s,u)\delta(s, u) and δ(s,w)\delta(s, w), which are therefore equal mod 2. By Lemma 2 these distances differ by at most 1, so they are equal: δ(s,u)=δ(s,w)=h\delta(s, u) = \delta(s, w) = h.

If u=wu = w (a self-loop), line L10 does not climb and C=(u)C = (u), of length 1, closed by the loop. Otherwise, by Lemma 1, each parent step lowers the distance by exactly 1. So after jj steps, xx and yy are both at distance h−jh - j. When j=hj = h, both are at distance 0, so both are ss, and the loop of line L10 stops after at most hh steps. Let j∗≤hj^* \le h be the first jj with x=yx = y, and zz that vertex. The path PP from uu up to zz and the path QQ from ww up to zz have j∗j^* edges each, and their vertices have distinct distances along each path. They share only zz: a common vertex xi=ykx_i = y_k has h−i=h−kh - i = h - k, so i=ki = k, and i<j∗i < j^* would contradict the choice of j∗j^*. Line L11 lists PP from uu to zz, then QQ from zz back to ww, which is 2j∗+12 j^* + 1 distinct vertices, with consecutive vertices joined by tree edges, and ww joined to uu by the edge that triggered line L8. The length is odd. (j∗≥1j^* \ge 1 because u≠wu \ne w.)

The final claim. If line L9 is reached, GG has a proper 2-coloring, so it is bipartite. If line L8 is reached, GG has a closed walk of odd length. Along it a 2-coloring would have to switch color at each of an odd number of steps and return to the start color, which is impossible. So GG is not bipartite. ■\blacksquare

Back to Bipartiteness testing