Reverse-Delete is the mirror image of the usual minimum-spanning-tree algorithms. Instead of adding the cheapest edges that keep the tree growing, it removes the most expensive edges that the graph can live without.
Problem. Given a connected, undirected, weighted graph, find a minimum spanning tree (MST): a subset of edges that connects every vertex with the smallest possible total weight.
Example: 4 vertices A, B, C, D with edges A-B (1), A-C (2), B-D (3), C-D (4), B-C (5). The MST keeps A-B, A-C, B-D for a total weight of 6.
The slow way first
You could enumerate every subset of edges, throw away the ones that are not spanning trees, and pick the cheapest. The number of subsets is exponential, so that is hopeless for anything but a tiny graph.
The question to ask: which edges are obviously safe to throw away? A heavy edge is a waste as long as the graph stays connected without it. So work from the most expensive edge downward and delete greedily.
The idea: delete the heaviest redundant edges
Sort the edges from heaviest to lightest. Walk that list. For each edge, ask: if I remove it, is the graph still connected? If yes, the edge was redundant — there was a cheaper way around it — so delete it. If no, removing it would split the graph, which means it is a bridge we are forced to keep.
What survives this pruning is exactly a minimum spanning tree. We always removed the most expensive edge we possibly could, so the cheapest necessary edges are the ones left standing.
Walk through it
Step through the animation. The five edges are tried in weight order 5, 4, 3, 2, 1. Edges B-C (5) and C-D (4) fade out — the graph stays connected without them. Then B-D, A-C, and A-B each turn out to be bridges: removing any of them would isolate a vertex, so they are kept, building the total up to 6.
Pseudocode
sort edges from heaviest to lightest
total = 0
for each edge (u, v, w) in that order:
temporarily remove (u, v, w)
if the graph is still connected:
keep it removed # redundant edge
else:
put it back # bridge — required
total = total + w
return totalThe Python solution
def reverse_delete(n, edges): # edges: list of (u, v, w)
edges.sort(key=lambda e: e[2], reverse=True)
total = 0
for u, v, w in edges:
without = [e for e in edges if e != (u, v, w)]
if connected(n, without):
edges = without # redundant — drop it
else:
total += w # bridge — keep it
return total- We sort with
reverse=Trueso the heaviest edge comes first. - For each edge we build
without, the edge set minus the current edge, and ask whether that smaller graph still connects allnvertices. connected(...)is a standard graph search (BFS/DFS or union-find) that returnsTruewhen every vertex is reachable.- If it is still connected, the edge was redundant — we commit to the deletion by setting
edges = without. - Otherwise the edge is a bridge: we leave it in and add its weight
wtototal.
Complexity
| Case | Time | Notes |
|---|---|---|
| Sort the edges | O(E log E) (moderate) | by weight, once |
| Connectivity check per edge | O(E + V) (moderate) | BFS/DFS each time |
| Overall | O(E · (E + V)) (moderate) | one check per edge |
O(V + E) (moderate)The connectivity check inside the loop is what makes this slower than Kruskal or Prim. It is prized for being conceptually clean and for working when you literally want the edges you can afford to delete.
When this pattern shows up
When a problem is about pruning a structure down to the cheapest connected core, think delete the heaviest thing you can spare. The mirror trick — greedily removing instead of greedily adding — also appears in network-resilience and redundant-link questions.
Process edges in descending weight order, not ascending. If you delete light edges first you can strand a vertex early and end up keeping an expensive edge you did not need.
Practice
In the example, after B-C (5) and C-D (4) are deleted, we try B-D (3). Is it kept or dropped, and why?
1. In what order does Reverse-Delete process the edges?
2. When is an edge kept rather than deleted?
3. Why is this slower than Kruskal or Prim?
4. What does the set of surviving edges form?