How Kruskal adds the cheapest safe edge

Short answer: Kruskal’s algorithm sorts the undirected edges by weight and adds an edge when its ends lie in different trees of a forest. A union-find structure answers that test. The sort dominates, at O(E log E), and the union-find work is essentially linear. The first |V|−1 accepted edges form a minimum spanning tree when the graph is connected. A rejected edge would have closed a cycle.


What the problem actually asks

The cut property says a lightest edge across any cut is safe. Kruskal’s order is a way to offer edges without maintaining an explicit cut. The lightest edge that touches two different components is a lightest edge across the cut around one of those components, so it is safe. An edge inside a component is the unique cycle-closer for its pair of ends and cannot belong to any spanning tree of that component’s vertices together with the edges already taken.

The algorithm needs a fast “same component” test that also merges components. That structure is union-find.


What the algorithm guarantees

After the edges are sorted, each accepted edge is safe, and a connected graph yields a minimum spanning tree of |V|−1 edges. A disconnected graph yields a minimum spanning forest. Time is O(E log E) from the sort, which is O(E log V) since E < V², plus O(E α(V)) for the union-find operations. Extra memory is the sorted edge list and the parent array.

Equal weights may be broken either way among edges of that weight. Negative weights are ordinary light edges and need no reweighting. The algorithm does not produce shortest paths, and the tree path between two vertices can be heavier than another path in the graph.


The working steps

  1. Sort the undirected edges by nondecreasing weight. Drop self-loops. Parallel edges will lose unless they are the lighter copy and it is accepted.
  2. Initialize a union-find structure with each vertex its own parent.
  3. For each edge in order, if its ends have different roots, add the edge to the forest and union the two roots.
  4. Stop early once |V|−1 edges are accepted and the graph is known to be connected. Otherwise scan every edge to finish a forest.
  5. If fewer than |V|−1 edges were accepted, the graph was disconnected. The accepted edges are still minimum for the components they span.

The sort-and-union procedure computes a minimum connector. It does not compute distances. Running it as a substitute for Dijkstra answers a different optimization problem. The union-find must use the actual endpoints, not vertex ids compared numerically.


Approaches that look simpler and fail

Approach Why it fails when a cycle edge is accepted
Add every edge in sorted order with no component test The result contains cycles and has more edges than a tree on the same vertices.
Test equality of vertex ids instead of roots Two vertices can lie in the same grown component without sharing an id. The edge between them closes a cycle.
Stop at the first edge that is heavier than some threshold and call the result a shortest path The thresholded edges are a bottleneck or a filter. Path length is the sum along a route, which this scan does not minimize.
Forget to sort The safety proof uses the lightest remaining cross edge. An unsorted scan can accept a heavy edge before a light one that would have connected the same components.

A single growing tree with a heap is Prim. Merging every component with its cheapest outgoing edge in parallel rounds is Borůvka. The component test itself is union-find, whose inverse-Ackermann cost is why it disappears beside the sort.


A working example

This Python fragment sorts edges and adds an edge when its endpoints lie in different union-find sets. The safe edges weigh 1+2+3 = 6.

parent = list(range(4))

def find(x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]
        x = parent[x]
    return x

edges = [(1, 0, 1), (2, 1, 2), (4, 0, 2), (3, 2, 3), (5, 1, 3)]
edges.sort()
total = 0
for w, u, v in edges:
    a, b = find(u), find(v)
    if a != b:
        parent[a] = b
        total += w
assert total == 6
print(total)

Where it is used

Kruskal’s algorithm lays out a connecting network, cable or road, by adding the cheapest edge that does not form a cycle. Single-linkage clustering is the same process read as a dendrogram. Prim grows the same tree from a starting vertex instead of a sorted edge list.


When you can drop this algorithm

Drop Kruskal when the graph is dense and an O(V²) Prim scan on a matrix will beat an E log E sort. Keep it when the graph is sparse, the edges are easy to sort, or the same union-find already exists for another reason.


Why this stays published

We publish this so a search for Kruskal’s algorithm returns the sorted-edge scan and the union-find test that rejects a cycle. Kruskal’s algorithm sorts undirected edges and accepts the next edge whose ends lie in different components, producing a minimum spanning tree in O(E log E) time.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *