Kruskal's MST Algorithm: Description
Kruskal's MST Algorithm: Description
Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Kruskal's algorithm is an example of a greedy algorithm
Description
create a forest F (a set of trees), where each vertex in the graph is a separate tree create a set S containing all the edges in the graph while S is nonempty and F is not yet spanning o remove an edge with minimum weight from S o if that edge connects two different trees, then add it to the forest, combining two trees into a single tree, otherwise discard that edge.
At the termination of the algorithm, the forest has only one component and forms a minimum spanning tree of the graph.
Example 1
Initially connected components are just singleton sets with individual vertices: {A} {B} {C} {D} {E} {F} {G} {H} {I} {J} {K} {L} {M} After the following five minimum edges A-1-B D-1-F E-1-G J-1-K, L-1-M have been added to the MST, components are: {A,B} {D,F} {C} {E,G} {H} {I} {J,K} {L,M}
B-1-C G-1-J K-1-I we will have five connected components: {A,B,C} {D,F} {E,G,J,K,I} {H} {L,M}
Next supposing the following minimum edges are chosen: A-2-F {A,B,C,D,F} {E,G,J,K,I} {H} {L,M} H-2-I {A,B,C,D,F} {E,G,J,K,I,H} {L,M} J-2-M {A,B,C,D,F} {E,G,J,K,I,H,L,M} And finally add edge D-2-E to get one connected component:: {A,B,C,D,F,E,G,J,K,I,H,L,M}
Example 2
wgraph3.txt Initially all 11 edges are in the heap and each vertex is isolated. Then after next 2 minimum edges A-5-D and C-5-E are chosen as shown. A 7 B 9 D 6 F 11 G 15 7 E 8 9 8 5 C 5 A 7 B 9 D 6 F 11 G 15 7 E 8 9 8 5 C
MST trees are { {A} {B} {C} {D} {E} {F} {G}}.
Then D-6-F and B-7-E chosen from heap. A C 5 7 15 6 F 11 MST trees { {A D} {B} {C E} {F} {G}} G E 8 9 D 6 F 11 G 5 A 7 B 9 15 7 E 8 9 8 5 C
7 B 9 D
7 B 9 D 6 F 15
8 5
7 E 8
But next 3 edges chosen from heap B-8-C, E-8-F and D-9-B would give rise to cycles. A 7 B 9 D 6 F 11 G 11 15 7 E 8 9 8 5 C A 7 B 9 D 6 F G 15 7 E 8 9 8 5 C
Edge E-9-G completes the MST. Still 2 edges on heap, F-11-G and D-15-E which lead to cycles and so are discarded. Finally heap is empty and algorithm is finished.
7 B 9 D 6 F 15
8 5
7 B 9 D 15 6 F
8 5
7 E 8
7 E 8
11
11
G 5
Pseudocode
MST_Kruskal() // Input is simple connected graph represented by array of edges edge[] // Output is list of edges T in MST // Create a partition for the set of vertices for each vertex v V Cv = {v} // create a minHeap h from array of edges E h = Heap( E) // let T be an initially empty tree T= while size(T) < n-1 e = h.removeMin() let ({u,v}, wgt) = e Cv = findSet(v) Cu = findSet(u) if Cu Cv union(Cu , Cv) T = T {e} return T
Time Complexity
Kruskal's algorithm can be shown to run in O(E log E) time, or equivalently, O(E log V) time.
Initially connected components are just singleton sets with individual vertices: {A} {B} {C} {D} {E} {F} {G} {H} {I} {J} {K} {L} {M} After the following five minimum edges A-1-B D-1-F E-1-G J-1-K, L-1-M have been added to the MST, components are: {A,B} {D,F} {C} {E,G} {H} {I} {J,K} {L,M}
Choosing 3 more minimum edges of weight 1, B-1-C G-1-J K-1-I we will have five connected components: {A,B,C} {D,F} {E,G,J,K,I} {H} {L,M}
Next supposing the following minimum edges are chosen: A-2-F {A,B,C,D,F} {E,G,J,K,I} {H} {L,M} H-2-I {A,B,C,D,F} {E,G,J,K,I,H} {L,M} J-2-M {A,B,C,D,F} {E,G,J,K,I,H,L,M} And finally add edge D-2-E to get one connected component:: {A,B,C,D,F,E,G,J,K,I,H,L,M}