Proofs: Dominator tree
These are the human proofs of Dominator tree, at full length.
The dominators of a vertex are its ancestors in the dominator tree full
This proof shows: correctness, termination.
Let be a directed graph with vertices and start vertex , and let be the set of vertices reachable from . For , dominates if every path from to contains . All paths below start at . We assume that line L1 returns the correct immediate dominators; that is proved on the page of the Lengauer-Tarjan algorithm.
Lemma 1 (partial order). Dominance on is reflexive, transitive and antisymmetric.
Proof. Reflexive: every path to ends at . Transitive: let dominate and dominate . A path to contains ; its prefix up to the first occurrence of is a path to , so it contains , and so does . Antisymmetric: let and suppose both dominate each other. Take a simple path to (one exists since ). It contains , so occurs on before its last vertex . The prefix of up to is a path to ; it must contain , but occurs on the simple path only at the end, after . Contradiction.
Lemma 2 (chain). Let , , and let be the set of strict dominators of . Then is not empty, any two elements of are comparable under dominance, and has a unique element that is dominated by every element of .
Proof. . Fix a simple path from to . Every element of occurs on , exactly once. Let with before on . Suppose does not dominate : there is a path to avoiding . Then followed by the part of after is a path to ; that part of does not contain , since occurs on only before . So we have a path to avoiding , a contradiction with . Hence dominates . So the elements of are totally ordered by dominance, in the order in which they occur on , and the last of them on is dominated by all others; by antisymmetry it is unique.
Lemma 3 (recursive form). For , the set of dominators of is , where is the set of dominators of , and .
Proof. If dominates , then dominates by transitivity, since dominates . Conversely, a dominator of is in and, by Lemma 2, dominates . Finally does not dominate , by antisymmetry, since dominates .
Lemma 4 (the tree). The edges for form a tree on rooted at , and for every , is the set of ancestors of in that tree.
Proof. By Lemma 3, . So following idom from strictly decreases the size of the dominator set, which is at least 1, and the walk ends after finitely many steps at the only vertex without an idom, . There is no cycle, every vertex other than has one parent, so the edges form a tree rooted at . By induction on : (if dominates , the path consisting of alone contains ), which is the set of ancestors of ; and for , Lemma 3 gives the ancestors of , which are the ancestors of .
Lemma 5 (the walk). Lines L3 to L7 terminate, and afterwards, for , is an ancestor of in the tree if and only if and .
Proof. Each tree vertex is pushed with phase enter once, by its parent (or initially, for ), and once with phase exit, so the loop runs times. The stack discipline makes this a preorder walk: when is popped, the entries for the subtree of are pushed above , so all of them are popped before it. So the vertices of the subtree of get the consecutive entry times , where is the subtree size, and . If is an ancestor of , 's entry time lies in 's range and 's range is inside 's, which gives both inequalities. If not, the subtrees of and are disjoint or is a proper ancestor of ; in the first case their ranges are disjoint, so the two inequalities cannot both hold, and in the second .
Theorem. DOMINATES returns true exactly when dominates , DOMINATORS returns exactly the dominators of , and all operations terminate.
Proof. If or is not in , does not dominate under our convention, and line L8 returns false. Otherwise, by Lemma 4, dominates exactly when is an ancestor of , and by Lemma 5 that is the test of line L9. Line L10 follows idom from to , which terminates by Lemma 4 and lists the ancestors of , that is, . For the implementation returns the empty list.