Given a connected, undirected and weighted graph G, find a subset of the
edges E such that G is still connected and the weight of the selected subset
is minimal
Example: Building a road network in a village such that each hut
(vertex) is connected and the cost of connecting roads (edges) is minimal
Kruskal’s algorithm revolves around sorting the graph’s edges (which should
be in an Edge List) based on their weights. After that, we greedily pick the smallest
edge to add spanning tree. Should this newly added edge form a cycle in our spanning tree,
we discard it.
We keep repeating this process until we’ve processed each edge E in the Edge List.
We should then be left with V-1 edges representing the minimum spanning tree.
Note: To detect whether or not the addition of an edge to our minimum spanning
tree will cause a cycle, we can use a Union-Find Disjoint Sets data structure
as demonstrated in the code below.
Code
C++11 code below.
Variations
Maximum Spanning Tree
Instead of sorting the Edge List in ascending order, we sort the Edge List
in descending order and proceed normally.
Minimum ‘Spanning Forest’
In this variant, we want to form a forest of K connected components.
Remember, when we first initialize our UnionFind class, we have V connected components
(none of our nodes are connected initially). Also, whenever we do a union operation
unionSet() on two sets, the number of connected components naturally decreases by 1 for
each union.
Hence, to find the Minimum Spanning Forest, we run Krushkal’s Algorithm as per normal,
but as soon as the number of connected components equals to the desired pre-determined number K,
we can terminate the algorithm.
Second Best Spanning Tree
Sometimes, we may not just want the MST, but also the second best MST of a graph.
To accomplish this, sort the edges then find the optimal MST using Kruskal. Next, for each edge in the MST
(there are at most V-1 edges in the MST), temporarily flag it so that it cannot be chosen,
then try to find the MST again but now excluding that flagged edge.
Note: We do not have to re-sort the edges at this point.
The best spanning tree found after this process is the second best ST.
Minimax and Maximin
The Minimax path problem is the problem of finding the maximum edge weight
along a minumum path.
For example, when traversing through a city roads (edges),
where each road has an associated weight related to sound pollution, we want to
traverse the city such that weight of the maximal edge is minimal.
The Maximin path problem is the opposite. We want the find the minimum edge along
a maximum path.
To solve this problem, we run Kruskal’s algorithm on our graph and acquire our
MST. The minimax path solution is thus the max edge weight along the unique
path between vertex i and j in this MST.