Sub topic 13
When Is the MST Unique?
This chapter assumes you already know the cut property and the cycle property, Kruskal's algorithm, and the DisjointSet class from the union-find chapter. Here we ask a narrower question. When does a weighted, connected, undirected graph have exactly one minimum spanning tree, and how can you check that in code?
This question comes up in practice. If you write tests that compare the edge set from Prim's algorithm with the edge set from Kruskal's algorithm, the tests pass on some graphs and fail on others, even though both implementations are correct. The cause is almost always equal edge weights.
Distinct Weights Guarantee a Unique MST
The main result is short: if all edge weights are distinct, the MST is unique.
The exchange proof
Suppose two different MSTs exist, T1 and T2, with the same total weight.
- Look at the edges that belong to exactly one of the two trees (the symmetric difference). This set is not empty because the trees differ.
- Let e be the lightest edge in that set. Without loss of generality, e is in T1 and not in T2.
- Adding e to T2 creates exactly one cycle. That cycle must contain some edge f that is not in T1, because T1 contains no cycles and so cannot hold the whole cycle.
- Edge f is in T2 but not in T1, so f is in the symmetric difference. Edge e is the lightest edge there and all weights are distinct, so w(f) > w(e).
- The tree T2 + e − f is a spanning tree that weighs less than T2. That contradicts T2 being minimum.
So two different MSTs cannot exist. The proof uses distinctness at only one point, step 4, where it turns "w(f) ≥ w(e)" into a strict inequality. When ties are allowed, step 4 can produce w(f) = w(e), and the swap T2 + e − f gives a second MST of equal weight instead of a contradiction.
The converse is false
Distinct weights are sufficient for uniqueness. They are not necessary. Two quick counterexamples:
- If the graph is already a tree, it has exactly one spanning tree. That tree is the MST even if every edge weight is 7.
- Take a triangle with weights 1, 1, 2. Every MST must drop the weight-2 edge, so the only MST is the two weight-1 edges, and it is unique despite the tie.
Equal weights only break uniqueness when the tied edges compete with each other for the same place in the tree.
The Exact Condition
The cycle property tells you which ties matter. Let T be any MST. For each non-tree edge e = (u, v), adding e to T closes a cycle made of e and the tree path from u to v.
The "if" direction is the swap argument you just saw. For the other direction, suppose every non-tree edge is strictly the heaviest edge on its cycle. Then the cycle property excludes every non-tree edge from every MST. All MSTs must therefore be subsets of T. A spanning tree has exactly V − 1 edges, so every MST equals T.
A dual statement uses cuts. T is unique if and only if, for every tree edge f, removing f splits T into two parts, and f is the unique lightest edge crossing that cut.
The triangle that has two MSTs
Pick T = {A–B, B–C}. The non-tree edge A–C closes the cycle A–B–C–A. The heaviest tree edge on that cycle is B–C with weight 2, which equals the weight of A–C. The condition fails, and swapping gives the second MST {A–B, A–C}.
What All MSTs Share
Even when a graph has many MSTs, they agree on several things:
| Property | Same across all MSTs? |
|---|---|
| Total weight | Yes, by definition |
| Sorted list of edge weights (the multiset) | Yes |
| Heaviest edge weight (the bottleneck) | Yes |
| Number of edges of each weight | Yes |
| The actual edge set | Only if the MST is unique |
The multiset claim follows from Kruskal's algorithm. After Kruskal finishes processing all edges of weight at most w, the connected components are fixed by the graph alone, no matter how ties were ordered. The number of edges of weight w that any MST uses equals the drop in component count during that weight class.
The number of distinct MSTs can be very large. In a complete graph on n vertices with all weights equal, every spanning tree is an MST, and Cayley's formula gives n^(n−2) of them. For n = 10 that is 100 million trees.
Checking Uniqueness with a Modified Kruskal
You could find an MST and then test every non-tree edge against the heaviest edge on its tree path. That works, but it needs path-maximum queries. A simpler method runs Kruskal's algorithm one weight class at a time.
For each group of edges that share a weight w:
- Count candidates. Before adding anything from the group, count the edges whose endpoints are in different components. Each of these edges could appear in some MST.
- Add edges. Run the usual union step over the group and count how many edges were actually added.
- Compare. If candidates > added, some candidate edge was rejected only because an earlier edge from the same group had already connected its endpoints. That rejected edge ties the heaviest edge on its cycle, so a second MST exists.
class DisjointSet:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, x):
while self.parent[x] != x:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x
def union(self, a, b):
ra, rb = self.find(a), self.find(b)
if ra == rb:
return False
if self.rank[ra] < self.rank[rb]:
ra, rb = rb, ra
self.parent[rb] = ra
if self.rank[ra] == self.rank[rb]:
self.rank[ra] += 1
return True
def mst_is_unique(n, edges):
# edges: list of (weight, u, v); graph assumed connected
edges = sorted(edges)
ds = DisjointSet(n)
total = 0
unique = True
i = 0
while i < len(edges):
j = i
while j < len(edges) and edges[j][0] == edges[i][0]:
j += 1
group = edges[i:j]
# Phase 1: edges that could join two components right now.
candidates = sum(1 for _, u, v in group if ds.find(u) != ds.find(v))
# Phase 2: edges actually added by Kruskal.
added = 0
for w, u, v in group:
if ds.union(u, v):
added += 1
total += w
if candidates > added:
unique = False
i = j
return total, unique
print(mst_is_unique(4, [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]))
# (6, True) distinct weights
print(mst_is_unique(3, [(1, 0, 1), (2, 1, 2), (2, 0, 2)]))
# (3, False) the triangle from the diagram
print(mst_is_unique(3, [(1, 0, 1), (1, 1, 2), (2, 0, 2)]))
# (2, True) a tie, but the tied edges do not compete
print(mst_is_unique(4, [(1, 0, 1), (1, 1, 2), (1, 2, 3), (1, 3, 0)]))
# (3, False) a 4-cycle with equal weights has four MSTsThe check costs the same as Kruskal: O(E log E) for the sort, plus nearly constant time per union-find operation. Phase 1 must finish before Phase 2 begins. If you interleave them, an edge added earlier in the group hides the conflict, and the function reports a unique MST when there are actually several.
Practical Consequences and Gotchas
Prim and Kruskal can disagree
With ties, Prim's algorithm and Kruskal's algorithm can each return a different valid MST. Even two runs of Kruskal can differ if the sort order of tied edges changes, for example when you sort (weight, u, v) tuples and then renumber vertices. Write tests that compare total weight, or the sorted list of weights. Compare edge sets only on graphs you know have a unique MST.
Forcing determinism with tie-breaking
If you need one canonical MST, make the weights distinct in a controlled way. Compare edges by the key (weight, edge_index) instead of by weight alone. Every key is now distinct, so the MST under that ordering is unique. It is also an MST under the original weights, because the tie-break never puts a heavier edge ahead of a lighter one. Python's tuple sort already does a version of this when you sort (w, u, v): ties on w are broken by u and then v.
Floating-point weights
Equality checks on floats are risky. Weights such as distances computed with math.dist can be mathematically equal but differ in the last bit. A uniqueness check then treats a genuine tie as distinct and reports True. When ties matter, round the weights to a fixed precision or convert them to integers (for example, millimetres instead of metres) before grouping.
Disconnected graphs
The same reasoning applies to each component of a minimum spanning forest. The forest is unique if and only if each component's MST is unique. The mst_is_unique function above already handles this case, because it never assumes the forest ends up with exactly V − 1 edges.
Exercises
Exercise
A connected graph has vertices 0–3 and edges (0,1,w=2), (1,2,w=2), (2,3,w=2), (0,2,w=3), (1,3,w=3). Is its MST unique? Justify using the cycle condition.
Show answer
Yes. The three weight-2 edges form the path 0–1–2–3, which is a spanning tree, so they make up the MST with total 6. Non-tree edge (0,2) closes the cycle 0–1–2 whose tree edges both weigh 2, which is less than 3. Non-tree edge (1,3) closes 1–2–3 with tree edges weighing 2, also less than 3. Every non-tree edge is strictly heavier than all tree edges on its cycle, so the MST is unique even though weights repeat.
Exercise
Someone rewrites mstisunique so that, inside each weight group, it checks ds.find(u) != ds.find(v) and calls ds.union(u, v) in the same loop, counting a candidate only when find shows different roots. What does it return for the triangle [(1,0,1),(2,1,2),(2,0,2)], and why is that wrong?
Show answer
It returns (3, True). After edge (1,2) is added, vertices 0 and 2 are already connected, so the merged loop never counts (0,2) as a candidate. Candidates and added are then both 1. The correct answer is False, because {0-1, 0-2} is a second MST. Candidates must be counted against the components as they stood before any edge of the group was added.
Exercise
Explain why every MST of a graph has the same heaviest edge weight, even when there are many MSTs.
Show answer
All MSTs share the same multiset of edge weights. After Kruskal processes every edge of weight at most w, the components depend only on the graph, not on how ties were ordered, so each MST uses the same number of edges of each weight. In particular, the largest weight used is the same for all of them.