Prims and kruskal algorithms

sagavalsalan3 3,843 views 52 slides Nov 01, 2016
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

Simple presentation for Prims and Kruskal Algorithms


Slide Content

Kruskal & Prims Algorithms Saga Valsalan www.SagaValsalan.blogspot.in www.YouTube.com/SagaValsalan www.Facebook.com/SagaValsalan

Minimum Spanning Trees (MST) A minimum spanning tree ( MST ) or minimum weight spanning tree is a spanning tree of a connected, undirected graph. It connects all the vertices together with the minimal total weighting for its edges. MST is a tree ,because it is acyclic A ny undirected graph has a minimum spanning forest , which is a union of minimum spanning trees for its connected components . If a graph has N vertices then the spanning tree will have N-1 edges Minimum Spanning Trees (MST)

Minimum Spanning Trees (MST) 2 19 9 1 5 13 17 25 14 8 21

Prims Algorithm Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. It finds a subset of the edges that forms a tree that includes every vertex , where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex.

Prims Algorithm Procedure: Initialize the min priority queue Q to contain all the vertices . Set the key of each vertex to ∞ and root’s key is set to zero Set the parent of root to NIL If weight of vertex is less than key value of the vertex, connect the graph. Repeat the process till all vertex are used.

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); Initialize the min priority queue Q to contain all the vertices. Set the key of each vertex to ∞ Set the parent of root to NIL Root’s key is set to 0 Until queue become null set Input– Graph, Weight, Root Set the parent of ‘v’ as ‘u’ Set the key of v = weight of edge connecting uv Prims Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); 14 10 3 6 4 5 2 9 15 8 Run on example graph Prims Algorithm

        14 10 3 6 4 5 2 9 15 8 Run on example graph MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

       14 10 3 6 4 5 2 9 15 8 Pick a start vertex r r MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); Prims Algorithm

       14 10 3 6 4 5 2 9 15 8 Black vertices have been removed from Q u Prims Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v );

     3  14 10 3 6 4 5 2 9 15 8 Black arrows indicate parent pointers u Prims Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v );

14     3  14 10 3 6 4 5 2 9 15 8 u Prims Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v );

14     3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

14   8  3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10   8  3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10   8  3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10 2  8  3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10 2  8 15 3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); Prims Algorithm

10 2  8 15 3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10 2 9 8 15 3  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

10 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

5 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

5 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

5 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); Prims Algorithm

5 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

5 2 9 8 15 3 4 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v); Prims Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin (Q); for each v  Adj [ u ] if (v  Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w( u,v ); 5 2 9 8 15 3 4 3 4 5 2 9 15 8 Prims Algorithm

Kruskals Algorithm Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm, adding increasing cost arcs at each step . This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. To visit all nodes, with minimum cost, without cycle formation

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } Kruskals Algorithm T-Tree E-Edge V-Vertices v-Vertex MakeSet (x): S = S U {{x}} Union(S i , S j ): S = S - {S i , S j } U {S i U S j } FindSet (X): return S i  S such that x  S i

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } Kruskals Algorithm

2 19 9 1? 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2? 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5? 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8? 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9? 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

2 19 9 1 5 13? 17 25 14 8 21 Run the algorithm: Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14? 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17? 25 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19? 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14 8 21? Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25? 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskals Algorithm

Kruskal () { T =  ; for each v  V MakeSet (v); sort E by increasing edge weight w for each ( u,v )  E (in sorted order) if FindSet (u)  FindSet (v) T = T U {{ u,v }}; Union( FindSet (u), FindSet (v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm: Kruskals Algorithm

Thank You