Professional Documents
Culture Documents
Test PDF
Test PDF
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 1 / 27
Table of Contents
Introduction
Overview
Algorithm
Example
Complexity
Application
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 2 / 27
Introduction
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 3 / 27
Introduction
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 3 / 27
Introduction
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 3 / 27
Overview of Kruskal Algorithm:
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 4 / 27
Overview of Kruskal Algorithm:
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 4 / 27
Overview of Kruskal Algorithm:
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 4 / 27
Overview of Kruskal Algorithm:
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 4 / 27
Overview of Kruskal Algorithm:
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 4 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
5 for each edge (u, v ) ∈ E [G ], taken in non decreasing order by weight
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
5 for each edge (u, v ) ∈ E [G ], taken in non decreasing order by weight
6 if FIND − SET (u) 6= FIND − SET (v )
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
5 for each edge (u, v ) ∈ E [G ], taken in non decreasing order by weight
6 if FIND − SET (u) 6= FIND − SET (v )
7 then A = A ∪ {(u, v )}
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
5 for each edge (u, v ) ∈ E [G ], taken in non decreasing order by weight
6 if FIND − SET (u) 6= FIND − SET (v )
7 then A = A ∪ {(u, v )}
8 UNION(u, v )
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
Algorithm: MST-KRUSKAL (G, w)
1 A=Φ
2 for each vertex v ∈ V [G ]
3 MAKE − SET (v )
4 sort the edges of E [G ] into non decreasing order by weight w
5 for each edge (u, v ) ∈ E [G ], taken in non decreasing order by weight
6 if FIND − SET (u) 6= FIND − SET (v )
7 then A = A ∪ {(u, v )}
8 UNION(u, v )
9 return A
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 5 / 27
How Kruskal Algorithm works
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 6 / 27
How Kruskal Algorithm works
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 6 / 27
How Kruskal Algorithm works
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 6 / 27
How Kruskal Algorithm works
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 6 / 27
Example of Kruskal Algorithm
5
B D 4
2 1 3
2
8 A C
12
Given graph
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 7 / 27
Example of Kruskal Algorithm
5
B D 4 ←− Loop
2 1 3
2
Loop −→ 8 A C
12
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 8 / 27
Example of Kruskal Algorithm
5
B D 4 ←− Loop
2 1 3
2
Loop −→ 8 A C
12
Note
Any edge that starts and ends at the same vertex is called a Loop.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 8 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
12
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 9 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
12
Step 2: Remove all parallel edges between two vertices except the one
with the least weight
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 9 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
12
Step 2: Remove all parallel edges between two vertices except the one
with the least weight
Note
Remove edge with weight 12 and keep 2 between A and C.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 9 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 10 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 10 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 10 / 27
Example of Kruskal Algorithm
Make all vertex as one one set by using MakeSet(), {A}, {B}, {C }, {D}
5
B D
2 1 3
2
A C
EDGE
WEIGHT
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 11 / 27
Example of Kruskal Algorithm
Make all vertex as one one set by using MakeSet(), {A}, {B}, {C }, {D}
5
B D
2 1 3
2
A C
EDGE
WEIGHT
Fill the 1st column of the table with the edge with minimum weight.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 11 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC
WEIGHT 1
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 12 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC
WEIGHT 1
Select the edge of the next minimum weight and include it into the edge
table.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 12 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC
WEIGHT 1
Select the edge of the next minimum weight and include it into the edge
table.
Note
In case we have two or more edges with the same weight then we select
them in any order.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 12 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC
WEIGHT 1 2 2
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 13 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC
WEIGHT 1 2 2
Again select the edge of the next minimum weight and include it into the
edge table.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 13 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC
WEIGHT 1 2 2
Again select the edge of the next minimum weight and include it into the
edge table.
Continue this process until all the edges are filled in the edge table.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 13 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC DC
WEIGHT 1 2 2 3
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 14 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 15 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Now we can find the minimum spanning tree from the edge table.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 16 / 27
Example of Kruskal Algorithm
5
B D
2 1 3
2
A C
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Now we can find the minimum spanning tree from the edge table.
Note
The graph has 4 vertices, so the minimum spanning tree will have 3 edges.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 16 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 17 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Step 4: To find the MST (Minimum Spanning Tree) we will start from the
smallest weight edge and keep selecting edges that does not form any
circuit with the previously selected edges.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 17 / 27
Example of Kruskal Algorithm:
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 18 / 27
Example of Kruskal Algorithm:
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 18 / 27
Example of Kruskal Algorithm:
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
B D
1
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 18 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 19 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge AB has the next smallest weight 2 and it doesn’t form a circuit
with previously selected edges, so we will select AB. So, A = {A, B, C }
and Sets are {A, B, C},{D}
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 19 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge AB has the next smallest weight 2 and it doesn’t form a circuit
with previously selected edges, so we will select AB. So, A = {A, B, C }
and Sets are {A, B, C},{D}
B D
2 1
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 19 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 20 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge AC has the next smallest weight 2 and it forms a circuit with
previously selected edges, so we will not select AC.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 20 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge AC has the next smallest weight 2 and it forms a circuit with
previously selected edges, so we will not select AC.
B D
2 1
2
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 20 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge AC has the next smallest weight 2 and it forms a circuit with
previously selected edges, so we will not select AC.
B D
2 1
2
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 21 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 22 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge DC has the next smallest weight 3 and it doesn’t form a circuit
with previously selected edges, so we will select DC. So, A = {A, B, C , D}
and Sets are {A, B, C, D}
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 22 / 27
Example of Kruskal Algorithm
EDGE BC AB AC DC BD
WEIGHT 1 2 2 3 5
The edge DC has the next smallest weight 3 and it doesn’t form a circuit
with previously selected edges, so we will select DC. So, A = {A, B, C , D}
and Sets are {A, B, C, D}
B D
2 1 3
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 22 / 27
Example of Kruskal Algorithm
Since we have 3 edges connecting all the 4 vertices of the graph we will
stop now.
B D
2 1 3
A C
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 23 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Complexity Analysis
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 24 / 27
Applications
Artificial Intelligence
Game Development
Cognitive Science
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 25 / 27
References
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 26 / 27
The End
Thank You
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar)Kruskal Algorithm April 12, 2017 27 / 27