Kruskal's algorithm

Uses your system voice; voice and rate in settings.

Snapshot

≈ 7 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree. It is a greedy algorithm that in each step adds to the forest the lowest-weight edge that will not form a cycle. The key steps of the algorithm are sorting and the use of a disjoint-set data structure to detect cycles. Its running time is dominated by the time to sort all of the graph edges by their weight. A minimum spanning tree of a connected weighted graph is a connected subgraph, without cycles, for which the sum of the weights of all the edges in the subgraph is minimal. For a disconnected graph, a minimum spanning forest is composed of a minimum spanning tree for each connected component. This algorithm was first published by Joseph Kruskal in 19561, and was rediscovered soon afterward by . Other algorithms for this problem include Prim's algorithm, Borůvka's algorithm, and the reverse-delete algorithm.

Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree. It is a greedy algorithm that in each step adds to the forest the lowest-weight edge that will not form a cycle. The key steps of the algorithm are sorting and the use of a disjoint-set data structure to detect cycles. Its running time is dominated by the time to sort all of the graph edges by their weight. A minimum spanning tree of a connected weighted graph is a connected subgraph, without cycles, for which the sum of the weights of all the edges in the subgraph is minimal. For a disconnected graph, a minimum spanning forest is composed of a minimum spanning tree for each connected component. This algorithm was first published by Joseph Kruskal in 1956, and was rediscovered soon afterward by . Other algorithms for this problem include Prim's algorithm, Borůvka's algorithm, and the reverse-delete algorithm.

Algorithm

The algorithm performs the following steps: Create a forest (a set of trees) initially consisting of a separate single-vertex tree for each vertex in the input graph. Sort the graph edges by weight. Loop through the edges of the graph, in ascending sorted order by their weight. For each edge: Test whether adding the edge to the current forest would create a cycle. If not, add the edge to the forest, combining two trees into a single tree. At the termination of the algorithm, the forest forms a minimum spanning forest of the graph. If the graph is connected, the forest has a single component and forms a minimum spanning tree.

Pseudocode

The following code is implemented with a disjoint-set data structure. It represents the forest F as a set of undirected edges, and uses the disjoint-set data structure to efficiently determine whether two vertices are part of the same tree. function Kruskal(Graph G) is F:= ∅ for each v in G.Vertices do MAKE-SET(v) for each {u, v} in G.Edges ordered by increasing weight({u, v}) do if FIND-SET(u) ≠ FIND-SET(v) then F := F ∪ { {u, v} } UNION(FIND-SET(u), FIND-SET(v)) return F

Spanning tree

Let G be a connected, weighted graph and let Y be the subgraph of G produced by the algorithm. Y cannot have a cycle, as by definition an edge is not added if it results in a cycle. Y cannot be disconnected, since the first encountered edge that joins two components of Y would have been added by the algorithm. Thus, Y is a spanning tree of G .

Minimality

We show that the following proposition P is true by induction: If F is the set of edges chosen at any stage of the algorithm, then there is some minimum spanning tree that contains F and none of the edges rejected by the algorithm. Clearly P is true at the beginning, when F is empty: any minimum spanning tree will do, and there exists one because a weighted connected graph always has a minimum spanning tree. Now assume P is true for some non-final edge set F and let T be a minimum spanning tree that contains F. If the next chosen edge e is also in T, then P is true for F + e. Otherwise, if e is not in T then T + e has a cycle C. The cycle C contains edges which do not belong to F + e, since e does not form a cycle when added to F but does in T. Let f be an edge which is in C but not in F + e. Note that f also belongs to T, since f belongs to T + e but not F + e. By P, f has not been considered by the algorithm. f must therefore have a weight at least as large as e. Then T − f + e is a tree, and it has the same or less weight as T. However since T is a minimum spanning tree then T − f + e has the same weight as T, otherwise we get a contradiction and T would not be a minimum spanning tree. So T − f + e is a minimum spanning tree containing F + e and again P holds. Therefore, by the principle of induction, P holds when F has become a spanning tree, which is only possible if F is a minimum spanning tree itself. pivot = choose_random(G.E) E , E = partition(G.E, pivot) A = filter_kruskal(E ) E = filter(E ) A = A ∪ filter_kruskal(E ) return A function partition(E, pivot) is E = ∅, E = ∅ foreach (u, v) in E do if weight(u, v) ≤ pivot then E = E ∪ {(u, v)} else E = E ∪ {(u, v)} return E , E function filter(E) is E = ∅ foreach (u, v) in E do if find_set(u) ≠ find_set(v) then E = E ∪ {(u, v)} return E Filter-Kruskal lends itself better to parallelization as sorting, filtering, and partitioning can easily be performed in parallel by distributing the edges between the processors. Finally, other variants of a parallel implementation of Kruskal's algorithm have been explored. Examples include a scheme that uses helper threads to remove edges that are definitely not part of the MST in the background, and a variant which runs the sequential algorithm on p subgraphs, then merges those subgraphs until only one, the final MST, remains.

See also

Dijkstra's algorithm Greedy geometric spanner Single-linkage clustering

Text derived from https://en.wikipedia.org/w/index.php?oldid=1372890657 under CC BY-SA 4.0; modifications have been made.

References

  1. ↩Wikipedia “Kruskal's algorithm” revision 1372890657 · retrieved 2026-10-20 publication date (×1) · confidence 0.95 revision URL