SlideShare a Scribd company logo
Saga Valsalan
www.SagaValsalan.blogspot.in
www.YouTube.com/SagaValsalan
www.Facebook.com/SagaValsalan
Minimum SpanningTrees (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
• Any 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 SpanningTrees (MST)
Minimum SpanningTrees (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
  
0  


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
  
0  


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);
  
0  
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  
0  
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  
0  
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  
0 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  
0 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  
0 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 
0 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 
0 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 
0 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
0 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
0 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
0 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
0 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
0 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
0 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
0 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
0 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(Si, Sj): S = S - {Si, Sj} U {Si U Sj}
FindSet(X): return Si  S such that x  Si
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
ThankYou

More Related Content

PPT
Graph traversal-BFS & DFS
PPT
Minimum spanning tree
PDF
Minimum spanning tree
PPTX
Dijkstra’S Algorithm
PPTX
Prim's algorithm
PPT
Web servers
PPTX
Dijkstra's Algorithm
Graph traversal-BFS & DFS
Minimum spanning tree
Minimum spanning tree
Dijkstra’S Algorithm
Prim's algorithm
Web servers
Dijkstra's Algorithm

What's hot (20)

PPTX
Kruskal Algorithm
PPTX
Topological Sorting
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
PPT
Spanning trees
PPT
Prim's Algorithm on minimum spanning tree
PPT
minimum spanning trees Algorithm
PPTX
Minimum spanning Tree
PPTX
PRIM'S ALGORITHM
PDF
All pairs shortest path algorithm
PPTX
Bellman ford Algorithm
PPTX
Kruskal's algorithm
PPT
Graph algorithms
PPTX
Tree traversal techniques
PPTX
Minimum Spanning Tree
PPTX
Data structure - Graph
PPT
Prim Algorithm and kruskal algorithm
PPT
Asymptotic notation
PPT
Graph coloring problem
PPT
Single source stortest path bellman ford and dijkstra
PPTX
A presentation on prim's and kruskal's algorithm
Kruskal Algorithm
Topological Sorting
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Spanning trees
Prim's Algorithm on minimum spanning tree
minimum spanning trees Algorithm
Minimum spanning Tree
PRIM'S ALGORITHM
All pairs shortest path algorithm
Bellman ford Algorithm
Kruskal's algorithm
Graph algorithms
Tree traversal techniques
Minimum Spanning Tree
Data structure - Graph
Prim Algorithm and kruskal algorithm
Asymptotic notation
Graph coloring problem
Single source stortest path bellman ford and dijkstra
A presentation on prim's and kruskal's algorithm
Ad

Viewers also liked (19)

PPTX
Kruskal & Prim's Algorithm
PPTX
Kruskal Algorithm
PPT
KRUSKAL'S algorithm from chaitra
PPTX
Minimum spanning tree algorithms by ibrahim_alfayoumi
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PPTX
カタカナの練習
PDF
New Tech for Your Boat
PDF
hydraces
PPTX
RLAS116 test
PDF
Newsletter2 web
PDF
Newsletter_2
PDF
Veg choice-menu-standard
PDF
Gail's Brochure and Preview of Her Work
PDF
Depression - Nobody Understands Until It Happens To Them
PDF
Beating Depression Breakthrough
PPTX
орхидея Убизский
PDF
Mang Quang cao Admarket display network admicro
PPT
презентация!\
PPTX
Ici presentation
Kruskal & Prim's Algorithm
Kruskal Algorithm
KRUSKAL'S algorithm from chaitra
Minimum spanning tree algorithms by ibrahim_alfayoumi
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
カタカナの練習
New Tech for Your Boat
hydraces
RLAS116 test
Newsletter2 web
Newsletter_2
Veg choice-menu-standard
Gail's Brochure and Preview of Her Work
Depression - Nobody Understands Until It Happens To Them
Beating Depression Breakthrough
орхидея Убизский
Mang Quang cao Admarket display network admicro
презентация!\
Ici presentation
Ad

Similar to Prims and kruskal algorithms (18)

PPT
Lec-35Graph - Copy Graph therory in Data strucure
PPT
lecture 20
PPT
Graphs > Discrete structures , Data Structures & Algorithums
PDF
algoritm prim kruskal review time complexity .pdf
PPT
prims algorithm for algorithms for cs st
PPT
lecture 22
PPT
Unit 5 session 2 MinimumSpanningTrees.ppt
PPTX
lec6.pptx
PPTX
Prims & kruskal algorithms
PDF
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
PPT
lecture 21
PPT
algorthm analysis from computer scince.ppt
PDF
Shortest Path in Graph
PDF
Algorithms explained
PDF
Control as Inference (強化学習とベイズ統計)
PPT
Greedy Approach in Design Analysis and Algorithms
PPT
Maximum clique detection algorithm
PPT
Unit27_MinimumSpanningTree.ppt data structure programming
Lec-35Graph - Copy Graph therory in Data strucure
lecture 20
Graphs > Discrete structures , Data Structures & Algorithums
algoritm prim kruskal review time complexity .pdf
prims algorithm for algorithms for cs st
lecture 22
Unit 5 session 2 MinimumSpanningTrees.ppt
lec6.pptx
Prims & kruskal algorithms
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
lecture 21
algorthm analysis from computer scince.ppt
Shortest Path in Graph
Algorithms explained
Control as Inference (強化学習とベイズ統計)
Greedy Approach in Design Analysis and Algorithms
Maximum clique detection algorithm
Unit27_MinimumSpanningTree.ppt data structure programming

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Classroom Observation Tools for Teachers
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharma ospi slides which help in ospi learning
PDF
Pre independence Education in Inndia.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
Classroom Observation Tools for Teachers
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharma ospi slides which help in ospi learning
Pre independence Education in Inndia.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Prims and kruskal algorithms

  • 2. Minimum SpanningTrees (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 • Any 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 SpanningTrees (MST)
  • 3. Minimum SpanningTrees (MST) 2 19 9 1 5 13 17 25 14 8 21
  • 4. 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.
  • 5. 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.
  • 6. 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
  • 7. 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
  • 8.         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
  • 9.    0     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
  • 10.    0     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);
  • 11.    0   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);
  • 12. 14   0   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);
  • 13. 14   0   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. 14   0 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
  • 15. 10   0 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
  • 16. 10   0 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
  • 17. 10 2  0 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
  • 18. 10 2  0 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
  • 19. 10 2  0 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
  • 20. 10 2 9 0 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
  • 21. 10 2 9 0 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
  • 22. 5 2 9 0 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
  • 23. 5 2 9 0 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
  • 24. 5 2 9 0 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
  • 25. 5 2 9 0 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
  • 26. 5 2 9 0 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
  • 27. 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 0 8 15 3 4 3 4 5 2 9 15 8 Prims Algorithm
  • 28. 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
  • 29. 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(Si, Sj): S = S - {Si, Sj} U {Si U Sj} FindSet(X): return Si  S such that x  Si
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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