Lably
Skip to content

Kruskal's algorithm

Minimum spanning forest algorithm that greedily adds edges

  • 7 min read
  • 6 chapters

Related on Lably

Through Sorting, Mathematical induction and Quicksort

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 Loberman & Weinberger (1957). 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.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Discrete mathematics