Sub topic 5
The Cycle Property: Which Edges Can Never Be in an MST
The cut property from the previous chapter tells you which edges are safe to add to a minimum spanning tree. The cycle property does the opposite job. It tells you which edges you can throw away without losing anything. Together the two properties explain every decision Kruskal's and Prim's algorithms make. They also give you a direct test for whether a given edge belongs to some MST, to every MST, or to none.
This chapter assumes you know graph basics (vertices, edges, paths, cycles), can read edge lists and adjacency lists in Python, and have seen the MST definition and the cut property in earlier chapters. All graphs here are weighted, undirected, and connected.
Statement of the Cycle Property
Let C be any cycle in the graph G. Let e be an edge on C whose weight is strictly greater than the weight of every other edge on C. Then e does not belong to any minimum spanning tree of G.
A weaker version holds when weights tie. If e is a maximum-weight edge on some cycle, with ties allowed, then at least one MST excludes e. Some other MST may still include it.
Three details of the statement cause most of the mistakes people make with it:
- One cycle is enough. The edge has to be the strict maximum on just one cycle. It does not have to be the heaviest edge on every cycle it lies on.
- "Heaviest on a cycle" is a different condition from "heaviest in the graph." The heaviest edge in the whole graph can be in every MST. This happens when the edge is a bridge, meaning it lies on no cycle at all.
- Strict versus non-strict. "Never in any MST" requires a strict maximum. With ties, all you can say is "not needed in at least one MST."
A Worked Example
Here is the same four-vertex graph used throughout this tutorial.
Look at the cycles:
| Cycle | Edge weights | Strict maximum | Conclusion |
|---|---|---|---|
| 0–1–2–0 | 1, 3, 4 | edge 0–2 (4) | 0–2 is in no MST |
| 1–2–3–1 | 3, 5, 2 | edge 2–3 (5) | 2–3 is in no MST |
| 0–1–3–2–0 | 1, 2, 5, 4 | edge 2–3 (5) | confirms 2–3 is in no MST |
That rules out two of the five edges. The graph has 4 vertices, so a spanning tree needs 3 edges, and exactly 3 remain: 0–1, 1–3, and 1–2. Their total weight is 1 + 2 + 3 = 6, which matches what Kruskal's and Prim's algorithms return in later chapters. Here you got the answer by deleting edges, without adding any.
Why It Is True: The Exchange Argument
The proof is a contradiction argument built on an edge swap. Suppose some MST T contains e = (u, v), and e is the strict maximum on cycle C.
- Remove e from T. A tree minus one edge splits into exactly two components. Call the vertex sets S (containing u) and V \ S (containing v).
- The cycle C starts at u, crosses to v through e, and must come back to u. A closed walk crosses any cut an even number of times, so C contains at least one more edge f that crosses between S and V \ S.
- Adding f to T − e reconnects the two components, so T' = T − e + f is a spanning tree.
- Edge e is the strict maximum on C, so w(f) < w(e). That gives w(T') = w(T) − w(e) + w(f) < w(T).
- T' is a spanning tree lighter than T, which contradicts T being minimum. So no MST contains e.
With ties, step 4 only gives w(f) ≤ w(e). Then T' weighs no more than T, so T' is also an MST, and it avoids e. That is exactly the weak version of the property.
The Path Form: A Test You Can Run
You can restate the cycle property in terms of paths. Edge e = (u, v) with weight w is the strict maximum on some cycle exactly when there is a u–v path that uses only edges strictly lighter than w. That path, together with e, is the cycle.
The converse also holds, and it gives a complete classification of every edge:
| Condition on edge e = (u, v), weight w | Membership |
|---|---|
| u and v are connected using only edges with weight < w | in **no** MST |
| not the above, but u and v are connected using edges with weight ≤ w, excluding e | in **some** MST, not all |
| u and v are not connected using edges with weight ≤ w, excluding e | in **every** MST |
The last row follows from the cut property. Take S to be everything reachable from u through edges of weight ≤ w other than e. Every other edge leaving S then weighs more than w, so e is the unique lightest edge across that cut.
The code below applies the table directly. Each check is one breadth-first search over a filtered set of edges.
from collections import deque
def connected_using(n, edges, u, v, keep):
# BFS from u using only edges i with keep(i, weight) True.
adj = [[] for _ in range(n)]
for i, (w, a, b) in enumerate(edges):
if keep(i, w):
adj[a].append(b)
adj[b].append(a)
seen = [False] * n
seen[u] = True
queue = deque([u])
while queue:
x = queue.popleft()
if x == v:
return True
for y in adj[x]:
if not seen[y]:
seen[y] = True
queue.append(y)
return False
def classify(n, edges):
# edges: list of (weight, u, v); returns each edge with "none", "some", or "every"
result = []
for i, (w, u, v) in enumerate(edges):
if connected_using(n, edges, u, v, lambda j, wj: wj < w):
label = "none"
elif connected_using(n, edges, u, v, lambda j, wj: j != i and wj <= w):
label = "some"
else:
label = "every"
result.append(((u, v, w), label))
return result
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]
for edge, label in classify(4, edges):
print(edge, label)
# Ties: a triangle with equal weights
print(classify(3, [(1, 0, 1), (1, 1, 2), (1, 0, 2)]))Output:
(0, 1, 1) every
(0, 2, 4) none
(1, 2, 3) every
(1, 3, 2) every
(2, 3, 5) none
[((0, 1, 1), 'some'), ((1, 2, 1), 'some'), ((0, 2, 1), 'some')]In the triangle, every edge is a non-strict maximum on the only cycle. Any two of the three edges form an MST, so each edge is in some MST but not in all of them. Each call to classify costs O(V + E) per edge, which is O(E(V + E)) in total. That is fine for checking answers and testing your own implementations, but too slow to use as an MST algorithm.
The Cycle Property Inside Kruskal's Algorithm
When Kruskal's algorithm rejects an edge (u, v) of weight w, it does so because u and v are already in the same component. The edges that connected them were processed earlier, so each weighs at most w. The rejected edge is therefore a maximum-weight edge on a cycle. The cycle property, in its weak form, says that discarding it is safe. Kruskal's accept step relies on the cut property, and its reject step relies on the cycle property.
Reverse-Delete: An Algorithm Built on the Cycle Property
You can also build an MST purely by deleting edges. Reverse-delete goes through the edges from heaviest to lightest and deletes each one whose removal leaves the graph connected. If removing e keeps u and v connected, then e lies on a cycle of surviving edges. Every heavier edge has already been examined, so e is a maximum on that cycle.
def reverse_delete(n, edges):
# Uses connected_using from the previous block.
alive = set(range(len(edges)))
order = sorted(range(len(edges)), key=lambda i: edges[i][0], reverse=True)
for i in order:
w, u, v = edges[i]
if connected_using(n, edges, u, v, lambda j, wj: j in alive and j != i):
alive.remove(i) # e lies on a cycle and is its heaviest remaining edge
kept = sorted(edges[i] for i in alive)
return sum(w for w, _, _ in kept), kept
print(reverse_delete(4, [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]))
# (6, [(1, 0, 1), (2, 1, 3), (3, 1, 2)])Reverse-delete is correct but slow. With a BFS connectivity check for each edge it runs in O(E(V + E)) time. Kruskal's and Prim's algorithms are much faster in practice. Reverse-delete is still useful because it shows that the cycle property alone is enough to find an MST.
Common Errors
- Deleting the globally heaviest edge. The heaviest edge might be a bridge. A bridge lies on no cycle, so it is in every spanning tree, and therefore in every MST.
- Using the strict conclusion when weights tie. If two edges on a cycle share the maximum weight, neither one is ruled out. You may drop only one of them.
- Checking only the shortest cycle. An edge may be the strict maximum on a long cycle but not on a short one. The property needs just one cycle, so use the path test: look for any u–v path made entirely of strictly lighter edges.
- Forgetting the edge itself in the path test. When checking the "every MST" condition, leave e out of the search. Otherwise u and v are trivially connected by e.
Exercise
A connected graph has edges a–b (2), b–c (2), a–c (2), c–d (9). Which edges are in every MST, which are in some MST but not all, and which are in no MST?
Show answer
Edge c–d (weight 9) is a bridge, so it is in every MST even though it is the heaviest edge in the graph. Each of a–b, b–c, and a–c is a tied maximum on the triangle a–b–c, and the other two edges form a path of weight-2 edges between its endpoints. So each of these three is in some MST but not all. No edge is in no MST. Every MST has weight 2 + 2 + 9 = 13.
Exercise
Edge (u, v) has weight 7. The graph contains a u–v path with edge weights 3, 8, 2 and another u–v path with weights 6, 7, 1. Can (u, v) be in an MST?
Show answer
Yes, it can, assuming no other u–v path uses only edges lighter than 7. The first path contains an 8, so (u, v) is not the maximum on that cycle. The second path contains a 7, which ties with (u, v), so the strict cycle property does not apply. By the path test, (u, v) is in some MST but not every MST: there is a path of edges of weight at most 7, but none made entirely of edges lighter than 7.
Exercise
Explain, using the cycle property, why Kruskal's algorithm never discards an edge that every MST needs.
Show answer
Kruskal's algorithm discards (u, v) of weight w only when u and v are already connected by edges it accepted earlier. Edges are processed in sorted order, so each of those edges weighs at most w. That path plus (u, v) is a cycle on which (u, v) is a maximum. By the weak cycle property, some MST avoids (u, v), so (u, v) is not needed by every MST, and discarding it cannot prevent the algorithm from building an MST.