Kruskal's algorithm
Appearance
Kruskal's algorithm is a greedy algorithm to find a minimum spanning tree in a weighed, undirected graph. Joseph Kruskal first described it in 1956:
Perform the following step as many times as possible: Among the edges (..) not yet chosen, choose the shortest edge, which does not form any loops with those edges already chosen
— Kruskal, 1956
In this case, the shortest edge is the one with the lowest weight.
Example
Image | Description |
---|---|
![]() |
AD and CE are the shortest edges, with length 5, and AD has been arbitrarily chosen, so it is highlighted. |
![]() |
CE is now the shortest edge that does not form a cycle, with length 5, so it is highlighted as the second edge. |
![]() |
The next edge, DF with length 6, is highlighted using much the same method. |
![]() |
The next-shortest edges are AB and BE, both with length 7. AB is chosen arbitrarily, and is highlighted. The edge BD has been highlighted in red, because there already exists a path (in green) between B and D, so it would form a cycle (ABD) if it were chosen. |
![]() |
The process continues to highlight the next-smallest edge, BE with length 7. Many more edges are highlighted in red at this stage: BC because it would form the loop BCE, DE because it would form the loop DEBA, and FE because it would form FEBAD. |
![]() |
Finally, the process finishes with the edge EG of length 9, and the minimum spanning tree is found. |