Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a finite undirected graph, with loops and parallel edges allowed, and let gg be its girth, the length of a shortest cycle, with g=∞g = \infty if GG is a forest. Fix one search, with root rr. Call the edges pe[w]\mathrm{pe}[w], for the vertices w≠rw \ne r that the search reaches, the tree edges.

Fact 1 (the search tree). Each tree edge pe[w]\mathrm{pe}[w] joins ww to pv[w]\mathrm{pv}[w], which was reached before ww, and dist[w]=dist[pv[w]]+1\mathrm{dist}[w] = \mathrm{dist}[\mathrm{pv}[w]] + 1. So the tree edges form a tree TrT_r on the reached vertices, which are the vertices of the component of rr; the path in TrT_r from rr to ww has dist[w]\mathrm{dist}[w] edges; and dist[w]\mathrm{dist}[w] is the distance from rr to ww in GG. 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 e={u,v}e = \{u, v\} be an edge in the component of rr that is not a tree edge, and let uu be the end dequeued first (either end, if u=vu = v). When uu scans ee at line L6, vv is already reached: otherwise line L7 would make e=pe[v]e = \mathrm{pe}[v] a tree edge. And e≠pe[u]e \ne \mathrm{pe}[u], since ee is not a tree edge. So line L8 compares dist[u]+dist[v]+1\mathrm{dist}[u] + \mathrm{dist}[v] + 1 with best\mathrm{best}, and afterwards best≤dist[u]+dist[v]+1\mathrm{best} \le \mathrm{dist}[u] + \mathrm{dist}[v] + 1.

Fact 3 (recorded edges are non-tree edges). If line L9 records (r,e,u,v)(r, e, u, v), then ee is not a tree edge of this search. Proof. e≠pe[u]e \ne \mathrm{pe}[u] by the test. A tree edge pe[w]\mathrm{pe}[w] has ww as an end, so only pe[v]\mathrm{pe}[v] is left to exclude, for u≠vu \ne v. If pe[v]=e\mathrm{pe}[v] = e, then vv was reached at line L7 by a scan of ee from its other end uu. But uu has ee only once in its list, since u≠vu \ne v, and in the present scan of that entry vv was already reached. So pe[v]≠e\mathrm{pe}[v] \ne e. □\square

Lemma A (closed walks). If a closed walk WW of length kk uses some edge ee exactly once, then GG has a cycle through ee of length at most kk.

Proof. If ee is a loop, it is a cycle of length 1. Otherwise rotate WW so that ee is its last edge, from aa to bb. The rest of WW is a walk from bb to aa of length k−1k - 1 that does not use ee. A shortest subwalk from bb to aa formed by its edges is a simple path PP (a repeated vertex would give a shorter one). Then PP followed by ee is a cycle of length at most kk: its inner vertices are distinct, and its edges are distinct because e∉Pe \notin P. □\square

Lemma B (every candidate is at least gg). If line L9 sets best\mathrm{best} to c=dist[u]+dist[v]+1c = \mathrm{dist}[u] + \mathrm{dist}[v] + 1, then GG has a cycle of length at most cc.

Proof. The path in TrT_r from rr to uu, the edge ee, and the path in TrT_r from vv to rr form a closed walk of length cc by Fact 1. By Fact 3, ee is not a tree edge, so it appears in this walk exactly once. Lemma A gives the cycle. □\square

Lemma C (a root on a shortest cycle finds gg). Let C=(c0,c1,…,cg−1,c0)C = (c_0, c_1, \ldots, c_{g-1}, c_0) be a shortest cycle with c0=rc_0 = r. After the search from rr, best≤g\mathrm{best} \le g.

Proof. All of CC lies in the component of rr. The tree TrT_r has no cycle, so some edge ee of CC, joining cic_i and ci+1c_{i+1} with indices modulo gg and 0≤i≤g−10 \le i \le g - 1, is not a tree edge. Walking along CC in either direction shows dist[cj]≤min⁡(j,g−j)\mathrm{dist}[c_j] \le \min(j, g - j) for 0≤j≤g0 \le j \le g. By Fact 2, after the search best≤dist[ci]+dist[ci+1]+1≤min⁡(i,g−i)+min⁡(i+1,g−i−1)+1\mathrm{best} \le \mathrm{dist}[c_i] + \mathrm{dist}[c_{i+1}] + 1 \le \min(i, g - i) + \min(i + 1, g - i - 1) + 1. If i+1≤g/2i + 1 \le g/2, this is at most i+(i+1)+1≤gi + (i + 1) + 1 \le g. If i≥g/2i \ge g/2, it is at most (g−i)+(g−i−1)+1=2g−2i≤g(g - i) + (g - i - 1) + 1 = 2g - 2i \le g. Otherwise i<g/2<i+1i < g/2 < i + 1, and it is at most i+(g−i−1)+1=gi + (g - i - 1) + 1 = g. □\square

Termination. Each search dequeues each vertex at most once and scans each list once, so it ends; there are n+1n + 1 searches. The climb at line L12 ends, because each step replaces aa or bb by its parent and lowers its distance by 1.

Theorem. If GG is a forest, GIRTH(G)(G) reports it. Otherwise it returns a cycle of length gg.

Proof. If GG is a forest, Lemma B shows that line L9 never runs, so found=NIL\mathrm{found} = \mathrm{NIL} at line L10. Otherwise gg is finite. By Lemma C, the search from a vertex of a shortest cycle makes best≤g\mathrm{best} \le g, and by Lemma B, best≥g\mathrm{best} \ge g at all times. So at the end best=g\mathrm{best} = g, and found=(r,e,u,v)\mathrm{found} = (r, e, u, v) was recorded with dist[u]+dist[v]+1=g\mathrm{dist}[u] + \mathrm{dist}[v] + 1 = g in the search from rr.

Line L11 repeats that search. The search is deterministic, so it rebuilds the same dist\mathrm{dist}, pv\mathrm{pv} and pe\mathrm{pe}. At line L12, while a≠ba \ne b, the one with the larger distance (or aa, on a tie) moves to its parent, so both walk up their paths in TrT_r towards rr and stop at the first common vertex ww, the lowest common ancestor of uu and vv. The path QuQ_u in TrT_r from ww to uu and the path QvQ_v from ww to vv share only ww. The returned closed walk is QuQ_u, then ee, then QvQ_v backwards. Its inner vertices are distinct, its tree edges are distinct, and ee is not a tree edge by Fact 3, so it is a cycle (the loop ee itself if u=vu = v). Its length is dist[u]+dist[v]+1−2 dist[w]≤g\mathrm{dist}[u] + \mathrm{dist}[v] + 1 - 2\,\mathrm{dist}[w] \le g, and no cycle is shorter than gg, so its length is exactly gg. ■\blacksquare

Back to Itai-Rodeh girth algorithm