Proofs: Itai-Rodeh girth algorithm
These are the human proofs of Itai-Rodeh girth algorithm, at full length.
The Itai-Rodeh girth algorithm returns a shortest cycle full
This proof shows: correctness, termination.
Let be a finite undirected graph, with loops and parallel edges allowed, and let be its girth, the length of a shortest cycle, with if is a forest. Fix one search, with root . Call the edges , for the vertices that the search reaches, the tree edges.
Fact 1 (the search tree). Each tree edge joins to , which was reached before , and . So the tree edges form a tree on the reached vertices, which are the vertices of the component of ; the path in from to has edges; and is the distance from to in . These are the standard properties of breadth-first search. A loop is never a tree edge, since it leads from a reached vertex to itself.
Fact 2 (non-tree edges are seen). Let be an edge in the component of that is not a tree edge, and let be the end dequeued first (either end, if ). When scans at line L6, is already reached: otherwise line L7 would make a tree edge. And , since is not a tree edge. So line L8 compares with , and afterwards .
Fact 3 (recorded edges are non-tree edges). If line L9 records , then is not a tree edge of this search. Proof. by the test. A tree edge has as an end, so only is left to exclude, for . If , then was reached at line L7 by a scan of from its other end . But has only once in its list, since , and in the present scan of that entry was already reached. So .
Lemma A (closed walks). If a closed walk of length uses some edge exactly once, then has a cycle through of length at most .
Proof. If is a loop, it is a cycle of length 1. Otherwise rotate so that is its last edge, from to . The rest of is a walk from to of length that does not use . A shortest subwalk from to formed by its edges is a simple path (a repeated vertex would give a shorter one). Then followed by is a cycle of length at most : its inner vertices are distinct, and its edges are distinct because .
Lemma B (every candidate is at least ). If line L9 sets to , then has a cycle of length at most .
Proof. The path in from to , the edge , and the path in from to form a closed walk of length by Fact 1. By Fact 3, is not a tree edge, so it appears in this walk exactly once. Lemma A gives the cycle.
Lemma C (a root on a shortest cycle finds ). Let be a shortest cycle with . After the search from , .
Proof. All of lies in the component of . The tree has no cycle, so some edge of , joining and with indices modulo and , is not a tree edge. Walking along in either direction shows for . By Fact 2, after the search . If , this is at most . If , it is at most . Otherwise , and it is at most .
Termination. Each search dequeues each vertex at most once and scans each list once, so it ends; there are searches. The climb at line L12 ends, because each step replaces or by its parent and lowers its distance by 1.
Theorem. If is a forest, GIRTH reports it. Otherwise it returns a cycle of length .
Proof. If is a forest, Lemma B shows that line L9 never runs, so at line L10. Otherwise is finite. By Lemma C, the search from a vertex of a shortest cycle makes , and by Lemma B, at all times. So at the end , and was recorded with in the search from .
Line L11 repeats that search. The search is deterministic, so it rebuilds the same , and . At line L12, while , the one with the larger distance (or , on a tie) moves to its parent, so both walk up their paths in towards and stop at the first common vertex , the lowest common ancestor of and . The path in from to and the path from to share only . The returned closed walk is , then , then backwards. Its inner vertices are distinct, its tree edges are distinct, and is not a tree edge by Fact 3, so it is a cycle (the loop itself if ). Its length is , and no cycle is shorter than , so its length is exactly .