Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
The minimum spanning tree, with one-way streets
Finds the cheapest arcs that reach every vertex from a root: each vertex takes its cheapest incoming arc, and any cycle this makes is contracted and solved again
Select an algorithm and generate steps to begin visualization
An arborescence is a spanning tree with directions: one root, and from it exactly one directed path to every other vertex, so every vertex except the root has exactly one incoming arc. The minimum arborescence is the cheapest such tree, the directed counterpart of a minimum spanning tree. Prim and Kruskal fail here: in a directed graph the cheapest arcs may point the wrong way, and a greedy choice can leave a vertex that the root cannot reach. Yoeng-Jin Chu and Tseng-Hong Liu (1965) and, independently, Jack Edmonds (1967) found the algorithm.
Every vertex other than the root picks its cheapest incoming arc. If the picks contain no cycle, they already form the answer, since no tree can do better than the cheapest arc into each vertex. If they do contain a cycle, the cycle is merged into a single vertex. An arc x → v entering the cycle would replace v's cycle arc, so its cost is lowered by the cost of that arc. The algorithm runs again on the smaller graph, and when the cycle is expanded the arc chosen to enter it decides which cycle arc to drop. This gives O(n·m) time, and Tarjan's version with mergeable heaps runs in O(m log n). The visualizer draws the picks in green, a cycle in magenta, colours each merged group, lists the adjusted costs and ends with the arborescence and its total.
Dependency parsers in natural language processing score every possible head for every word and take the maximum arborescence as the sentence's structure, which is how non-projective parsers find crossing links. The same problem appears when one sender must reach everyone in a network of one-way links at least cost, in phylogenetics when choosing which variant descends from which, and as a building block in directed network design. If a vertex cannot be reached from the root at all, no arborescence exists and the run reports it.
A recursive description: pick, look for a cycle, contract, solve the smaller problem, expand.
ChuLiuEdmonds(G, r):
for each vertex v != r:
in(v) = cheapest arc entering v // none: no arborescence
if the arcs in(v) form no cycle:
return them // already optimal
C = a cycle among them
contract C into one vertex c
for each arc x → v with v in C and x not in C:
cost(x → c) = cost(x → v) - cost(in(v))
A = ChuLiuEdmonds(contracted G, r)
expand c: keep A and the arcs of C,
except in(v) for the vertex v that A enters
return the resultThe adjusted cost is what entering the cycle at v really costs: the arc x → v comes in, and v no longer needs its cycle arc. There is always a best arborescence that keeps all of the cycle but one arc, so it enters the cycle exactly once. Arcs between two vertices of the cycle disappear in the contraction.
The six-vertex example on this page, rooted at A.
Example graph: Arcs A → B (9), A → C (8), A → E (10), B → C (2), C → B (3), B → D (6), C → D (4), D → E (3), E → D (1), D → F (5), E → F (2).
The minimum arborescence is A → B (9), B → C (2), C → D (4), D → E (3), E → F (2), total 20. Taking the tied A → C instead gives A → C, C → B and the same total 20, so this example has two optimal answers.
Time: O(n·m) · Space: O(n + m)
Each round picks the cheapest incoming arcs and looks for a cycle in O(m). Each contraction removes at least one vertex, so there are at most n − 1 rounds, O(n·m) in all. Tarjan (1977) kept the incoming arcs in mergeable heaps and reached O(m log n), and Gabow, Galil, Spencer and Tarjan (1986) reached O(m + n log n).
Related tree problems and when each one applies.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Prim or Kruskal | The graph is undirected. Then any vertex can be the root, and a minimum spanning tree is the answer. | O(m log n) |
| Shortest path tree (Dijkstra) | Each vertex must be reached by its cheapest path from the root, rather than the whole tree being as cheap as possible. The two trees differ in general. | O(m + n log n) |
| Tarjan, or Gabow et al. | Large graphs. The same answer, with mergeable heaps instead of rescanning every arc each round. | O(m log n) or O(m + n log n) |
Read the full article: Minimum Spanning Trees: Prim, Kruskal and Boruvka
Read the full article: Directed vs Undirected Graphs Explained
Related algorithms: Prim's MST Algorithm, Kruskal's MST Algorithm, Borůvka's Algorithm, Steiner Tree, Topological Sort