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 be a simple undirected graph, and let be the bijection of line L1. After line L2, each list holds the neighbours of in increasing order of . For a vertex and a vertex , write ; since is sorted by , is a prefix of .
Termination. Lines L4 and L5 run over finite lists. Each pass of the loop of lines L7 to L11 increases or 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 with , lines L6 to L11 append exactly for the vertices , once each.
Proof. Write and , and let and be their prefixes and . The loop of line L7 runs while is inside and is inside , since the entries of and with are exactly the prefixes.
We show the invariant: every common element of and that is not yet appended has position at least in and at least in , and no element is appended twice. At the start, and nothing is appended. In a pass with and :
- If , then is not in at a position , since is sorted and already has a larger ; and by the invariant, is not a common element at a position that is still to be found. So is not a common element still to be found, and skipping it keeps the invariant.
- The case is symmetric.
- If , then , since 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, has passed the end of or the end of , so by the invariant no common element is left to find. An element appended is a common element by the third case.
Theorem. COMPACT-FORWARD returns each triangle of exactly once, and each set it returns is a triangle.
Proof. Only triangles. By Lemma 1, a returned set has (line L5), and , and , so the three vertices are distinct and pairwise adjacent.
Each triangle at least once. Let be a triangle, named so that . Line L4 reaches , line L5 reaches since and , and . By Lemma 1, is appended in this merge.
Each triangle at most once. Every append of happens in the merge for some pair with and . This order is unique, so , , . Line L5 visits each pair once, since occurs once in , and Lemma 1 appends once in that merge.