Kruskal's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected, weighted graph. The algorithm sorts edges by weight and iteratively adds the smallest weight edge that does not create a cycle.
The algorithm performs the following steps:
Kruskal's algorithm is particularly effective for sparse graphs where the number of edges is much smaller than
When to use Kruskal's:
Looking at the main operations in Kruskal's algorithm:
find operation (twice) and possibly a unionSets operation on the union-find data structure.With path compression and union by rank optimizations, each union-find operation takes
Processing all
Combining these operations:
Since