Prims & kruskal algorithms
-
Upload
ayesha-tahir -
Category
Engineering
-
view
87 -
download
4
Transcript of Prims & kruskal algorithms
![Page 1: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/1.jpg)
1
![Page 2: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/2.jpg)
Minimum Spanning TREEKruskal’s AlgorithmPrim’s Algorithm
![Page 3: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/3.jpg)
3What is a Spanning Tree
In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edges)
Use the fewest number of edges possible.
![Page 4: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/4.jpg)
4Spanning tree but
not MSTSpanning trees
![Page 5: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/5.jpg)
5What is a Spanning Tree
In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edge)
Use the fewest number of edges possible.
![Page 6: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/6.jpg)
6What is a Spanning Tree
In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edge)
Use the fewest number of edges possible.
If a loop can be found,then one of the edges is not necessary
![Page 7: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/7.jpg)
7What is a Spanning Tree
In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by an arc (edge)
Use the fewest number of edges possible.
You may think now, How many edges you have in a spanning tree given N vertices
![Page 8: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/8.jpg)
8Minimum Spanning Tree
The subset of edges E that connects all vertices V in the graph, and has minimum total weight.
a
b c
d
1
6
54
2
![Page 9: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/9.jpg)
9Minimum Spanning Tree
The subset of edges E that connects all vertices V in the graph, and has minimum total weight.
a
b c
d
1
6
54
2
MST = { (a,b),(a,d),(d,c) }
![Page 10: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/10.jpg)
10APPLICATIONS OF MST
Communication problem.Circuit diagram.T.V. cables.
![Page 11: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/11.jpg)
Kruskal’s AlgorithmTHE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
![Page 12: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/12.jpg)
12Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
Kruskal's algorithm utilizes Disjoint Sets (DS).
![Page 13: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/13.jpg)
13Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
Kruskal's algorithm utilizes Disjoint Sets (DS).
DS has two Operations Find and Union.
![Page 14: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/14.jpg)
14Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
Find: takes an item and pretend that this is a DS that item belongs to
Union: will merge two DS of its input items in to one DS
![Page 15: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/15.jpg)
15Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
A: is used to store all edges of the minimum spanning treeInitially A is emptyEventually return A
![Page 16: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/16.jpg)
16Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
for each vertex v of the graph, make a DS with v Each vertex will be used to create a single vertex
Disjoint Set
![Page 17: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/17.jpg)
17Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
Sort all edges by weight increasingly the edge of smallest weight will be in front and
the one with largest weight will be at the end
![Page 18: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/18.jpg)
18Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
1st we will take the edge with the smallest weight
C and f don’t belong to the same DS
![Page 19: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/19.jpg)
19Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),}
c and f will be merged in a one DS (c,f) is pushed to A
![Page 20: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/20.jpg)
20Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),}
![Page 21: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/21.jpg)
21Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),}
![Page 22: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/22.jpg)
22Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),}
c and d will be merged in a one DS
![Page 23: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/23.jpg)
23Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),}
c and d will be merged in a one DS
![Page 24: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/24.jpg)
24Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),}
![Page 25: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/25.jpg)
25Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),}
e and f belong to the same DS DO NOT MERGE !!
![Page 26: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/26.jpg)
26Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),(a,b)}
![Page 27: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/27.jpg)
27Kruskal’s AlgorithmKruskal(V,E)A = øforeach v V:
Make-disjoint-set(v)Sort E by weight
increasinglyforeach (v1,v2) E:
if Find(v1) ≠ Find(v2):A = A U {(v1,v2)}Union (v1,v2)
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),(a,b)}
Now we have a Spanning Tree that connects all vertices which have the minimum total weight
![Page 28: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/28.jpg)
28Kruskal’s Algorithm
![Page 29: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/29.jpg)
29Kruskal’s Algorithm
![Page 30: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/30.jpg)
Prim’s AlgorithmTHE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
![Page 31: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/31.jpg)
31Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
A: is used to store all edges of the minimum spanning treeInitially A is emptyEventually return A
![Page 32: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/32.jpg)
32Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
for each vertex v of the graph, Each vertex will have two attributes KEY and PARENT
KEY and PARENT initialized to ∞ and null Respectively
![Page 33: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/33.jpg)
33Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
a
2
cb d
f
4
63
4
15
e
2
A = { }
for each vertex v of the graph, Each vertex will have two attributes KEY and PARENT
KEY and PARENT initialized to ∞ and null Respectively
∞,null∞,null
∞,null
∞,null
∞,null
∞,null
![Page 34: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/34.jpg)
34Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Randomly choose a vertex as a ROOT r and set its KEY value to Zero
∞,null∞,null
∞,null
∞,null
0,null
∞,null
![Page 35: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/35.jpg)
35Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Create Q list which contains all vertices in the graph
∞,null∞,null
∞,null
∞,null
0,null
∞,null
Q = { a,b,c,d,e,f }
![Page 36: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/36.jpg)
36Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Find Q with the smallest (Minimum) KEY In this case, KEY[a] = 0 (Minimum KEY)
∞,null∞,null
∞,null
∞,null
0,null
∞,null
Q = { a,b,c,d,e,f }
![Page 37: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/37.jpg)
37Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Remove a from Q
∞,null∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
![Page 38: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/38.jpg)
38Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
PARENT [a] = null !! Skip the if statement
∞,null∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
![Page 39: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/39.jpg)
39Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Update attributes for all vertices Adjacent to a which are b and f
Starting with f , 2 < ∞
∞,null∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
![Page 40: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/40.jpg)
40Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { }
Update attributes for all vertices Adjacent to a which are b and f
Starting with f , 2 < ∞ PARENT [ f ] = a and KEY [ f ] = 2
∞,null∞,null
∞,null
2, a
0,null
4, a
Q = { b,c,d,e,f }
![Page 41: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/41.jpg)
41Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f) }
Now, Enter the Second Loop a ! Q u = min(Q) = f , Remove f from Q PARENT [ f ] ! = null, push (a,f) to A
∞,null∞,null
∞,null
2, a
0,null
4, a
Q = { b,c,d,e }
![Page 42: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/42.jpg)
42Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f) }
Update attributes for all vertices Adjacent to f which are c and d where weight < KEY
1, f∞,null
4, f
2, a
0,null
4, a
Q = { b,c,d,e }
![Page 43: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/43.jpg)
43Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f) }
Now, Enter the NEXT Loop u = min(Q) = c , Remove c from Q PARENT [ c ] ! = null, push (c,f) to A
1, f∞,null
4, f
2, a
0,null
4, a
Q = { b,d,e }
![Page 44: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/44.jpg)
44Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f) }
Update attributes for all vertices Adjacent to c where weight < KEY
1, f3, c
4, f
2, a
0,null
4, a
Q = { b,d,e }
![Page 45: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/45.jpg)
45Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d) }
Enter the NEXT Loop u = min(Q) = d , Remove d from Q PARENT [ d ] ! = null, push (c,d) to A
1, f3, c
4, f
2, a
0,null
4, a
Q = { b,e }
![Page 46: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/46.jpg)
46Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d) }
Update attributes for all vertices Adjacent to d where weight < KEY
1, f3, c
2, d
2, a
0,null
4, a
Q = { b,e }
![Page 47: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/47.jpg)
47Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e) }
Enter the NEXT Loop u = min(Q) = e , Remove e from Q PARENT [ e] ! = null, push (d,e) to A
1, f3, c
2, d
2, a
0,null
4, a
Q = { b }
![Page 48: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/48.jpg)
48Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e) }
No Updates for attributes because d and f ! Q
1, f3, c
2, d
2, a
0,null
4, a
Q = { b }
![Page 49: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/49.jpg)
49Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
Enter the NEXT Loop u = b , Remove b from Q PARENT [ b] ! = null, push (a,b) to A
1, f3, c
2, d
2, a
0,null
4, a
Q = { }
![Page 50: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/50.jpg)
50Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
No Updates for attributes because all neighbors of b doesn’t exist in Q
1, f3, c
2, d
2, a
0,null
4, a
Q = { }
![Page 51: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/51.jpg)
51Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
Enter the NEXT Loop, Q = ø Exit loop Return A
Q = { }
![Page 52: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/52.jpg)
52Prim’s AlgorithmA = øforeach v V:
KEY[v] = ∞PARENT[v] = null
KEY[r] = 0Q = V While Q != ø:
u = min (Q) by KEY valueQ = Q – uif PARENT(u) != null:A = A U (u, PARENT(u))foreach v Adj(u):if v Q and w(u,v) < KEY[v]:PARENT[v] = u KEY[v] = w
Return A
3
a
2
cb d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
Now we have a Spanning Tree that connects all vertices which have the minimum total weight
Q = { }
![Page 53: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/53.jpg)
53Prim’s Algorithm
![Page 54: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/54.jpg)
54Prim’s Algorithm The time complexity of the algorithm is computed as follows: Step 1 costs q(n) time. The for loop in Step 2 requires Ø(n) time. The time taken by steps 3 - 6 is O(n). The time taken by Step 10 to search for a vertex y closest to X is Ø(n) per
iteration. This is because the algorithm inspects each entry in the vector representing the set Y. Since it is executed n-1 times, the overall time required by Step 10 is Ø(n2).
Steps 11,12 and 13 cost Ø(1) time per iteration for a total of q(n) time. The for loop in Step 14 is executed 2m times, where m=|E|. This is because
each edge (y, w) is inspected twice: once when y is moved to X and the other when w is moved to X.
Hence, the overall time required by step 14 is Ø(m). The test in Step 15 is executed exactly m times, and Steps 16 and 17 are
executed at most m times. Thus, Steps 14 to 19 cost q(m) time. It follows that the time complexity of the algorithm is Ø(m + n2) = Ø (n2)
![Page 55: Prims & kruskal algorithms](https://reader038.fdocument.pub/reader038/viewer/2022103010/58ce5ba01a28ab2f268b4b47/html5/thumbnails/55.jpg)
THANKS