Algorithm Search⌘K

Proofs: Compact-forward algorithm

These are the human proofs of Compact-forward algorithm, at full length.

The compact-forward algorithm lists every triangle exactly once full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be a simple undirected graph, and let η:V→{0,…,n−1}\eta : V \to \{0, \ldots, n-1\} be the bijection of line L1. After line L2, each list Adj[x]\mathrm{Adj}[x] holds the neighbours of xx in increasing order of η\eta. For a vertex vv and a vertex xx, write N<v(x)={y∈Adj[x]:η(y)<η(v)}N_{<v}(x) = \{y \in \mathrm{Adj}[x] : \eta(y) < \eta(v)\}; since Adj[x]\mathrm{Adj}[x] is sorted by η\eta, N<v(x)N_{<v}(x) is a prefix of Adj[x]\mathrm{Adj}[x].

Termination. Lines L4 and L5 run over finite lists. Each pass of the loop of lines L7 to L11 increases ii or jj or both, and both are bounded by the lengths of the lists, so each merge stops. So the algorithm stops.

Lemma 1 (the merge). For an edge {v,u}\{v, u\} with η(v)<η(u)\eta(v) < \eta(u), lines L6 to L11 append {a,v,u}\{a, v, u\} exactly for the vertices a∈N<v(v)∩N<v(u)a \in N_{<v}(v) \cap N_{<v}(u), once each.

Proof. Write p=Adj[v]p = \mathrm{Adj}[v] and q=Adj[u]q = \mathrm{Adj}[u], and let p′p' and q′q' be their prefixes N<v(v)N_{<v}(v) and N<v(u)N_{<v}(u). The loop of line L7 runs while ii is inside p′p' and jj is inside q′q', since the entries of pp and qq with η<η(v)\eta < \eta(v) are exactly the prefixes.

We show the invariant: every common element cc of p′p' and q′q' that is not yet appended has position at least ii in pp and at least jj in qq, and no element is appended twice. At the start, i=j=0i = j = 0 and nothing is appended. In a pass with a=p[i]a = p[i] and b=q[j]b = q[j]:

  • If η(a)<η(b)\eta(a) < \eta(b), then aa is not in q′q' at a position ≥j\ge j, since qq is sorted and q[j]q[j] already has a larger η\eta; and by the invariant, aa is not a common element at a position <j< j that is still to be found. So aa is not a common element still to be found, and skipping it keeps the invariant.
  • The case η(a)>η(b)\eta(a) > \eta(b) is symmetric.
  • If η(a)=η(b)\eta(a) = \eta(b), then a=ba = b, since η\eta is injective. It is common to both prefixes, and it is appended now. Both indices move past it, and since the lists are sorted and have no repeated entries, it is not met again.

When the loop stops, ii has passed the end of p′p' or jj the end of q′q', so by the invariant no common element is left to find. An element appended is a common element by the third case. □\square

Theorem. COMPACT-FORWARD(G)(G) returns each triangle of GG exactly once, and each set it returns is a triangle.

Proof. Only triangles. By Lemma 1, a returned set {a,v,u}\{a, v, u\} has {v,u}∈E\{v, u\} \in E (line L5), a∈Adj[v]a \in \mathrm{Adj}[v] and a∈Adj[u]a \in \mathrm{Adj}[u], and η(a)<η(v)<η(u)\eta(a) < \eta(v) < \eta(u), so the three vertices are distinct and pairwise adjacent.

Each triangle at least once. Let {x,y,z}\{x, y, z\} be a triangle, named so that η(x)<η(y)<η(z)\eta(x) < \eta(y) < \eta(z). Line L4 reaches v=yv = y, line L5 reaches u=zu = z since z∈Adj[y]z \in \mathrm{Adj}[y] and η(z)>η(y)\eta(z) > \eta(y), and x∈N<y(y)∩N<y(z)x \in N_{<y}(y) \cap N_{<y}(z). By Lemma 1, {x,y,z}\{x, y, z\} is appended in this merge.

Each triangle at most once. Every append of {x,y,z}\{x, y, z\} happens in the merge for some pair (v,u)(v, u) with {a,v,u}={x,y,z}\{a, v, u\} = \{x, y, z\} and η(a)<η(v)<η(u)\eta(a) < \eta(v) < \eta(u). This order is unique, so a=xa = x, v=yv = y, u=zu = z. Line L5 visits each pair (v,u)(v, u) once, since uu occurs once in Adj[v]\mathrm{Adj}[v], and Lemma 1 appends xx once in that merge. ■\blacksquare

Back to Compact-forward algorithm