Proofs: Triangle listing by degree ordering
These are the human proofs of Triangle listing by degree ordering, at full length.
Triangle listing by degree ordering reports each triangle exactly once full
This proof shows: correctness, termination.
Let be a simple undirected graph, and let with the lexicographic order. Write when . Since the second entries are the distinct vertex numbers, is a strict total order on .
Lemma 1 (the oriented graph). After line L2, for each edge exactly one of and holds, namely the one from the -smaller end. No other pair appears in an out-list, and no out-list holds a vertex twice.
Proof. Line L2 puts in exactly when is a neighbour of and . Since is a strict total order and , exactly one of and holds. The graph has no repeated edges, so a neighbour occurs once in .
We write for . By Lemma 1, if and only if and .
Lemma 2 (marks). At the start of each iteration of line L5, every mark is false. During lines L7 to L9 of the iteration for , is true if and only if .
Proof. By induction on the iterations. Before the first one, line L3 sets every mark to false. In the iteration for , line L6 sets to true for exactly the with , and lines L7 to L9 do not change any mark. So the second claim holds. Line L10 sets the same marks back to false, so every mark is false at the start of the next iteration.
Lemma 3 (soundness). Each set appended at line L9 is a triangle with .
Proof. At line L9 we have (line L7), (line L8), and true, so by Lemma 2. By Lemma 1, the three pairs , and are edges, and . Since is strict, the three vertices are distinct.
Lemma 4 (completeness and uniqueness). Each triangle of is appended exactly once.
Proof. Let be a triangle, named so that . By Lemma 1, , and .
At least once. In the iteration for , line L7 reaches because , line L8 reaches because , and is true by Lemma 2 because . So line L9 appends .
At most once. Suppose line L9 appends at the step . By Lemma 3, and . The order is total, so the only way to list the three vertices in increasing order is . Hence . Each value of is taken once at line L5, each once at line L7, and each once at line L8 (Lemma 1, no repeats), so the step happens once.
Theorem. TRIANGLES-BY-DEGREE stops, and the list it returns holds each triangle of exactly once and nothing else.
Proof. Termination: every loop runs over a finite list that the loop body does not change, so the algorithm stops after at most steps of the loops, a finite number. By Lemma 3, every entry of is a triangle, and by Lemma 4, every triangle is an entry of exactly once.
The cost bound. For an arc , gives . So every out-neighbour of has degree at least . If vertices have degree at least , then , and the out-neighbours of are among these vertices, so . Hence for , and when . The work of lines L7 to L9 over the whole run is , so the time is .