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 be an undirected graph, with self-loops and parallel edges allowed. A self-loop at puts in its own adjacency list. For a search started at in line L3, let be the number of edges of a shortest path from to .
Lemma 1 (layers). Consider the search from , lines L3 to L8, and suppose it runs to the end without a return. Then each vertex reachable from gets a color, , and for , is a neighbor of with . 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 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 when it discovers from , and ends with ; the values set before any time are already final. Line L7 sets at the same moment, so by induction on the order of discovery, , and is a neighbor with distance one less.
Lemma 2 (edges). For each edge with reachable from , .
Proof. A shortest path to followed by the edge is a walk to , so ; by symmetry .
Theorem. BIPARTITE-TEST stops. If it returns at line L9, is a proper 2-coloring. If it returns at line L8 with the vertices and , the output is a list of vertices of odd length in which each two consecutive vertices, and the last and the first, are adjacent; when the vertices of are distinct, so is a simple cycle of odd length. So line L9 is reached if and only if 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 and steps in total. The climb of line L10 stops by the argument below.
Return at line L9. Let 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 , is dequeued at line L5, line L6 scans , which already has a color (or gets one at line L7 from , which is the opposite color). If had the color of , line L8 would have returned. Colors never change after they are set. So at the end for each edge, and each vertex has a color because the loop of line L2 starts a search at each uncolored vertex. So is a proper 2-coloring.
Return at line L8. The return happens while the search from some runs, with dequeued, a neighbor of with a color, and . Both and were colored in this search, as is adjacent to . By Lemma 1 their colors are the parities of and , which are therefore equal mod 2. By Lemma 2 these distances differ by at most 1, so they are equal: .
If (a self-loop), line L10 does not climb and , of length 1, closed by the loop. Otherwise, by Lemma 1, each parent step lowers the distance by exactly 1. So after steps, and are both at distance . When , both are at distance 0, so both are , and the loop of line L10 stops after at most steps. Let be the first with , and that vertex. The path from up to and the path from up to have edges each, and their vertices have distinct distances along each path. They share only : a common vertex has , so , and would contradict the choice of . Line L11 lists from to , then from back to , which is distinct vertices, with consecutive vertices joined by tree edges, and joined to by the edge that triggered line L8. The length is odd. ( because .)
The final claim. If line L9 is reached, has a proper 2-coloring, so it is bipartite. If line L8 is reached, 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 is not bipartite.