Professional Documents
Culture Documents
Kruskal's Algorithm
Kruskal's Algorithm
Numerical 1 A D
Find Minimum spanning
Tree (MST) of the given
undirected edge-weighted 8
graph using Kruskal’s 6
algorithm. 7 8
C 6
E
B 8
5
6
A D
Problem 1 8
6
7 8
C 6
E
Edge AB AC BC BD BE CD CE DE
Wt. 5 6 7 8 6 8 6 8
B 8
5
6
A D
Step 1 8
6
7 8
C 6
E
Edge AB AC BE CE BC BD CD DE
Wt. 5 6 6 6 7 8 8 8
B 8
5
6
A D
Step 2 8
6
7 8
C 6
E
B 8
5
6
A D
Step 3
6
Total Weight = 5+6+6+8
= 25
C E
8 7
Numerical 2 B C D
Find Minimum spanning Tree
4 2 9
(MST) of the given undirected
edge-weighted graph using
Kruskal’s algorithm.
A 11 J 4 14 E
7
8 6 10
H 1 G 2 F
Edge AB BC CD DE EF FG GH HJ BH DF JC JG CF AH
Wt. 4 8 7 9 10 2 1 7 11 14 2 6 4 8
8 7
Step 1 B C D
Sort all the edges in non-
4 2 9
decreasing order of their
weights.
A 11 J 4 14 E
7
8 6 10
H 1 G 2 F
Edge HG JC FG AB CF JG CD HJ AH BC DE EF BH DF
Wt. 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
Step 2 B C D
Pick the smallest edge.
4 2 9
Check if it forms a cycle
with the spanning tree
A 11 J 4 14 E
formed so far.
7
If cycle is not formed,
include this edge. 8 6 10
Else, discard it.
H 1 G 2 F
Edge HG JC FG AB CF JG CD HJ AH BC DE EF BH DF
Wt. 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
Step 3 B C D
Repeat step 2 until there
4 2 9
are (V-1) edges in the
spanning tree.
A 11 J 4 14 E
7
8 6 10
H 1 G 2 F
Edge HG JC FG AB CF JG CD HJ AH BC DE EF BH DF
Wt. 1 2 2 4 4 6 7 7 8 8 9 10 11 14
8 7
Step 3 B C D
Repeat step 2 until there
4 2 9
are (V-1) edges in the
spanning tree.
A 11 J 4 14 E
In this problem, we have 9
nodes. 7
We terminate the process 8 6 10
of adding edges to the tree
as we have got 9 – 1 = 8
edges. H 1 G 2 F
Edge HG JC FG AB CF JG CD HJ AH BC DE EF BH DF
Wt. 1 2 2 4 4 6 7 7 8 8 9 10 11 14
7
Answer B C D
Weight of the Minimum
4 2 9
Spanning Tree
=1+ 2 + 2 + 4 + 4 + 7 + 8 + 9
= 37
A J 4 E
8
H 1 G 2 F
Edge HG JC FG AB CF JG CD HJ AH BC DE EF BH DF
Wt. 1 2 2 4 4 6 7 7 8 8 9 10 11 14
BASIS FOR
PRIM’S ALGORITHM KRUSKAL’S ALGORITHM
COMPARISON
The algorithm obtains the minimum To obtain the minimum
spanning tree by choosing the spanning tree this algorithm
Basic
adjacent vertices from a set of select the edges from a set
selected vertices. of edges.
Differences Selection of the
Vertices Edges
Between route is based on
Connected All the graph components must be Disconnected graph may
Prim’s Algorithm components connected. present.
& Speed Better for the dense graph. Good for the sparse graph.
Kruskal’s Algorithm 2
Time complexity O(V ) O(E*log V)