Purpose: Compute the dominator tree of a flow graph in (or with the simple version) — the standard algorithm in every optimising compiler.
Dominators
In a directed graph with entry , vertex dominates if every path passes through . The immediate dominator is the closest such . The idom relation forms a tree rooted at — the dominator tree.
Algorithm
- DFS from , numbering vertices in preorder as . Record the DFS tree and
parent[]. - Semidominators. Define
In words: the highest-numbered vertex from which you can reach through vertices all numbered higher than . Compute these in reverse DFS order using a link-eval structure (a DSU with path compression that also tracks the minimumsdomalong the compressed path). - Implicit idom. Bucket each under . When processing , for each in its bucket, find — the vertex on the path with minimum sdom. Then
- Final pass in DFS order fixes the deferred entries: .
Complexity
| Version | Time |
|---|---|
| Simple (path compression only) | |
| Sophisticated (union by size + path compression) | |
| Iterative dataflow (Cooper-Harvey-Kennedy) | worst, near-linear in practice |
| Alstrup et al. / Buchsbaum et al. |
Space is throughout.
Why the semidominator detour
Computing idom directly is hard because the definition quantifies over all paths. The semidominator is a relaxation that only looks at paths through higher-numbered vertices, which makes it computable by a single reverse-order sweep. The two-case rule in step 3 then converts sdom into idom exactly. This indirection is the whole trick, and the reason the algorithm looks mysterious the first time.
In a contest
Dominator trees appear rarely, but when they do the problem is usually “which vertices become unreachable if is removed” or “count vertices only reachable through ” — both are subtree questions on the dominator tree. The Cooper-Harvey-Kennedy iterative algorithm is ~30 lines and fast enough for contest sizes; keep Lengauer-Tarjan for when is large.
Variants / Use Cases
- Compiler SSA construction — dominance frontiers come straight from the dominator tree; this is the original and still main use
- Dominator Tree — the topic page
- Reachability under vertex deletion — “how many vertices become unreachable if fails”
- Post-dominators — run the same algorithm on the reversed graph from the exit node
- Control dependence — the combination of dominators and post-dominators
- DSU with path compression — the link-eval structure is a DSU variant carrying an aggregate