Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a simple undirected graph, and let r(v)=(d(v),v)r(v) = (d(v), v) with the lexicographic order. Write u≺vu \prec v when r(u)<r(v)r(u) < r(v). Since the second entries are the distinct vertex numbers, ≺\prec is a strict total order on VV.

Lemma 1 (the oriented graph). After line L2, for each edge {v,w}∈E\{v, w\} \in E exactly one of w∈Out[v]w \in \mathrm{Out}[v] and v∈Out[w]v \in \mathrm{Out}[w] holds, namely the one from the ≺\prec-smaller end. No other pair appears in an out-list, and no out-list holds a vertex twice.

Proof. Line L2 puts ww in Out[v]\mathrm{Out}[v] exactly when ww is a neighbour of vv and v≺wv \prec w. Since ≺\prec is a strict total order and v≠wv \ne w, exactly one of v≺wv \prec w and w≺vw \prec v holds. The graph has no repeated edges, so a neighbour occurs once in Adj[v]\mathrm{Adj}[v]. □\square

We write v→wv \to w for w∈Out[v]w \in \mathrm{Out}[v]. By Lemma 1, v→wv \to w if and only if {v,w}∈E\{v, w\} \in E and v≺wv \prec w.

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 uu, mark[x]\mathrm{mark}[x] is true if and only if u→xu \to x.

Proof. By induction on the iterations. Before the first one, line L3 sets every mark to false. In the iteration for uu, line L6 sets mark[x]\mathrm{mark}[x] to true for exactly the xx with u→xu \to x, 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. □\square

Lemma 3 (soundness). Each set appended at line L9 is a triangle {u,v,w}\{u, v, w\} with u≺v≺wu \prec v \prec w.

Proof. At line L9 we have u→vu \to v (line L7), v→wv \to w (line L8), and mark[w]\mathrm{mark}[w] true, so u→wu \to w by Lemma 2. By Lemma 1, the three pairs {u,v}\{u, v\}, {v,w}\{v, w\} and {u,w}\{u, w\} are edges, and u≺v≺wu \prec v \prec w. Since ≺\prec is strict, the three vertices are distinct. □\square

Lemma 4 (completeness and uniqueness). Each triangle of GG is appended exactly once.

Proof. Let {a,b,c}\{a, b, c\} be a triangle, named so that a≺b≺ca \prec b \prec c. By Lemma 1, a→ba \to b, a→ca \to c and b→cb \to c.

At least once. In the iteration for u=au = a, line L7 reaches v=bv = b because a→ba \to b, line L8 reaches w=cw = c because b→cb \to c, and mark[c]\mathrm{mark}[c] is true by Lemma 2 because a→ca \to c. So line L9 appends {a,b,c}\{a, b, c\}.

At most once. Suppose line L9 appends {a,b,c}\{a, b, c\} at the step (u,v,w)(u, v, w). By Lemma 3, {u,v,w}={a,b,c}\{u, v, w\} = \{a, b, c\} and u≺v≺wu \prec v \prec w. The order ≺\prec is total, so the only way to list the three vertices in increasing order is (a,b,c)(a, b, c). Hence (u,v,w)=(a,b,c)(u, v, w) = (a, b, c). Each value of uu is taken once at line L5, each v∈Out[u]v \in \mathrm{Out}[u] once at line L7, and each w∈Out[v]w \in \mathrm{Out}[v] once at line L8 (Lemma 1, no repeats), so the step (a,b,c)(a, b, c) happens once. □\square

Theorem. TRIANGLES-BY-DEGREE(G)(G) stops, and the list TT it returns holds each triangle of GG 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 n+∑u(2d+(u)+∑v∈Out[u]d+(v))n + \sum_u (2d^+(u) + \sum_{v \in \mathrm{Out}[u]} d^+(v)) steps of the loops, a finite number. By Lemma 3, every entry of TT is a triangle, and by Lemma 4, every triangle is an entry of TT exactly once. ■\blacksquare

The cost bound. For an arc v→wv \to w, r(v)<r(w)r(v) < r(w) gives d(v)≤d(w)d(v) \le d(w). So every out-neighbour of vv has degree at least d(v)d(v). If kk vertices have degree at least d(v)≥1d(v) \ge 1, then k d(v)≤∑xd(x)=2mk\, d(v) \le \sum_x d(x) = 2m, and the out-neighbours of vv are among these kk vertices, so d+(v)≤k≤2m/d(v)d^+(v) \le k \le 2m/d(v). Hence d+(v)≤min⁡(d(v),2m/d(v))≤2md^+(v) \le \min(d(v), 2m/d(v)) \le \sqrt{2m} for d(v)≥1d(v) \ge 1, and d+(v)=0d^+(v) = 0 when d(v)=0d(v) = 0. The work of lines L7 to L9 over the whole run is ∑u∑v:u→v(1+d+(v))≤m+m2m\sum_u \sum_{v : u \to v} (1 + d^+(v)) \le m + m\sqrt{2m}, so the time is O(n+m3/2)O(n + m^{3/2}).

Back to Triangle listing by degree ordering