Solution: Kruskal’s Minimum Spanning Tree
In this review, we give a detailed analysis on finding the minimum spanning tree of the given graph using Kruskal's algorithm.
We'll cover the following...
Solution: Kruskal’s algorithm
Kruskal’s algorithm is a minimum-spanning-tree algorithm that finds an edge of the least possible weight.
It is a greedy algorithm, as it finds a minimum spanning tree for a connected weighted graph and adds increasing cost arcs at each step.
class Kruskal {public static void KruskalMST(Graph g) {Graph.Edge answer[] = new Graph.Edge[g.Vertex];int j = 0; //index for keeping track of asnwer[]int i = 0; //index for keeping track of sorted edgesfor (i = 0; i < g.Vertex; ++i) {answer[i] = new Graph.Edge();}//sort all edgesArrays.sort(g.edge);//allocating memory to create subsetsGraph.DisjointSets mySet[] = new Graph.DisjointSets[g.Vertex];for (i = 0; i < g.Vertex; ++i)mySet[i] = new Graph.DisjointSets();//creating subsetsfor (int x = 0; x < g.Vertex; ++x) {mySet[x].p = x;mySet[x].r = 0;}i = 0;while (j < g.Vertex - 1) {//picking the smallest edgeGraph.Edge nextEdge = new Graph.Edge();//incrementing the index for next iterationnextEdge = g.edge[i++];int temp1 = g.find(mySet, nextEdge.source);int temp2 = g.find(mySet, nextEdge.destination);//if cycle not formed, include edge in answer[]if (temp1 != temp2) {//incrementing the index for answer[] for next edgeanswer[j++] = nextEdge;g.merge(mySet, temp1, temp2);}}//printing contents of answer[] to display the MSTfor (i = 0; i < j; ++i)System.out.println(answer[i].source + " , " + answer[i].destination);}}class Main {public static void main(String[] args) {int V = 4, E = 5;Graph graph = new Graph(V, E);// add edge 0-1graph.edge[0].source = 0;graph.edge[0].destination = 1;graph.edge[0].weight = 10;// add edge 0-2graph.edge[1].source = 0;graph.edge[1].destination = 2;graph.edge[1].weight = 6;// add edge 0-3graph.edge[2].source = 0;graph.edge[2].destination = 3;graph.edge[2].weight = 5;// add edge 1-3graph.edge[3].source = 1;graph.edge[3].destination = 3;graph.edge[3].weight = 15;// add edge 2-3graph.edge[4].source = 2;graph.edge[4].destination = 3;graph.edge[4].weight = 4;System.out.println("Minimum Spanning Tree of Graph 1");Kruskal.KruskalMST(graph);System.out.println();V = 6;E = 15;Graph graph1 = new Graph(V, E);graph1.edge[0].source = 0;graph1.edge[0].destination = 1;graph1.edge[0].weight = 4;graph1.edge[1].source = 0;graph1.edge[1].destination = 2;graph1.edge[1].weight = 3;graph1.edge[2].source = 1;graph1.edge[2].destination = 2;graph1.edge[2].weight = 1;graph1.edge[3].source = 1;graph1.edge[3].destination = 3;graph1.edge[3].weight = 2;graph1.edge[4].source = 2;graph1.edge[4].destination = 3;graph1.edge[4].weight = 4;graph1.edge[5].source = 3;graph1.edge[5].destination = 4;graph1.edge[5].weight = 2;graph1.edge[6].source = 4;graph1.edge[6].destination = 5;graph1.edge[6].weight = 6;System.out.println("Minimum Spanning Tree of Graph 2");Kruskal.KruskalMST(graph1);}}
Explanation
Kruskal’s algorithm does the following:
-
It sorts all the edges in non-decreasing order of their weight (line 11).
-
It chooses the smallest edge (line 27) and checks if it forms a cycle with the spanning tree formed so far. It includes this edge if a cycle is not formed (lines 36-40). Otherwise, it discards the edge. To detect a cycle, it iterates through all the edges of the graph to find a subset of both vertices of every edge. If both subsets are the same, there is a cycle in the graph (lines 26-33).
-
It repeats step two until there are (V-1) edges in the spanning tree.
Animation
The animation below gives a demo of how Kruskal’s algorithm is applied to a graph to find its minimum spanning tree.