Kruskal's Algorithm: Melanie Ferreri
Kruskal's Algorithm: Melanie Ferreri
The Algorithm
Time Complexity
References
Kruskal’s Algorithm
Melanie Ferreri
February 8, 2021
Preliminary Definitions
Definition
A graph G = (V, E) consists of a finite set V of vertices, and a set E
of two-element subsets of V , which represent edges between
vertices. We sometimes denote the vertex set of G by V (G) and the
edge set by E(G). A weighted graph is a graph whose edges have
each been assigned a numerical weight. The weight of a graph is the
sum of the weights of its edges.
Preliminary Definitions
Definition
A tree is an acyclic connected graph (there is a path between any
pair of vertices, and there are no cycles).
Some Facts
The cheapest way to ensure that we can get from any village A to any
village B is by building roads in the shape of a minimum spanning
tree.
Kruskal’s Algorithm
Theorem
Kruskal’s algorithm produces a minimum spanning tree in a
connected weighted graph.
Proof.
Let G be a connected weighted graph with n vertices. Let T be a
spanning tree obtained by Kruskal’s algorithm, where
E(T ) = {e1 , e2 , . . . , en−1 } ordered by selection in constructing the
tree. Let w(ei ) be the weight of ei , so w(e1 ) ≤ w(e2 ) ≤ · · · ≤ w(en−1 ),
n−1
X
and w(T ) = w(ei ).
i=1
We want to show that T is a MST. Suppose to the contrary that T is
not minimum, and let H be a MST which has the most possible edges
in common with T among all MST’s.
Example
b c
2 1
d
7 3
2
e
6 1
5
f g
Example (continued)
We start out with the graph T having no edges, then we add the first
two edges of minimum weight.
a a
b c b 1 c
d d
e e 1
f g → f g
Example (continued)
b 1 c b 2 1 c
d d
2 2
e 1 e 1
f g → f g
Example (continued)
4 a 4 a
b 2 1 c b 2 1 c
d d
2 2
e 1 e 1
5
f g → f g
We can sort the edges with MergeSort, which takes O(m log m) time.
For checking for cycles, we can use a Union Find data structure to
manage the edges of the MST.
Union Find
Union Find has two operations: union, and find. When we call
Union(a,b), we set vertex b as the parent of vertex a, so Arr[a] =
b. The Find operation tells us whether two vertices are connected,
by checking if they are in the same tree. This is done by following up
the parents of each vertex until we reach a vertex which is its own
parent - this is the root of the tree. If two vertices are in the same tree,
we will get the same root, and know they are connected.
b 1 c
d
2
e 1
f g
b 1 c
d
2
e 1
f g
Overall Complexity
At each step of Kruskal’s algorithm, we have to use the union and find
operations in order to check if our potential new edge will create a
cycle. We will have to iterate through at most all m edges of G, which
contributes m ∗ O(log n) = O(m log n) time. We also know sorting the
edges by weight takes O(m log m) time, so in total we have time
complexity O(m log m + m log n). There can be at most n2 edges of
G; thus log(m) is at most 2 log(n), so O(log m) = O(log n) in this case.
Thus, the total time complexity of Kruskal’s algorithm is O(m log m).
References