Table of Contents

4.6. Implementing Kruskal's Algorithm: The Union-Find Data Structure


As each edge e = (u,w) is considered
Find the connected components containing v and w.

If these components are different:
Then there is no path from v to w, so e should be added to the structure

Elif they're the same:

Then there's a path from v to w, so e should be omitted.


The Problem


A simple Data Structure for Union-Find

»»»»»»»»> Maintain an Array Component of size n that contains the name of the set currently containing each element

Let S = {1,…,n}

Component[s] = the name of set containing s

To implement MakeUnionFind(S) :
Set up an array and initialize it to Component[s] = s for all s in S

With this operation, Find(v) is done in O(1) time

But Union(A,B) can take O(n) time with this operation(–>We have to update the values of Component[s] for all elements in A and B)

To improve on this bound of Union(A,B):

Maintain the list of elements in each set

Choose the name for the union to be the name of one of the sets, so we only update Component[s] for s in the other set

If the other set is big, name the union to be the name of that set and update Component[s] for s in the smaller set

Improvements:
Maintain an Array Size of length n

Size[A] = the size of A

So when a Union(A,B) operation is needed, we use the name of the larger set for the union and update Component for only the smaller set

However, we still have a O(n) time even after the improvements

So, overall, Find(u) takes O(1) time,MakeUnionFind(A,B) takes O(n) time and any sequence of k Union operations takes at most O(klogk) time with this simple implementation of the algorithm.


A Better Implementation for Union-Find


We maintain a set S of size n

Unions keep the name of the largest set

A Union operation takes O(1) time

MakeUnionFind(S) takes O(n) time

Find(u) takes O(logn) time

Implementing Kruskal's Algorithm

T = {}
foreach (u ∈ V) make a set containing singleton u
for i = 1 to m
(u,v) = ei
if (u and v are in different sets)
T = T ∪ {ei}
merge the sets containing u and v

return T

( The above algorithm, is from the class discussion)

Kruskal's algorithm implemented as above runs in O(mlogn) time.

This section was also interesting, although it contained some long proofs. I give it an 8/10