Sub topic 13
When Is the MST Unique?
This chapter assumes you know the cut and cycle properties, Kruskal's algorithm, and the disjoint-set structure from earlier chapters. You only need the idea of swapping one edge for another in a spanning tree.
A graph can have several minimum spanning trees. Each one has the same smallest total weight but uses a different set of edges. Uniqueness matters when you compare the output of two implementations, write tests, or want a deterministic answer. If the MST is not unique, Prim and Kruskal can both be correct and still return different trees.
Distinct Weights Guarantee a Unique MST
If every edge weight is different, the graph has exactly one MST.
Here is the proof. Suppose T1 and T2 are two different MSTs. Let e be the lightest edge that appears in exactly one of them, say T1. Adding e to T2 creates a cycle. That cycle must contain some edge f that is not in T1, because otherwise T1 would contain the whole cycle. Edge f lies in T2 but not T1, and e is the lightest such edge, so weight(f) > weight(e). Then T2 + e − f is a spanning tree lighter than T2. That contradicts T2 being minimum.
Ties Do Not Always Break Uniqueness
The converse is false. Equal weights allow multiple MSTs, but they do not force them.
| Graph (edges as u–v:w) | Tied weights? | Number of MSTs |
|---|---|---|
| 0–1:1, 1–2:1, 0–2:1 | yes | 3 |
| 0–1:1, 1–2:1, 0–2:3 | yes | 1 |
| 0–1:1, 1–2:2, 0–2:3 | no | 1 |
In the second row, the two tied edges are both in the tree. Swapping either one for the edge 0–2 makes the tree heavier, so nothing can replace them.
The Exact Test
Take any MST T. T is the only MST if and only if every non-tree edge (u, v, w) is strictly heavier than every edge on the tree path between u and v.
If some tree edge on that path has weight equal to w, you can swap the two edges and get a different tree with the same total. This is the cycle property with "strictly" in place of "at least".
Checking Uniqueness in Code
A simple brute-force check removes each MST edge in turn and recomputes the MST. If any recomputation reaches the same total weight, a second MST exists. This takes O(V · E log E) time, which is fine for tests. A faster version computes path maxima over the tree.
def mst_weight(n, edges, skip=None):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
total, used = 0, []
for i, (w, u, v) in sorted(enumerate(edges), key=lambda p: p[1]):
if i == skip:
continue
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv
total += w
used.append(i)
if len(used) != n - 1:
return None, used # graph is disconnected without the skipped edge
return total, used
def mst_is_unique(n, edges):
best, used = mst_weight(n, edges)
for i in used:
alt, _ = mst_weight(n, edges, skip=i)
if alt == best:
return False
return True
triangle = [(1, 0, 1), (1, 1, 2), (1, 0, 2)]
tied_but_unique = [(1, 0, 1), (1, 1, 2), (3, 0, 2)]
print(mst_is_unique(3, triangle)) # False
print(mst_is_unique(3, tied_but_unique)) # TrueExercise
Take the four-vertex graph from earlier chapters, edges (w, u, v) = (1,0,1), (4,0,2), (3,1,2), (2,1,3), (5,2,3), and change the weight of edge 0–2 from 4 to 3. Is the MST still unique? Justify your answer with the exact test.
Show answer
No. One MST is {0–1:1, 1–3:2, 1–2:3} with total 6. The non-tree edge 0–2 now has weight 3. The tree path from 0 to 2 is 0–1–2, and its heaviest edge (1–2) also weighs 3. Swapping 1–2 for 0–2 gives {0–1, 1–3, 0–2}, a second MST of weight 6.
Exercise
A graph has all distinct weights except two edges that both weigh 7. Both are in the MST that Kruskal returns. Can you conclude the MST is unique?
Show answer
Not from the tie alone, but the tie is harmless here. Since both tied edges are in the tree and every other weight is distinct, no non-tree edge can equal a tree-path maximum. That would require a non-tree edge of weight 7, and none exists. So the MST is unique.