Proofs: Lengauer-Tarjan algorithm
These are the human proofs of Lengauer-Tarjan algorithm, at full length.
The Lengauer-Tarjan algorithm computes the immediate dominators full
This proof shows: correctness, termination.
Setting. is a directed graph with start vertex . Only the vertices reachable from matter: an edge from an unreachable vertex lies on no path from , so dropping such edges, as line L4 does, changes no dominator. After line L1 the reachable vertices are identified with their preorder numbers in a depth-first search from , so . is the DFS tree. We write if is an ancestor of in (possibly ) and for a proper ancestor. All paths start where stated; "dominates" refers to paths from . Dominance is a partial order whose strict dominators of a vertex form a chain, and is the lowest of them; this is proved on the page of the dominator tree. The semidominator for is the least such that some path has for ; we call such a path an sdom path for . Removing a cycle from an sdom path gives an sdom path, so we may take them simple.
Lemma 0 (preorder intervals). The descendants of in are exactly the numbers , where is the size of the subtree of . The ancestors of a vertex form the tree path from to , with numbers increasing downward.
Proof. The DFS numbers when it first reaches it and then numbers all vertices reached before it leaves , which are exactly the descendants of , so they get the next numbers. A parent is numbered before its child.
Lemma 1 (DFS edges). If is an edge with , then .
Proof. When the DFS examines the edge , is on the stack. If is unvisited, it becomes a child of . Otherwise was numbered after and before this moment, that is, while was on the stack, so is a descendant of .
Lemma 2 (path lemma). If , every path from to contains a common ancestor of and .
Proof. Let be the vertex of least number on the path. By induction along the path, every vertex after is a descendant of : for an edge with a descendant of , either , and is a descendant of by Lemma 1, or , and lies in the interval of by Lemma 0. So is a descendant of , and places in the interval of as well.
Lemma 3 (positions). For : , , and .
Proof. The tree path from to is a path in , so every dominator of is on it, and the strict ones are proper ancestors. The edge from the parent of is an sdom path, so . Take an sdom path from to . By Lemma 2 it contains a common ancestor of and , so ; internal vertices are and itself is not an ancestor of , so , and . Finally, the tree path from to followed by the sdom path is a path to whose vertices are ancestors of , numbers , or . A proper ancestor of with has and lies on neither part, so it does not dominate . Hence , a proper ancestor of , is an ancestor of .
Lemma 4 (nesting). If and , then or .
Proof. Let satisfy . Then does not dominate , since the dominators of are and the ancestors of . So some path from to avoids ; followed by the tree path from to , whose vertices are descendants of and hence not , it reaches avoiding . So , which is on the tree path to , is not strictly between and : it is either a descendant of or an ancestor of .
Theorem 1 (semidominator formula). For , , where and .
Proof. Let ; contains , so it is not empty. . An element is the start of the sdom path . For an element , take an sdom path for , then the tree path from to , then the edge . The internal vertices of the first part are , itself is , and the tree path from consists of descendants of , numbered . So this is an sdom path for from .
. Let be a simple sdom path. If , then . If , let be the least index with ; it exists because qualifies. We claim for . Otherwise let be the least of , with (equality is excluded because the path is simple). By Lemma 2 the subpath from to contains a common ancestor of both, with . Then , so or by the choice of . If , with , contradicting the choice of ; if , is an ancestor of and , a contradiction. So is an sdom path for , and . Also because it is an internal vertex, and with . So and .
Theorem 2 (immediate dominator rule). For , let be a vertex of least among the vertices with . Then , and if , while if .
Proof. The set contains , so . We use one claim twice.
Claim. Let be a proper ancestor of and a path from to that avoids . Let be the last vertex of with (it exists because ), and the first vertex after on with (it exists because qualifies). Then .
Proof of the claim. Let be the part of between them, made simple. Suppose some internal has , and take the least such . By Lemma 2 the subpath from to contains a common ancestor of and ; as before , so . Since comes after on , , and because avoids ; an ancestor of numbered above the ancestor of is a proper descendant of (Lemma 0). So , and comes before , against the choice of . So all internal vertices exceed , the part is an sdom path for , and .
Case . Then for every with . If , it dominates . Otherwise suppose a path avoids ; the claim gives such a with , a contradiction. So dominates ; it is a proper ancestor of and, by Lemma 3, a descendant of . As is the lowest strict dominator, .
Case . Here , since . By Lemma 3, , and is an ancestor of numbered below , hence . So . By Lemma 4 applied to , either , which is impossible because (Lemma 3), or .
It remains to show that dominates ; then is a strict dominator of , hence an ancestor of , and the two are equal. If this is clear. Otherwise suppose a path to avoids and take and from the claim, with . If , then is in the set that defines , and (by Lemma 3, ) contradicts the choice of . Otherwise , so is strictly between and on the tree path. Now is an ancestor of (Lemma 3) numbered below , so it is a proper ancestor of . The tree path from to (ancestors of , all numbered below ), an sdom path from to (internal vertices above ), and the tree path from to (descendants of ) form a path from to that avoids , a contradiction.
Lemma 5 (the forest). Consider the moment just before EVAL is called. Every vertex with none has been linked, , every vertex on the tree path from , exclusive, down to has been linked, and is a vertex of least on that path. Moreover of a linked vertex no longer changes. Then EVAL returns if is unlinked, and otherwise a vertex of least on the tree path from the highest unlinked ancestor of , exclusive, down to ; and it preserves the invariant.
Proof. The invariant holds after line L6 links , since and the path from , exclusive, to is ; is final at that point and is not written again. If is unlinked, line L12 returns . Otherwise line L14 collects as long as exists, so it stops at a vertex whose ancestor is unlinked: is the root of the forest tree, and the highest unlinked ancestor of , because all vertices strictly below on the path are linked by the invariant, and is unlinked. Line L15 processes . When it reaches , already holds and is a minimum on the path from , exclusive, to (for this is the invariant for , whose ancestor is ). Combining with , a minimum on the path from , exclusive, to , gives a minimum on the path from , exclusive, to , and becomes . So the invariant holds after the update, and is the required minimum.
Theorem 3. The algorithm terminates and returns for every reachable from .
Proof. Termination. The DFS pushes each vertex once. Each loop of lines L3 to L11 runs over a finite set, and the loop of line L14 climbs strictly upward in (by Lemma 5, ).
Semidominators. We show that when is processed, line L5 sets . At that time the linked vertices are exactly those numbered above , with their final by induction. For a predecessor : is unlinked, , and EVAL returns : these are the elements of . For a predecessor : the ancestors of numbered above are linked, and the highest unlinked ancestor of has , so the tree path from , exclusive, to is exactly the set of ancestors of with , by Lemma 0. EVAL returns one of them with least . These give the elements of , and starts at $w > $ every element of , so line L5 computes by Theorem 1.
Buckets. Vertex enters when it is processed. Let be the child of on the tree path to . The vertices processed after and before are numbered between and , hence lie in the subtree of (Lemma 0), and none of them is a child of . So stays in until is processed, and is handled in line L8 at , after is linked. At that moment the linked vertices are those numbered , so the whole tree path from to is linked and is not, and by Lemma 5 EVAL returns a vertex of least among with , which is the vertex of Theorem 2. If , then and line L8 sets . Otherwise it sets with and .
Final pass. Line L11 changes exactly when , that is, in the second case; there , so in increasing order is already final, and . In the first case and nothing changes. Renaming back to the original labels preserves all answers.