Kruskal
Kruskal's Algorithm is a minimum-spanning-tree algorithm which finds a minimal spanning tree for a connected weighted graph.
It's a greedy algorithm.
It uses UnionFind (aka Disjoint Set).
Algorithm
Summary: Try to include the edges one by one from smallest cost to greatest cost. Skip those causing cycles. Stop once all nodes are connected.
create a forest F (a set of trees), where each vertex in the graph is a separate tree
create a set S containing all the edges in the graph
while S is nonempty and F is not yet spanning
remove an edge with minimum weight from S
if the removed edge connects two different trees then add it to the forest F, 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
Implementation
When the run time limitation is tight and it's a dense graph E ~= V^2
, using sort
might cause TLE. We need to use heap
instead, which will reduce the time complexity from V^2 * log(V^2)
to K * log(V^2)
where K
is the number of edges we need to scan to complete the tree. K
is usually way smaller than V^2
.
The following solution is for 1584. Min Cost to Connect All Points (Medium).
Problems
Reference
Last updated