Simple Algorithm
Algorithms & Data Structures · Optimal · 26 min

MST Variants & Applications

Bottleneck paths, second-best trees, Kruskal reconstruction trees and clustering.

A minimum spanning tree is more than a cheap network. The path it gives between any two vertices makes the heaviest edge on the route as light as possible, so one tree answers all n2n^2 bottleneck questions; deleting its k−1k - 1 heaviest edges yields the kk clusters with the widest gaps; and replaying Kruskal's merges as a tree turns "where can I go on edges of weight at most WW?" into a subtree lookup.

What you’ll learn

  • Prove that MST paths are minimum-bottleneck paths, certified by a cut, and that every MST is a minimum bottleneck spanning tree but not conversely.
  • Prove that a second-best spanning tree is one swap away from an MST, compute it in O(n2)O(n^2) or O(mlog⁡n)O(m \log n), and decide which edges lie in some or in every MST.
  • Build a Kruskal reconstruction tree and use it for bottleneck and threshold-reachability queries.
  • Prove that single-linkage clustering maximizes the spacing, and use MSTs for Euclidean networks, Steiner trees and the traveling salesman.

Bottleneck paths

DerivationProof: the heaviest tree edge is certified by a cut
  1. T−e=Tu∪Tv,u∈Tu, v∈TvT - e = T_u \cup T_v,\qquad u \in T_u,\ v \in T_v

    Let ee be the heaviest edge on the TT-path from uu to vv. Deleting ee splits TT into two parts, which define a cut with uu and vv on opposite sides.

  2. w(e)≤w(f)for every edge f crossing (Tu,Tv)w(e) \le w(f)\quad\text{for every edge } f \text{ crossing } (T_u, T_v)

    ee is a lightest edge across that cut, for otherwise swapping it for a lighter crossing edge ff would give a lighter spanning tree T−e+fT - e + f.

  3. b(u,v) ≥ w(e)=bottleneck of the T-path ≥ b(u,v)b(u, v) \ \ge\ w(e) = \text{bottleneck of the } T\text{-path} \ \ge\ b(u, v)

    Every path from uu to vv in GG crosses the cut somewhere, so its bottleneck is at least w(e)w(e), which the tree path attains. The argument never assumed distinct weights.

Predict before you look

In the graph with edges A–B 3, A–C 4, B–C 1, B–D 7, C–D 5, B–E 10, D–E 2, D–F 8, C–F 11, E–G 6 and F–G 9, what is the smallest possible weight of the heaviest edge on a route from A to G?

Show the answer

6

The MST path A–B–C–D–E–G uses weights 3, 1, 5, 2, 6, so the answer is at most 6. It is at least 6 because the edges lighter than 6 (B–C, D–E, A–B, A–C, C–D) connect only A, B, C, D and E: G cannot be reached without an edge of weight 6 or more.

The converse fails. In the triangle A–B 1, B–C 2, A–C 2, the tree {\{B–C, A–C}\} has bottleneck 2, as small as possible, but weight 4, while the MST weighs 3. An MBST can even be found in O(m)O(m) time, faster than any known deterministic MST algorithm, by repeatedly comparing the edge weights with their median and contracting (Camerini, 1978).

Answering queries. For one pair, run Dijkstra's algorithm with max⁡\max in place of ++, as in the shortest-paths lesson, or read the path off an MST. For all pairs, one traversal of the MST from each vertex gives every b(u,v)b(u, v) in O(n2)O(n^2). For many online queries, binary lifting on the MST answers each in O(log⁡n)O(\log n), and the Kruskal reconstruction tree of step 3 does even better.

Second-best trees, and which edges matter

DerivationProof sketch: walk a second-best tree toward T
  1. T−f+eandT′−e+fT - f + e \quad\text{and}\quad T' - e + f

    Take a second-best tree T′T' and an edge e∈T′∖Te \in T' \setminus T. Deleting ee from T′T' defines a cut; the TT-path between ee's ends crosses it with an edge f∈T∖T′f \in T \setminus T'. Both swaps give spanning trees:

  2. w(e)=w(f)⟹w(T′−e+f)=w(T′)w(e) = w(f) \quad\Longrightarrow\quad w(T' - e + f) = w(T')

    TT is minimum, so w(e)≥w(f)w(e) \ge w(f). If T′−e+f=TT' - e + f = T, then T′=T−f+eT' = T - f + e is a single swap. Otherwise T′−e+fT' - e + f is a tree different from TT, and T′T' is second-best, so w(f)≥w(e)w(f) \ge w(e):

  3. w2=w(T)+min⁡e=(u,v)∉T(w(e)−max⁡g∈pathT(u,v)w(g))w_2 = w(T) + \min_{e = (u, v) \notin T}\Big(w(e) - \max_{g \in \text{path}_T(u, v)} w(g)\Big)

    So T′−e+fT' - e + f is another second-best tree, with one more edge in common with TT. Repeating ends at a second-best tree T−f+eT - f + e. For a fixed non-tree edge ee, the cheapest such swap removes the heaviest edge on ee's tree path:

python
def second_best(n, edges):
    """Connected graph, edges = [(w, u, v), ...]. Returns (MST weight, second-best weight).
    Uses the DSU class of the Optimized lesson. O(n^2 + m log m) time, O(n^2) memory."""
    dsu, total, adj, rest = DSU(n), 0, [[] for _ in range(n)], []
    for w, u, v in sorted(edges):
        if dsu.union(u, v):                    # Kruskal: a tree edge
            total += w
            adj[u].append((v, w)); adj[v].append((u, w))
        elif u != v:
            rest.append((w, u, v))             # a non-tree edge
    heaviest = [[float('-inf')] * n for _ in range(n)]   # max weight on the tree path s..x
    for s in range(n):
        stack = [s]
        while stack:
            x = stack.pop()
            for y, w in adj[x]:
                if y != s and heaviest[s][y] == float('-inf'):
                    heaviest[s][y] = max(heaviest[s][x], w)
                    stack.append(y)
    return total, min((total + w - heaviest[u][v] for w, u, v in rest), default=None)

One traversal of the tree from each vertex tabulates every path maximum, which suits dense graphs. For large sparse graphs, binary lifting stores the maximum edge on each jump of 2k2^k steps toward the root and answers each path-maximum query in O(log⁡n)O(\log n): O(mlog⁡n)O(m \log n) in all. If the second-best weight must be strictly larger than w(T)w(T), keep the largest and the second-largest distinct weights on each jump, and use the second whenever the largest equals w(e)w(e).

This classifies all edges in O(mlog⁡m)O(m \log m): process the weight classes in Kruskal's order. Within a class, an edge whose ends are already joined by lighter edges is in no MST. The others live in the multigraph whose vertices are the components formed by the lighter edges; among them, exactly the bridges of that multigraph are in every MST, and the rest are in some but not all. Bridges come from one depth-first search per class (see Bridges, Articulation Points & Biconnectivity); then union the class and move on.

Kruskal reconstruction trees

Run Kruskal's algorithm, but each time an edge of weight ww merges two components, create a new node with value ww whose two children are the trees of those components. The original vertices are the leaves. On a connected graph the result is a binary tree with nn leaves and n−1n - 1 internal nodes: the Kruskal reconstruction tree, a record of the order in which the graph comes together.

python
def kruskal_tree(n, edges):
    """Leaves 0..n-1 are the vertices; each merge by an edge of weight w adds a node of value w.
    Returns up (parent pointers; a root points to itself), value, and leaves (leaf count)."""
    comp = list(range(2 * n))                 # union-find over tree nodes
    def find(x):
        while comp[x] != x:
            comp[x] = comp[comp[x]]
            x = comp[x]
        return x
    up, value, leaves = list(range(2 * n)), [0] * (2 * n), [1] * n + [0] * n
    new = n
    for w, u, v in sorted(edges):
        a, b = find(u), find(v)
        if a == b:
            continue
        value[new], leaves[new] = w, leaves[a] + leaves[b]
        up[a] = up[b] = comp[a] = comp[b] = new   # the new node adopts both components
        new += 1
    return up, value, leaves

def reachable(up, value, leaves, v, W):
    """How many vertices can v reach using only edges of weight <= W?"""
    while up[v] != v and value[up[v]] <= W:     # binary lifting makes this O(log n)
        v = up[v]
    return leaves[v]

The climb is written as a loop for clarity; with binary lifting over up, each query takes O(log⁡n)O(\log n) after O(nlog⁡n)O(n \log n) preprocessing, and lowest common ancestors give bottlenecks in O(log⁡n)O(\log n) or, with an Euler tour and a sparse table, O(1)O(1) (see Lowest Common Ancestors).

Predict before you look

On the graph of step 1 (A–B 3, A–C 4, B–C 1, B–D 7, C–D 5, B–E 10, D–E 2, D–F 8, C–F 11, E–G 6, F–G 9), how many vertices, A included, can A reach using only edges of weight at most 7?

Show the answer

6

The tree's internal nodes have values 1, 2, 3, 5, 6, 8 (B–C, D–E, A–B, C–D, E–G, D–F). From A, climb through 3, 5 and 6 and stop below 8: the node of value 6 covers A, B, C, D, E and G. F needs D–F 8 or more. The edge B–D 7 is allowed but changes nothing: it joins two vertices already connected.

What it is used for. "Starting from vv, which places can you reach on roads with weight limit at least your truck's weight?" (build the tree from a maximum spanning tree, in decreasing order). "Drive on the roads that are still above water, then walk": compute walking distances to the destination with Dijkstra's algorithm, store at each tree node the minimum over its leaves, and every query is one climb. Since the leaves below any node form a contiguous range in a depth-first order, a segment tree or a persistent structure over that order answers "the kk-th largest value among reachable vertices" as well.

Offline, the same questions can be answered by sorting queries by WW and adding edges to a union-find structure as WW grows; the reconstruction tree answers them online, in any order.

Clustering, geometry and approximation

DerivationProof
  1. spacing(C)=d∗\text{spacing}(\mathcal C) = d^*

    Let C\mathcal C be Kruskal's clustering, and d∗d^* the length of the edge Kruskal would add next; that edge joins two different clusters, so C\mathcal C has spacing d∗d^*.

  2. p=x0,x1,…,xt=q,d(xi,xi+1)≤d∗p = x_0, x_1, \dots, x_t = q, \qquad d(x_i, x_{i+1}) \le d^*

    Take any other kk-clustering C′\mathcal C'. Some cluster of C\mathcal C contains two points p,qp, q that C′\mathcal C' separates. Kruskal connected them inside that cluster by a path of edges, each of length at most d∗d^*, since each was added before the edge of length d∗d^*.

  3. spacing(C′) ≤ d(xi,xi+1) ≤ d∗=spacing(C)\text{spacing}(\mathcal C') \ \le\ d(x_i, x_{i+1}) \ \le\ d^* = \text{spacing}(\mathcal C)

    The path starts in one cluster of C′\mathcal C' and ends in another, so some step (xi,xi+1)(x_i, x_{i+1}) joins two different clusters of C′\mathcal C':

Check your understanding

On the graph of step 1, whose MST is B–C 1, D–E 2, A–B 3, C–D 5, E–G 6, D–F 8, you delete the two heaviest MST edges to get three clusters. What is their spacing?

  1. 8
  2. 5
  3. 6
  4. 9
Show the answer

C. 6

Deleting D–F 8 and E–G 6 leaves the clusters {A,B,C,D,E}\{A, B, C, D, E\}, {F}\{F\} and {G}\{G\}. The edges between different clusters are E–G 6, D–F 8, F–G 9 and C–F 11, so the spacing is 6, the weight of the second heaviest MST edge, which is exactly what the theorem predicts.

Geometric MSTs. For nn points in the plane, the Euclidean MST is a subgraph of the Delaunay triangulation, which has at most 3n−63n - 6 edges and can be built in O(nlog⁡n)O(n \log n), so the EMST takes O(nlog⁡n)O(n \log n) time instead of Θ(n2)\Theta(n^2) (Shamos & Hoey, 1975). That is optimal in the algebraic decision-tree model, because the EMST's shortest edge is the closest pair. In contests the Manhattan-distance MST uses the same idea: each point only needs an edge to its nearest neighbor in each of 8 octants, which leaves O(n)O(n) candidate edges.

The same doubling argument makes a doubled MST, shortcut along an Euler tour, a 2-approximation for the metric traveling salesman problem; Christofides and Serdyukov add a minimum-weight matching on the odd-degree vertices to reach 3/23/2. The lesson Approximating the Traveling Salesman develops both, and the 2021 improvement below 3/23/2.

Key takeaways

  • In any MST, the tree path between two vertices minimizes the heaviest edge, and a cut certifies it; every MST is a minimum bottleneck spanning tree, but not conversely.
  • A second-best spanning tree is T+e−fT + e - f for one non-tree edge ee and the heaviest edge ff on its tree path: O(n2)O(n^2) with one traversal per vertex, O(mlog⁡n)O(m \log n) with binary lifting. An edge is in some MST iff lighter edges do not connect its ends, and in every MST iff it is a bridge of its weight class after contracting the lighter edges.
  • The Kruskal reconstruction tree has 2n−12n - 1 nodes; the value at LCA(u,v)\mathrm{LCA}(u, v) is the bottleneck b(u,v)b(u, v), and the vertices reachable from vv with edges of weight ≤W\le W are the leaves below one ancestor.
  • Stopping Kruskal at kk components gives the kk-clustering of maximum spacing; planar Euclidean MSTs take O(nlog⁡n)O(n \log n) through the Delaunay triangulation; an MST of the terminals' metric closure is within 2(1−1/∣R∣)2(1 - 1/\abs{R}) of an optimal Steiner tree.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Every MST is a minimum bottleneck spanning tree. Is every minimum bottleneck spanning tree an MST?

  1. Yes: a tree whose heaviest edge is minimum must have minimum total weight
  2. Only when the graph is complete
  3. Yes, if all weights are distinct
  4. No: in the triangle A–B 1, B–C 2, A–C 2, the tree A–C, B–C has the minimum bottleneck 2 but weight 4, more than the MST's 3
Show the answer

D. No: in the triangle A–B 1, B–C 2, A–C 2, the tree A–C, B–C has the minimum bottleneck 2 but weight 4, more than the MST's 3

The bottleneck only constrains the heaviest edge; the lighter edges of an MBST can be wasteful. That freedom is why an MBST can be found in linear time while finding an MST deterministically in linear time is open.

Question 2

Edge e=(u,v)e = (u, v) has weight 5. The edges of weight less than 5 do not connect uu and vv, but after deleting ee, another path of edges of weight at most 5 still does. Then ee is...

  1. in some minimum spanning trees but not in all of them
  2. in every minimum spanning tree
  3. in no minimum spanning tree
  4. in the MST only if it is a bridge of the whole graph
Show the answer

A. in some minimum spanning trees but not in all of them

No strictly lighter path joins its ends, so ee is a lightest edge across some cut and can be in an MST. But another path of weight-≤5\le 5 edges exists, so some edge of weight exactly 5 on it can replace ee: not every MST needs it.

Question 3

In a Kruskal reconstruction tree, what is the value stored at the lowest common ancestor of two leaves uu and vv?

  1. The shortest-path distance between uu and vv
  2. The minimum bottleneck b(u,v)b(u, v): the least possible weight of the heaviest edge on a uu–vv path
  3. The weight of the MST path between uu and vv
  4. The number of edges on the MST path between uu and vv
Show the answer

B. The minimum bottleneck b(u,v)b(u, v): the least possible weight of the heaviest edge on a uu–vv path

That node was created by the edge that first connected uu's and vv's components during Kruskal's scan, and every lighter edge had already been tried, so it is the smallest possible heaviest edge, b(u,v)b(u, v).

Question 4

You have an MST TT and add a non-tree edge e=(u,v)e = (u, v). To get the cheapest spanning tree that contains ee, which edge do you remove?

  1. The heaviest edge of the whole tree TT
  2. The lightest edge on the TT-path from uu to vv
  3. Any edge on the cycle; they all give the same weight
  4. The heaviest edge on the TT-path from uu to vv
Show the answer

D. The heaviest edge on the TT-path from uu to vv

Adding ee creates exactly one cycle, ee plus the tree path; removing any edge of that path restores a spanning tree, and removing the heaviest one keeps the total smallest. Minimizing over all non-tree edges gives a second-best tree.

Further reading

  • J. C. Gower & G. J. S. Ross, "Minimum spanning trees and single linkage cluster analysis," Journal of the Royal Statistical Society, Series C (Applied Statistics) 18(1) (1969), 54–64.
  • M. I. Shamos & D. Hoey, "Closest-point problems," Proceedings of the 16th IEEE Symposium on Foundations of Computer Science (1975).
  • L. Kou, G. Markowsky & L. Berman, "A fast algorithm for Steiner trees," Acta Informatica 15 (1981), 141–145.
  • J. Kleinberg & É. Tardos, Algorithm Design (Addison-Wesley, 2005), section 4.7.
  • T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 21 and its problems.