SlideShare a Scribd company logo
Kruskal’s Algorithm for Computing MSTs
Presented by: Raj Kumar Ranabhat
M.E in Computer Engineering(I/I)
Kathmandu University
Tree
A tree is a graph with the following properties:
• The graph is connected (can go from anywhere to anywhere)
• There are no cycles(acyclic)
Graphs that are not treesTree
Minimum Spanning Tree (MST)
3
• T is a tree (i.e., it is acyclic)
• T covers all the vertices V
• contains |V| - 1 edges
• T has minimum total weight
• A single graph can have many different spanning trees.
Let G=(V,E) be an undirected connected graph.
A sub graph T=(V,E’) of G is a spanning tree of G if :-
Connected undirected graph Spanning Tree
Kruskal’s Algorithm
• It is a algorithm used to find a minimum cost spanning tree for connected weighted
undirected graph
• This algorithm first appeared in Proceedings of the American Mathematical Society
in 1956, and was written by Joseph Kruskal
A B
C D
A B
C D
Connected Weighted
1
5
3
4 26
1
3
2
• It's a spanning tree because it connects all vertices
without loops.
• Tree weight is minimum of all possibilities hence
minimum cost spanning tree
Disjoint Sets
6
• A disjoint set is a data structure which keeps track of all elements that are separated by
a number of disjoint (not connected) subsets.
• It supports three useful operations
• MAKE-SET(x): Make a new set with a single element x
• UNION (S1,S2): Merge the set S1 and set S2
• FIND-SET(x): Find the set containing the element x
Disjoint Sets
7
• A disjoint set is a data structure which keeps track of all elements that are separated by
a number of disjoint (not connected) subsets.
• It supports three useful operations
• MAKE-SET(x): Make a new set with a single element x
• UNION (S1,S2): Merge the set S1 and set S2
• FIND-SET(x): Find the set containing the element x
1 2 3
MAKE-SET(1), MAKE-SET(2), MAKE-
SET(3) creates new set S1,S2,S3
S1 S2 S3
Disjoint Sets
8
• A disjoint set is a data structure which keeps track of all elements that are separated by
a number of disjoint (not connected) subsets.
• It supports three useful operations
• MAKE-SET(x): Make a new set with a single element x
• UNION (S1,S2): Merge the set S1 and set S2
• FIND-SET(x): Find the set containing the element x
1 2
UNION (1,2) merge set S1 and set S2
to create set S4
1 2 3
S1 S2 S3 S4
MAKE-SET(1), MAKE-SET(2), MAKE-
SET(3) creates new set S1,S2,S3
Disjoint Sets
9
• A disjoint set is a data structure which keeps track of all elements that are separated by
a number of disjoint (not connected) subsets
• It supports three useful operations
• MAKE-SET(x): Make a new set with a single element x
• UNION (S1,S2): Merge the set S1 and set S2
• FIND-SET(x): Find the set containing the element x
1 2
UNION (1,2) merge set S1 and set S2
to create set S4
1 2 3
S1 S2 S3 S4
FIND-SET(2) returns set S4
1 2
S4
MAKE-SET(1), MAKE-
SET(2), MAKE-SET(3) creates
new set S1,S2,S3
KRUSKAL(V,E):
A = ∅
foreach 𝑣∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Kruskal’s Algorithm
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
AB 4
BC 6
CD 3
DE 2
EF 4
AF 2
BF 5
CF 1
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
AB 4
BC 6
CD 3
DE 2
EF 4
AF 2
BF 5
CF 1
A ={ }
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
AB 4
BC 6
CD 3
DE 2
EF 4
AF 2
BF 5
CF 1
A B C
A ={ }
F DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A B C
F DE
A ={ }
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
A ={ }
A B C
F DE
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A ={ }
A B C
F DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A ={(C,F)}
A B C
F DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A ={(C,F)}
A B
DE
C,F
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
C
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A ={(C,F)}
A B
DE
C,F
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
F
D
E
4
6 3
2
4
2
5 1
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
C
A ={(C,F),(A,F)}
A B
DE
C,F
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A
B
F
D
E
4
6 3
2
4
2
5 1
A,C,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
C
A ={(C,F),(A,F)}
B
DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
B D
E
4
6 3
2
4
2
5 1
A
F
C
A ={(C,F),(A,F)}
A,C,FB
DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
B
4
6 3
2
4
2
5 1
D
EA
F
C
A ={(C,F),(A,F),(D,E)}
A,C,FB
DE
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
D,E
B
4
6 3
2
4
2
5 1
D
EA
F
C
A ={(C,F),(A,F),(D,E)}
A,C,FB
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
B
4
6 3
2
4
2
5 1
A
C
F
D
E
A ={(C,F),(A,F),(D,E)}
D,E
A,C,FB
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
B
4
6 3
2
4
2
5 1
A
C
F
D
E
A ={(C,F),(A,F),(D,E),
(C,D)}
D,E
A,C,FB
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A,C,D,E,F
B
4
6 3
2
4
2
5 1
A
C
F
D
E
A ={(C,F),(A,F),(D,E),
(C,D)}
B
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
B
4
6 3
2
4
2
5 1
A
C
F
D
E
A ={(C,F),(A,F),(D,E),
(C,D)}
A,C,D,E,FB
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
6 3
2
4
2
5 1
B
A
C
F
D
E
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,C,D,E,FB
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
6 3
2
4
2
5 1
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
6 3
2
4
2
5 1
A
B
C
F
D
E
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
F
E
4
6 3
2
4
2
5 1
Is in a same set.
A
B
C
D
It is cyclic
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
6 3
2
2
5 1
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
4
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
6 3
2
2
5 1
F
EA
B
C
DKRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
6 3
2
2
5 1
Is in a same set.
It is cyclic
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
6 3
2
2
1
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
5
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
6 3
2
2
1
F
EA
B
C
DKRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
6 3
2
2
1
It is cyclic
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
Is in a same set.
A,B,C,D,E,F
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
A
B
C
F
D
E
4
3
2
2
1
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
A,B,C,D,E,F
6
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
3
2
2
1
F
EA
B
C
DKRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
A ={(C,F),(A,F),(D,E),
(C,D),(A,B)}
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Edges Weight
CF 1
AF 2
DE 2
CD 3
AB 4
FE 4
BF 5
BC 6
4
3
2
2
1
Total Weight :- 1 + 2 + 2 + 3 + 4
= 12
F
EA
B
C
D
A ={(C,F),(A,F),(D,E),(C,D),(A,B)}
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Time Complexity
O(1)
O(V)
O(E log E)
O(E log V)
KRUSKAL(V,E):
A = ∅
foreach 𝑣 ∈ V:
MAKE-SET(𝑣)
Sort E by weight increasingly
foreach (𝑣1,𝑣2) ∈ E:
if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2):
A = A ∪ {(𝑣1,𝑣2)}
UNION(𝑣1,𝑣2)
else
Remove edge (𝑣1,𝑣2)
return A
Time Complexity
O(1)
O(V)
O(E log E)
Time Complexity = O(1)+ O(V)+O(E log E)+O(E log V)
= O(E log E) + O(E log V)
Since, |E| ≤ |V|2 ⇒log |E| = O(2 log V) = O(log V).
= O(E log V) + O(E log V)
= O(E log V)
O(E log V)
Real-life applications of Kruskal's algorithm
• Landing Cables
• TV Network
• Tour Operations
• Computer networking
• Study of Molecular bonds in Chemistry
• Routing Algorithms
Problem: Laying Telephone Wire
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers

More Related Content

PPTX
Green cloud computing
PPTX
Kruskal’s algorithm
PPTX
Green cloud computing
PPT
Java interfaces
PPTX
Knapsack Problem (DP & GREEDY)
PPTX
Introduction to GCP presentation
PDF
Introduction to NumPy (PyData SV 2013)
DOCX
Packet tracer practical guide
Green cloud computing
Kruskal’s algorithm
Green cloud computing
Java interfaces
Knapsack Problem (DP & GREEDY)
Introduction to GCP presentation
Introduction to NumPy (PyData SV 2013)
Packet tracer practical guide

What's hot (20)

PPTX
Kruskal Algorithm
PPTX
Prims and kruskal algorithms
PPTX
A presentation on prim's and kruskal's algorithm
PPTX
Minimum spanning tree (mst)
PPT
Minimum spanning tree
PDF
Minimum spanning tree
PPT
Spanning trees
PPTX
Dijkstra’s algorithm
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
PPT
The Floyd–Warshall algorithm
PPTX
Kruskal’s Algorithm
PPTX
Shortest path algorithm
PPTX
All pair shortest path
PPTX
Strassen's matrix multiplication
PPT
Bellman Ford's Algorithm
PPTX
Asymptotic Notation
PDF
All pairs shortest path algorithm
PPTX
Bellman ford algorithm
PPT
Graph coloring problem
PPTX
Bellman ford Algorithm
Kruskal Algorithm
Prims and kruskal algorithms
A presentation on prim's and kruskal's algorithm
Minimum spanning tree (mst)
Minimum spanning tree
Minimum spanning tree
Spanning trees
Dijkstra’s algorithm
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
The Floyd–Warshall algorithm
Kruskal’s Algorithm
Shortest path algorithm
All pair shortest path
Strassen's matrix multiplication
Bellman Ford's Algorithm
Asymptotic Notation
All pairs shortest path algorithm
Bellman ford algorithm
Graph coloring problem
Bellman ford Algorithm
Ad

Similar to Kruskal's algorithm (20)

PPT
kruskal Algorithm in Algorithm for CS St
PPTX
Prims & kruskal algorithms
PDF
Abstract Algebra Lecture for Mathematics major
PPTX
Minimum spanning tree algorithms by ibrahim_alfayoumi
PPT
minimum spanning trees Algorithm
PPT
lecture 22
PPT
Ch01-2.ppt
PPTX
Finite Maths Problems
PDF
SETS - Vedantu.pdf
PPTX
Algorithm analysis Greedy method in algorithm.pptx
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PDF
2 》set operation.pdf
PDF
Homework 2 sol
PDF
Capitulo 1, 7ma edición
PDF
Elliptic Curves
PPTX
PDF
Bellman ford
PPT
Mtk3013 chapter 2-3
PPTX
35 tangent and arc length in polar coordinates
PDF
01_Sets.pdf
kruskal Algorithm in Algorithm for CS St
Prims & kruskal algorithms
Abstract Algebra Lecture for Mathematics major
Minimum spanning tree algorithms by ibrahim_alfayoumi
minimum spanning trees Algorithm
lecture 22
Ch01-2.ppt
Finite Maths Problems
SETS - Vedantu.pdf
Algorithm analysis Greedy method in algorithm.pptx
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
2 》set operation.pdf
Homework 2 sol
Capitulo 1, 7ma edición
Elliptic Curves
Bellman ford
Mtk3013 chapter 2-3
35 tangent and arc length in polar coordinates
01_Sets.pdf
Ad

More from Raj Kumar Ranabhat (9)

PPTX
AI Revolutionizing the Salesforce Developer's Day
PPTX
Sales forcedemo
PPTX
Sales force
PPTX
From data mining to knowledge discovery in
PPTX
PPTX
The Adoption of Knowledge Management Systems in Small Firms
PPTX
Visual notation
PPTX
Take-Grant Protection Model
PPT
Visual Notation
AI Revolutionizing the Salesforce Developer's Day
Sales forcedemo
Sales force
From data mining to knowledge discovery in
The Adoption of Knowledge Management Systems in Small Firms
Visual notation
Take-Grant Protection Model
Visual Notation

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Supply Chain Operations Speaking Notes -ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Kruskal's algorithm

  • 1. Kruskal’s Algorithm for Computing MSTs Presented by: Raj Kumar Ranabhat M.E in Computer Engineering(I/I) Kathmandu University
  • 2. Tree A tree is a graph with the following properties: • The graph is connected (can go from anywhere to anywhere) • There are no cycles(acyclic) Graphs that are not treesTree
  • 3. Minimum Spanning Tree (MST) 3 • T is a tree (i.e., it is acyclic) • T covers all the vertices V • contains |V| - 1 edges • T has minimum total weight • A single graph can have many different spanning trees. Let G=(V,E) be an undirected connected graph. A sub graph T=(V,E’) of G is a spanning tree of G if :-
  • 5. Kruskal’s Algorithm • It is a algorithm used to find a minimum cost spanning tree for connected weighted undirected graph • This algorithm first appeared in Proceedings of the American Mathematical Society in 1956, and was written by Joseph Kruskal A B C D A B C D Connected Weighted 1 5 3 4 26 1 3 2 • It's a spanning tree because it connects all vertices without loops. • Tree weight is minimum of all possibilities hence minimum cost spanning tree
  • 6. Disjoint Sets 6 • A disjoint set is a data structure which keeps track of all elements that are separated by a number of disjoint (not connected) subsets. • It supports three useful operations • MAKE-SET(x): Make a new set with a single element x • UNION (S1,S2): Merge the set S1 and set S2 • FIND-SET(x): Find the set containing the element x
  • 7. Disjoint Sets 7 • A disjoint set is a data structure which keeps track of all elements that are separated by a number of disjoint (not connected) subsets. • It supports three useful operations • MAKE-SET(x): Make a new set with a single element x • UNION (S1,S2): Merge the set S1 and set S2 • FIND-SET(x): Find the set containing the element x 1 2 3 MAKE-SET(1), MAKE-SET(2), MAKE- SET(3) creates new set S1,S2,S3 S1 S2 S3
  • 8. Disjoint Sets 8 • A disjoint set is a data structure which keeps track of all elements that are separated by a number of disjoint (not connected) subsets. • It supports three useful operations • MAKE-SET(x): Make a new set with a single element x • UNION (S1,S2): Merge the set S1 and set S2 • FIND-SET(x): Find the set containing the element x 1 2 UNION (1,2) merge set S1 and set S2 to create set S4 1 2 3 S1 S2 S3 S4 MAKE-SET(1), MAKE-SET(2), MAKE- SET(3) creates new set S1,S2,S3
  • 9. Disjoint Sets 9 • A disjoint set is a data structure which keeps track of all elements that are separated by a number of disjoint (not connected) subsets • It supports three useful operations • MAKE-SET(x): Make a new set with a single element x • UNION (S1,S2): Merge the set S1 and set S2 • FIND-SET(x): Find the set containing the element x 1 2 UNION (1,2) merge set S1 and set S2 to create set S4 1 2 3 S1 S2 S3 S4 FIND-SET(2) returns set S4 1 2 S4 MAKE-SET(1), MAKE- SET(2), MAKE-SET(3) creates new set S1,S2,S3
  • 10. KRUSKAL(V,E): A = ∅ foreach 𝑣∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Kruskal’s Algorithm
  • 11. A B C F D E 4 6 3 2 4 2 5 1 Edges Weight AB 4 BC 6 CD 3 DE 2 EF 4 AF 2 BF 5 CF 1 KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A
  • 12. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight AB 4 BC 6 CD 3 DE 2 EF 4 AF 2 BF 5 CF 1 A ={ }
  • 13. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight AB 4 BC 6 CD 3 DE 2 EF 4 AF 2 BF 5 CF 1 A B C A ={ } F DE
  • 14. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F DE A ={ }
  • 15. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 A ={ } A B C F DE Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6
  • 16. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A ={ } A B C F DE
  • 17. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A ={(C,F)} A B C F DE
  • 18. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A ={(C,F)} A B DE C,F
  • 19. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B C F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A ={(C,F)} A B DE C,F
  • 20. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B F D E 4 6 3 2 4 2 5 1 Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 C A ={(C,F),(A,F)} A B DE C,F
  • 21. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A B F D E 4 6 3 2 4 2 5 1 A,C,F Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 C A ={(C,F),(A,F)} B DE
  • 22. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 B D E 4 6 3 2 4 2 5 1 A F C A ={(C,F),(A,F)} A,C,FB DE
  • 23. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 B 4 6 3 2 4 2 5 1 D EA F C A ={(C,F),(A,F),(D,E)} A,C,FB DE
  • 24. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 D,E B 4 6 3 2 4 2 5 1 D EA F C A ={(C,F),(A,F),(D,E)} A,C,FB
  • 25. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 B 4 6 3 2 4 2 5 1 A C F D E A ={(C,F),(A,F),(D,E)} D,E A,C,FB
  • 26. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 B 4 6 3 2 4 2 5 1 A C F D E A ={(C,F),(A,F),(D,E), (C,D)} D,E A,C,FB
  • 27. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A,C,D,E,F B 4 6 3 2 4 2 5 1 A C F D E A ={(C,F),(A,F),(D,E), (C,D)} B
  • 28. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 B 4 6 3 2 4 2 5 1 A C F D E A ={(C,F),(A,F),(D,E), (C,D)} A,C,D,E,FB
  • 29. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 6 3 2 4 2 5 1 B A C F D E A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,C,D,E,FB
  • 30. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 6 3 2 4 2 5 1 KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 31. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 6 3 2 4 2 5 1 A B C F D E KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 32. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 F E 4 6 3 2 4 2 5 1 Is in a same set. A B C D It is cyclic KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 33. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 6 3 2 2 5 1 KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F 4
  • 34. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 6 3 2 2 5 1 F EA B C DKRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 35. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 6 3 2 2 5 1 Is in a same set. It is cyclic A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 36. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 6 3 2 2 1 KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F 5
  • 37. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 6 3 2 2 1 F EA B C DKRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F
  • 38. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 6 3 2 2 1 It is cyclic A ={(C,F),(A,F),(D,E), (C,D),(A,B)} Is in a same set. A,B,C,D,E,F
  • 39. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 A B C F D E 4 3 2 2 1 KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)} A,B,C,D,E,F 6
  • 40. Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 3 2 2 1 F EA B C DKRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A A ={(C,F),(A,F),(D,E), (C,D),(A,B)}
  • 41. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Edges Weight CF 1 AF 2 DE 2 CD 3 AB 4 FE 4 BF 5 BC 6 4 3 2 2 1 Total Weight :- 1 + 2 + 2 + 3 + 4 = 12 F EA B C D A ={(C,F),(A,F),(D,E),(C,D),(A,B)}
  • 42. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Time Complexity O(1) O(V) O(E log E) O(E log V)
  • 43. KRUSKAL(V,E): A = ∅ foreach 𝑣 ∈ V: MAKE-SET(𝑣) Sort E by weight increasingly foreach (𝑣1,𝑣2) ∈ E: if FIND-SET(𝑣1) ≠ FIND-SET(𝑣2): A = A ∪ {(𝑣1,𝑣2)} UNION(𝑣1,𝑣2) else Remove edge (𝑣1,𝑣2) return A Time Complexity O(1) O(V) O(E log E) Time Complexity = O(1)+ O(V)+O(E log E)+O(E log V) = O(E log E) + O(E log V) Since, |E| ≤ |V|2 ⇒log |E| = O(2 log V) = O(log V). = O(E log V) + O(E log V) = O(E log V) O(E log V)
  • 44. Real-life applications of Kruskal's algorithm • Landing Cables • TV Network • Tour Operations • Computer networking • Study of Molecular bonds in Chemistry • Routing Algorithms
  • 45. Problem: Laying Telephone Wire Central office
  • 46. Wiring: Naïve Approach Central office Expensive!
  • 47. Wiring: Better Approach Central office Minimize the total length of wire connecting the customers