![Page 1: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/1.jpg)
A PRESENTATION ON PRIM’S AND KRUSKAL’S ALGORITHM
By:Gaurav Vivek Kolekar andLionel Sequeira
![Page 2: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/2.jpg)
GRAPHS AND IT’S EXAMPLES
![Page 3: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/3.jpg)
MINIMUM COST SPANNING TREE
A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight
![Page 4: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/4.jpg)
ALGORITHM CHARACTERISTICS• Both Prim’s and Kruskal’s Algorithms work with
undirected graphs• Both work with weighted and unweighted graphs• Both are greedy algorithms that produce optimal
solutions
![Page 5: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/5.jpg)
Kruskal’s algorithm
1. Select the shortest edge in a network
2. Select the next shortest edge which does not create a cycle
3. Repeat step 2 until all vertices have been connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest edge connected to that vertex
3. Select the shortest edge connected to any vertex already connected
4. Repeat step 3 until all vertices have been connected
![Page 6: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/6.jpg)
Work with edges, instead of nodes of a graphTwo steps:
– Sort edges by increasing edge weight– Select the first |V| – 1 edges that do not
generate a cycle
KRUSKAL’S ALOGORITHM
![Page 7: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/7.jpg)
Consider an undirected, weight graph
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10
WALK-THROUGH
![Page 8: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/8.jpg)
Sort the edges by increasing edge weight
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 9: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/9.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 10: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/10.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 11: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/11.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
![Page 12: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/12.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 13: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/13.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 14: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/14.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 15: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/15.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 16: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/16.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 17: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/17.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 18: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/18.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 19: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/19.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G 3
24
63
4
3
48
4
3
10 edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
![Page 20: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/20.jpg)
Select first |V|–1 edges which do not generate a cycle
edge dv(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
HB
F
E
D
C
G2
3
3
3
edge dv(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
DoneTotal Cost = dv = 21
4
}not considered
![Page 21: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/21.jpg)
PRIM’S ALGORITHM
![Page 22: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/22.jpg)
WALK-THROUGHInitialize array
K dv pvA F
B F
C F
D F
E F
F F
G F
H F
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
2
![Page 23: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/23.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Start with any node, say D
K dv pvA
B
C
D T 0
E
F
G
H
2
![Page 24: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/24.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA
B
C 3 D
D T 0
E 25 D
F 18 D
G 2 D
H
2
![Page 25: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/25.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA
B
C 3 D
D T 0
E 25 D
F 18 D
G T 2 D
H
2
![Page 26: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/26.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA
B
C 3 D
D T 0
E 7 G
F 18 D
G T 2 D
H 3 G
2
![Page 27: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/27.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA
B
C T 3 D
D T 0
E 7 G
F 18 D
G T 2 D
H 3 G
2
![Page 28: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/28.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA
B 4 C
C T 3 D
D T 0
E 7 G
F 3 C
G T 2 D
H 3 G
2
![Page 29: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/29.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA
B 4 C
C T 3 D
D T 0
E 7 G
F T 3 C
G T 2 D
H 3 G
2
![Page 30: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/30.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA 10 F
B 4 C
C T 3 D
D T 0
E 2 F
F T 3 C
G T 2 D
H 3 G
2
![Page 31: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/31.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
![Page 32: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/32.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Table entries unchanged
![Page 33: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/33.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
![Page 34: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/34.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
![Page 35: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/35.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA T 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
![Page 36: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/36.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Update distances of adjacent, unselected nodes
K dv pvA T 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Table entries unchanged
![Page 37: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/37.jpg)
4
25
A
HB
F
E
D
C
G 7
210
183
4
3
78
9
3
10
Select node with minimum distance
K dv pvA T 4 H
B T 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
![Page 38: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/38.jpg)
4
A
HB
F
E
D
C
G2
34
3
3
Cost of Minimum Spanning Tree = dv = 21
K dv pvA T 4 H
B T 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
![Page 39: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/39.jpg)
DIFFERENCE BETWEEN PRIM’S AND KRUSKAL’S ALGORITHM• The difference between Prim’s algorithm and Kruskal’s
algorithm is that the set of selected edges forms a tree at all times when using Prim’s algorithm while a forest is formed when using Kruskal’s algorithm.
• In Prim’s algorithm, a least-cost edge (u, v) is added to T such that T∪ {(u, v)} is also a tree. This repeats until T contains n-1 edges.
![Page 40: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/40.jpg)
•Both algorithms will always give solutions with the same length.
•They will usually select edges in a different order.
•Occasionally they will use different edges – this may happen when you have to choose between edges with the same length.
SOME POINTS TO NOTE
![Page 41: A presentation on prim's and kruskal's algorithm](https://reader037.vdocument.in/reader037/viewer/2022102505/58ac4a731a28ab99028b5c37/html5/thumbnails/41.jpg)
READINGS