I was reading wikipeida and found Kruskal’s Pseudocode as the following:
KRUSKAL(G):
foreach v ∈ G.V:
MAKE_SET(v)
G.E = sort(G.E)
i = 0
while (i != |V|-1):
pick the next (u, v) edge from sorted list of edges G.E
if (FIND_SET(u) != FIND_SET(v)):
UNION(u, v)
i = i + 1
I’m not quiet sure what FIND_SET() does, and Wikipedia has the follow description:
if that edge connects two different trees, then add it to the forest, combining two trees into a single tree.
So I guess it checks if two different trees are connected, but what does this really mean?
Initially, each vertex is in a set all by itself: There is a singleton set
{v}for every vertexv. In the pseudo-code, these sets are the result ofmake_set(v).For a given vertex
v, the functionfind_set(v)gives you the set containingv.The algorithm merges sets iteratively, so if
{u},{v}are singleton sets initially and there is an edge(u, v), then the algorithm replaces the two sets by their union{u, v}. Now bothfind_set(u)andfind_set(v)will return that set.The algorithm terminates after you’ve added
|V| - 1non-trivial edges, which is precisely the number of edges in a tree.