Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
One swap away from the minimum spanning tree
The cheapest spanning tree other than the minimum one: add each edge outside the tree and drop the heaviest edge of the cycle it closes
Select an algorithm and generate steps to begin visualization
The second-best minimum spanning tree is the cheapest spanning tree that differs from a given minimum spanning tree. It answers questions such as what happens to a network design if one chosen link becomes unavailable, and whether the minimum spanning tree is unique. A classic result makes it easy: the second-best tree always differs from the minimum one by a single swap, one edge added from outside and one tree edge removed. Adding an edge outside the tree closes exactly one cycle, and removing the heaviest tree edge on that cycle keeps a spanning tree at the smallest extra cost. Trying every outside edge and keeping the cheapest swap gives the answer. If some swap costs nothing, the minimum spanning tree is not unique.
First Kruskal's algorithm builds the minimum spanning tree. Then each edge outside the tree is tried in turn: the visualizer walks the tree path between its ends, which together with the edge forms a cycle, finds the heaviest tree edge on that path and writes the difference, the weight the swap would add. The cheapest difference wins. On the example from Kruskal's own page, 7 vertices and 11 edges, the tree weighs 39. Adding B–C (8) and dropping B–E (7) costs +1, as does adding E–F (8) and dropping B–E. Adding D–E (15) would cost +8. So the second-best tree weighs 40, and because two swaps tie, it is not unique, though its weight is. The tree path is found by walking up from both ends here, O(V) per edge, while binary lifting answers each path maximum in O(log V).
Network designers keep a backup plan: if a link of the cheapest network is closed or too expensive, the second-best tree says how much the next design costs and which link replaces it. The same swap computation tells how sensitive a minimum spanning tree is, how much each edge weight may change before the tree changes, which is used in sensitivity analysis of cable, pipe and road layouts. A difference of zero proves the minimum spanning tree is not unique, which matters when ties should be broken on purpose. The problem is a staple of programming contests, as a direct question or as a step in harder tree problems, and the path maximum query it needs leads naturally to binary lifting and heavy-light decomposition.
Build the minimum spanning tree, then try every single swap.
SecondBestMST(G):
T = Kruskal(G) // minimum spanning tree, weight W
best = infinity
for each edge (u, v) of G not in T:
m = the heaviest edge on the tree path from u to v
best = min(best, w(u, v) - w(m))
return W + best // add (u, v), remove mAdding an edge outside the tree closes exactly one cycle, and the edge to remove must lie on it, or the result is not a tree. Removing the heaviest one costs the least. The path maximum is the only costly part: walking up the tree is O(V) per edge, binary lifting or heavy-light decomposition make it O(log V).
The example from the Kruskal page.
Example graph: A–B 7, A–D 5, B–C 8, B–D 9, B–E 7, C–E 5, D–E 15, D–F 6, E–F 8, E–G 9, F–G 11. 7 vertices, 11 edges.
Second-best weight 40 = 39 + 1: add B–C and remove B–E. Adding E–F and removing B–E ties at +1, so there are two second-best trees.
Time: O(E log E + V·E) as shown, O(E log V) with binary lifting · Space: O(V + E)
Kruskal sorts the edges in O(E log E). Each of the E − V + 1 edges outside the tree then needs the maximum edge on a tree path. Walking up from both ends costs O(V), so O(V·E) in all. Preprocessing the tree with binary lifting, where each vertex stores its 2^i-th ancestor and the heaviest edge on the way, answers each path maximum in O(log V), for O(E log V) in total.
Related spanning tree questions.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Kruskal or Prim alone | Only the minimum tree is needed. | O(E log V) |
| Recompute without one edge | One specific edge is in question, such as a link that may fail. Run Kruskal again with that edge removed. | O(E log E) per edge |
| Enumerate all spanning trees | The graph is tiny and every tree is wanted, for teaching or testing. The matrix-tree theorem counts them first. | up to n^(n − 2) trees |
Related algorithms: Kruskal's MST Algorithm, Prim's MST Algorithm, Borůvka's Algorithm, Lowest Common Ancestor (Binary Lifting), Heavy-Light Decomposition
Built by Mohammed Islam Hadjoudj · Work with me