learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Second-Best MST Visualizer

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

Time: O(V·E) as shown, O(E log V) with binary lifting
Space: O(V + E)
Use Case: Backup network designs, how an MST reacts when an edge fails, checking whether the MST is unique, contest problems
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Second-Best Minimum Spanning Tree

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.

How it works

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).

Applications

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.

Pseudocode

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 m

Adding 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).

Worked example, step by step

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.

  1. Kruskal. The tree takes A–D 5, C–E 5, D–F 6, A–B 7, B–E 7 and E–G 9, for a weight of 39. The other 5 edges are the candidates.
  2. Add B–C (8). Its tree path is B, E, C. The heaviest edge on it is B–E (7), so the swap costs 8 − 7 = +1.
  3. Add B–D (9) and D–E (15). B–D closes the path B, A, D, heaviest A–B (7): +2. D–E closes D, A, B, E, heaviest 7: +8.
  4. Add E–F (8) and F–G (11). E–F closes E, B, A, D, F, heaviest B–E (7): +1. F–G closes F, D, A, B, E, G, heaviest E–G (9): +2.

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.

Complexity, and where it comes from

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.

When to use Second-Best Minimum Spanning Tree, and when not to

Related spanning tree questions.

AlternativePrefer it whenCost
Kruskal or Prim aloneOnly the minimum tree is needed.O(E log V)
Recompute without one edgeOne 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 treesThe 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

Common pitfalls

  • Removing the heaviest tree edge overall. The edge to remove must lie on the cycle closed by the new edge. B–E is removed for B–C because it is on the path B, E, C, not because it is heavy in general.
  • Missing a zero-cost swap. If an outside edge weighs the same as the heaviest edge on its path, the swap costs 0 and the second-best tree weighs as much as the minimum. When a strictly larger weight is required, also keep the largest edge strictly lighter than the new one on each path.
  • Expecting a unique tree. Ties are common: on this graph B–C and E–F both cost +1. The weight of the second-best tree is unique, the tree itself need not be.

Frequently asked questions

Why is one swap enough?
An exchange argument shows that among the spanning trees other than the minimum one, a cheapest one always shares all but one edge with it. So trying every single swap finds the second-best weight.
How does it test whether the MST is unique?
The minimum spanning tree is unique exactly when every swap costs more than 0. Compute the second-best weight: if it equals the minimum, there are several minimum trees.
What is binary lifting here?
Each vertex stores its ancestors at distances 1, 2, 4, 8 and so on, with the heaviest edge on each jump. The maximum on the path from u to v is found while lifting u and v to their lowest common ancestor, in O(log V) jumps.

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 HadjoudjWork with me

An example graph: you can change it, or start blank.

Zoom Controls

40%
Nodes: 7
Edges: 11