How to Solve a Minimum Spanning Tree with Reverse-Delete
A minimum spanning tree (MST) connects every vertex in a connected, undirected weighted graph without creating a cycle while giving the smallest possible total edge weight. The reverse-delete algorithm begins with the complete graph, considers edges from heaviest to lightest, and removes an edge whenever doing so leaves the graph connected. For a graph with (n) vertices, the surviving network has (n-1) edges.
The task is about connectivity rather than one traveller’s preferred route. Every edge needs a meaningful weight, such as distance, cost, delay or energy consumption, and the input graph must be connected. Self-loops can be ignored, parallel edges remain valid, and equal weights may produce different minimum trees with the same total cost.
| Method | Edge process | Connectivity structure | Practical profile |
|---|---|---|---|
| Reverse-delete | Start with every edge and delete from heaviest to lightest | DFS, bridge checks or dynamic connectivity | Directly models which network edges can be removed |
| Kruskal | Build a forest from lightest to heaviest | Disjoint-set union | Simple and efficient for many graphs |
| Prim | Grow one tree from a chosen vertex | Minimum-priority queue | Effective for dense graphs and repeated growth from a root |
What reverse deletion is solving
An MST minimises the aggregate weight of the selected edges; it does not calculate the shortest path between each pair of vertices. If the requirement is to find minimum-cost routes from one source, particularly when negative weights are possible, the Bellman-Ford guide describes the appropriate relaxation method.
Picture a rail-asset network across Sydney and Melbourne. Stations and stops are vertices, track sections are edges, and weights might represent kilometres, possession time or maintenance expenditure in Australian dollars. The desired result is the least expensive connected rail network that keeps every station serving daily commuters, not merely the quickest path for one passenger.
Represent the problem as (G=(V,E)), with a numeric weight attached to every edge. Reverse-deletion tests global connectivity after each tentative removal. If an edge is a bridge, deleting it would create isolated regions, so it must remain. Because the graph starts connected and edges are removed only when an alternative connection survives, the final graph is connected and acyclic.
Why heaviest-first removal is safe
The cycle property provides the main greedy argument. If an edge is the unique heaviest edge on a cycle, it cannot be required by an MST: the rest of the cycle supplies a connection without it. When several cycle edges share the maximum weight, at least one of them can be excluded while preserving a minimum total weight.
A temporary deletion and the cut property explain the opposite case. If removing an edge would disconnect the current graph, that edge is a bridge. It is also a lightest available edge across the resulting cut because every heavier edge has already been processed. Keeping it therefore preserves the possibility of constructing an MST.
order ← edges sorted by non-increasing weight
active ← all graph edges
for edge in order:
trial ← active with edge removed
if trial connects every vertex:
active ← trial
return active
Ties may be processed in any order because the cycle and cut properties remain valid. Different orders can retain different edges, but their summed weight will be identical when the input is connected. A stable secondary key, such as edge ID, is useful when reproducible output matters.
A Python implementation you can test
The following implementation uses vertex numbers from 0 through n - 1. Each edge is stored separately by ID, so parallel connections are handled correctly, while self-loops are discarded. A depth-first search checks whether the graph would remain connected after the current edge is skipped.
def reverse_delete_mst(n, edges):
graph = [[] for _ in range(n)]
candidates = []
active = set()
for edge_id, (u, v, weight) in enumerate(edges):
if u == v:
continue
graph[u].append(edge_id)
graph[v].append(edge_id)
candidates.append((weight, edge_id))
active.add(edge_id)
def reaches_all(blocked, start):
seen = {start}
stack = [start]
while stack:
node = stack.pop()
for edge_id in graph[node]:
if edge_id == blocked or edge_id not in active:
continue
u, v, _ = edges[edge_id]
neighbour = v if node == u else u
if neighbour not in seen:
seen.add(neighbour)
stack.append(neighbour)
return len(seen) == n
if n == 0:
return [], 0
if not reaches_all(None, 0):
raise ValueError("The input graph must be connected")
for _, edge_id in sorted(candidates, reverse=True):
u, v, weight = edges[edge_id]
if reaches_all(edge_id, u):
active.remove(edge_id)
tree = [edges[edge_id] for edge_id in sorted(active)]
return tree, sum(weight for _, _, weight in tree)
For a graph with edges [(0,1,7), (0,2,3), (1,2,5), (1,3,9), (2,3,4)], the edges weighted 9 and 7 are deleted. The remaining weights are 3, 5 and 4, producing a total cost of 12.
Automated tests should verify that the result has exactly (n-1) edges, reaches every vertex, contains no cycle and agrees with the total from another MST implementation. The function also rejects a disconnected input because reverse deletion can produce only a forest in that case.
Complexity, scaling and common failure modes
Sorting costs (O(E \log E)), while each connectivity search can visit (O(V+E)) vertices and edges. The complete running time is therefore (O(E \log E + E(V+E))), with (O(V+E)) additional space. Reverse-deletion is often more instructional than computationally efficient, although dynamic-connectivity structures can reduce repeated search costs.
For a very large graph or many repeated MST calculations, Kruskal with disjoint-set union usually offers a simpler performance path. Reverse-deletion remains valuable when engineers need to audit rejected connections, enforce a heaviest-first removal policy or explain the cycle property directly.
Common mistakes include mixing kilometres with monetary costs, treating a directed graph as undirected, and assuming an unrecorded connection has zero weight. A Perth council, for example, might weight sealed road segments by inspection labour and closure hours rather than their physical length. Those measurements describe different resources and cannot be compared until converted into a consistent unit.
Reliable use in Australian planning projects
A dependable implementation needs explicit assumptions and repeatable checks rather than relying on accidental graph behaviour.
- Confirm that the input is a connected, undirected graph before deleting edges.
- Assign every candidate edge a unique identifier, including parallel connections.
- Use one consistent cost unit and record its currency, scale or collection time.
- Define a deterministic tie-breaking rule when reproducible results are required.
- Preserve rejected edges with their weights so the final tree can be audited.
A Brisbane wholesaler linking warehouses, depots and independent stores illustrates the modelling decisions. Freight kilometres, unloading time, fuel expenditure and disruption risk are different quantities; converting them to Australian-dollar costs may suit investment planning, while keeping separate dimensions may be necessary for operational analysis.
Graphs can also contain information about people. If vertices identify customers, patients or sole traders, an exported network may fall under the Privacy Act 1988 and the Australian Privacy Principles, including data minimisation, purpose limitation and security obligations. The representation also appears in machine learning workflows, where products, users or records become vertices and similarity or cost becomes an edge weight. An MST can then provide a compact dependency network, provided that privacy restrictions and domain constraints remain separate from the optimisation objective.