SlideShare a Scribd company logo
Data Structure and Algorithm
Minimum Cost Spanning Tree
Submitted by,
M. Kavitha,
II – M.Sc(CS&IT),
Nadar Saraswathi College of
Arts & Science, Theni.
Data Structure and Algorithm
Contents :
1. Minimum Cost Spanning Tree
1.1. Prim’s Algorithm
1.2. Kruskal’s Algorithm
1.3. An Optimal Randomized Algorithm (*)
Minimum – Cost Spanning Tree
Definition :
* Let G = (V,E) be an undirected connected graph.
* A sub-graph t = (V,E’) of G is a spanning tree of
G iff t is a tree.
1
2
376
5 4
10
28
14 16
24
25
22
18 12
1
2
376
5
4
10
14 16
25
22
12
Fig 1 : Graph and minimum – cost spanning tree
Prim’s Algorithm
* A greedy method to obtain a minimum-cost spanning
tree builds the tree edge by edge.
* Prim's algorithm finds a minimum spanning tree
(MST) for a connected weighted graph.
* MST = subset of edges that forms a tree including
every vertex, such that total weight of all edges is minimum cost
spanning.
There are two possible ways:
1. The set of edges so far selected form a tree. If A is the set
of edges selected so far then A forms a tree.
2. The next edge (u,v) to be include in A is a minimum-cost
edge not in A with the property that AU{(u,v)} is also a tree.
1
2
376
5
4
1
2
376
5
4
10
25
10
(A) (B)
1
2
376
5
4
10
25
22
(C)
Stages in Prim’s Algorithm :
1
2
376
5
4
10
25
22
12
1
2
376
5
4
10
16
25
22
12
1
2
376
5
4
10
14 16
25
22
12
(D) (E)
(F)
* The algorithm is a minimum cost spanning tree
includes for each vertex v a minimum-cost edge incident to v.
* It support t is a minimum-cost spanning tree for G =
(V,E).
* These algorithms find the minimum spanning forest
in a possibly disconnected graph.
* The most basic form of Prim's algorithm only finds
minimum spanning trees in connected graphs.
mincost :=0;
for i:=2 to n do near[i]:=1;
// Vertex 1 is initially in t.
near[i]:=0;
for i:=1 to n-1 do
{
// Find n-1 edges for t.
Algorithm Prim(E,cost,n,t)
{
Let(k,l) be an edge of minimum cost in E;
mincost:=cost[k,l];
t[1,1]:=k; t[1,2]:=l;
for i:=1 to n do
if (cost[i,l]<cost[i,k])then near[i]:=l;
Else near[i]:=k;
near[k]:=near[l]:=0;
for i:=2 to n-1 do {
Let j be an index such that near[j]≠0 and
cost[j,near[j]] is minimum;
t[i,1]:=j;t[i,2]:=near[j];
mincost:=mincost+cost[j,near[j]];
near[j]:=0;
for k:=1 to n do
if((near[k]≠0)and(cost[k,near[k]]>cost[k,j]))
then near[k]:=j; }
return mincost;
}
Prim’s minimum-cost
spanning tree algorithm
kruskal’s Algorithm
* Kruskal's algorithm is a greedy algorithm in graph
theory that finds a minimum spanning tree for a connected
weighted graph.
* 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.
* If the graph is not connected, then it finds a
minimum spanning forest (a minimum spanning tree for each
connected component).
* Kruskal's Algorithm add edges in increasing weight,
skipping those whose addition would create a cycle.
1
2
376
5
4
1
2
376
5
4
10
1
2
376
5
4
10
12
(A)
(c)
(B)
Stages in Kruskal’s Algorithm :
1
2
37
6
5
4
1
2
376
5
4
10
1
2
376
5
4
10
12
(D)
(F)
(E)
10
12
14
14 16
12
14 16
22
t:=0;
while (t has less than n-1 edges) and (E≠0)) do
{
Choose an edge(v,w) from E of lowest cost;
Delete (v,w) from E;
if (v,w) does not create a cycle in t then add (v,w) to t;
else discard (v,w);
}
1.Would create a cycle if v and w are already in the
same component.
2. I We start with a component for each node.
3. I Components merge when we add an edge.
4. Need a way to: check if v and w are in same
component and to merge two components into one.
Algorithm Kruskal(E,cost,n,t)
{
Construct a heap out of the edge costs using
Heapify;
for i:=1 to n do parent[i]:=-1;
i:=0; mincost:=0.0;
While((i<n-1) and (heap not empty)) do {
Delete a minimum cost edge (u,v) from the
heap and reheapify using Adjust;
j:=Find(u);k:=Find(v);
if(j≠k) then {
i:=i+1;
t[i,1]:=u;t[i,2]:=v;
mincost:=mincost+cost[u,v];
Union(j,k);
} }
if(i≠n-1) then write(“No spanning tree”);
else return mincost;
}
Kruskal’s Algorithm
An Optimal Randomized Algorithm (*)
* The minimum-cost spanning tree of a graph G(V,E) to
spend Ω(|V|+|E|) time in worst case.
* A randomized algorithm that runs in time the
Õ(|V|+|E|) can be devised :
1. Randomly sample m edges from G (for some suitable m).
2. Let G’ be the induced sub graph that is G’ has V as its
node set and the sampled edges in its edge set.
3. The sub graph G’ need not be connected a minimum-cost
spanning tree for each component of G;.
4. Let F be the minimum-cost spanning tree forest of G’.
5. F eliminate edges called the F-heavy edges of G that
cannot possibly be in a minimum-cost spanning tree.
Data structure
Thank You

More Related Content

PPTX
Minimal spanning tree class 15
Kumar
 
PPTX
Aplicaciones de la derivada
JOELSEBASTIANANDRADE
 
PPTX
Aplicaciones de la derivada
JOELSEBASTIANANDRADE
 
PPT
KRUSKAL'S algorithm from chaitra
guest1f4fb3
 
PPTX
Dijkstra's algorithm presentation
Subid Biswas
 
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
PPTX
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 
Minimal spanning tree class 15
Kumar
 
Aplicaciones de la derivada
JOELSEBASTIANANDRADE
 
Aplicaciones de la derivada
JOELSEBASTIANANDRADE
 
KRUSKAL'S algorithm from chaitra
guest1f4fb3
 
Dijkstra's algorithm presentation
Subid Biswas
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 

What's hot (18)

PPTX
Shortest path problem
Ifra Ilyas
 
PPT
Prim's Algorithm on minimum spanning tree
oneous
 
PPT
Unit26 shortest pathalgorithm
meisamstar
 
PPT
Spanning trees
Shareb Ismaeel
 
PPTX
Dijkstra’S Algorithm
ami_01
 
PPTX
Numerical on dichotomous search
Sumita Das
 
PDF
Dijkstra's Algorithm
ArijitDhali
 
PPTX
Luis ppt
RhodaLuis
 
PDF
Algorithms explained
PIYUSH Dubey
 
PPTX
Discrete Mathematics Presentation
Salman Elahi
 
PPTX
Numerical on bisection method
Sumita Das
 
PPT
8.5 Bingo
aleksbudarick
 
PPT
8.5 Bingo
aleksbudarick
 
PDF
Numerical Methods in Mechanical Engineering - Final Project
Stasik Nemirovsky
 
PPTX
Shortest path algorithm
Subrata Kumer Paul
 
PPTX
Networks dijkstra's algorithm- pgsr
Linawati Adiman
 
PPTX
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
PDF
Graph Analytics and Complexity Questions and answers
Animesh Chaturvedi
 
Shortest path problem
Ifra Ilyas
 
Prim's Algorithm on minimum spanning tree
oneous
 
Unit26 shortest pathalgorithm
meisamstar
 
Spanning trees
Shareb Ismaeel
 
Dijkstra’S Algorithm
ami_01
 
Numerical on dichotomous search
Sumita Das
 
Dijkstra's Algorithm
ArijitDhali
 
Luis ppt
RhodaLuis
 
Algorithms explained
PIYUSH Dubey
 
Discrete Mathematics Presentation
Salman Elahi
 
Numerical on bisection method
Sumita Das
 
8.5 Bingo
aleksbudarick
 
8.5 Bingo
aleksbudarick
 
Numerical Methods in Mechanical Engineering - Final Project
Stasik Nemirovsky
 
Shortest path algorithm
Subrata Kumer Paul
 
Networks dijkstra's algorithm- pgsr
Linawati Adiman
 
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Graph Analytics and Complexity Questions and answers
Animesh Chaturvedi
 
Ad

Similar to Data structure (20)

PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
PPT
Weighted graphs
Core Condor
 
PDF
Ireducible core and equal remaining obligations rule for mcst games
vinnief
 
PDF
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
PDF
Proportional and decentralized rule mcst games
vinnief
 
PPTX
kruskal and prims algorithm _
swati463221
 
PDF
Daa chapter11
B.Kirron Reddi
 
PPTX
All pair shortest path
Arafat Hossan
 
PDF
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
ijscmcj
 
PDF
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
ijscmc
 
PPTX
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
ssusered62011
 
PPTX
Dynamic Programming in design and analysis .pptx
dimpuk1
 
PDF
Unit 3 - Greedy Method
MaryJacob24
 
PDF
Unit 3 greedy method
MaryJacob24
 
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Nguyễn Công Hoàng
 
PPT
Chap07alg
Munkhchimeg
 
PPT
Chap07alg
Munhchimeg
 
PDF
Algorithm chapter 9
chidabdu
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Weighted graphs
Core Condor
 
Ireducible core and equal remaining obligations rule for mcst games
vinnief
 
Minimum Spanning Trees Artificial Intelligence
jiraf23341
 
Proportional and decentralized rule mcst games
vinnief
 
kruskal and prims algorithm _
swati463221
 
Daa chapter11
B.Kirron Reddi
 
All pair shortest path
Arafat Hossan
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
ijscmcj
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
ijscmc
 
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
ssusered62011
 
Dynamic Programming in design and analysis .pptx
dimpuk1
 
Unit 3 - Greedy Method
MaryJacob24
 
Unit 3 greedy method
MaryJacob24
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Nguyễn Công Hoàng
 
Chap07alg
Munkhchimeg
 
Chap07alg
Munhchimeg
 
Algorithm chapter 9
chidabdu
 
Ad

More from kavitha muneeshwaran (13)

PPTX
Physical Security
kavitha muneeshwaran
 
PPTX
Digital Audio
kavitha muneeshwaran
 
PPTX
Internet Programming with Java
kavitha muneeshwaran
 
PPTX
Digital image processing
kavitha muneeshwaran
 
PPTX
Staffing level estimation
kavitha muneeshwaran
 
PPTX
Data Integration and Transformation in Data mining
kavitha muneeshwaran
 
PPTX
Transaction Management - Deadlock Handling
kavitha muneeshwaran
 
PPT
Digital Logic circuit
kavitha muneeshwaran
 
PPTX
C and C++ functions
kavitha muneeshwaran
 
PPTX
I/O system in intel 80386 microcomputer architecture
kavitha muneeshwaran
 
PPTX
narrow Band ISDN
kavitha muneeshwaran
 
Physical Security
kavitha muneeshwaran
 
Digital Audio
kavitha muneeshwaran
 
Internet Programming with Java
kavitha muneeshwaran
 
Digital image processing
kavitha muneeshwaran
 
Staffing level estimation
kavitha muneeshwaran
 
Data Integration and Transformation in Data mining
kavitha muneeshwaran
 
Transaction Management - Deadlock Handling
kavitha muneeshwaran
 
Digital Logic circuit
kavitha muneeshwaran
 
C and C++ functions
kavitha muneeshwaran
 
I/O system in intel 80386 microcomputer architecture
kavitha muneeshwaran
 
narrow Band ISDN
kavitha muneeshwaran
 

Recently uploaded (20)

PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
CDH. pptx
AneetaSharma15
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Basics and rules of probability with real-life uses
ravatkaran694
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 

Data structure

  • 1. Data Structure and Algorithm Minimum Cost Spanning Tree Submitted by, M. Kavitha, II – M.Sc(CS&IT), Nadar Saraswathi College of Arts & Science, Theni.
  • 2. Data Structure and Algorithm Contents : 1. Minimum Cost Spanning Tree 1.1. Prim’s Algorithm 1.2. Kruskal’s Algorithm 1.3. An Optimal Randomized Algorithm (*)
  • 3. Minimum – Cost Spanning Tree Definition : * Let G = (V,E) be an undirected connected graph. * A sub-graph t = (V,E’) of G is a spanning tree of G iff t is a tree. 1 2 376 5 4 10 28 14 16 24 25 22 18 12 1 2 376 5 4 10 14 16 25 22 12 Fig 1 : Graph and minimum – cost spanning tree
  • 4. Prim’s Algorithm * A greedy method to obtain a minimum-cost spanning tree builds the tree edge by edge. * Prim's algorithm finds a minimum spanning tree (MST) for a connected weighted graph. * MST = subset of edges that forms a tree including every vertex, such that total weight of all edges is minimum cost spanning. There are two possible ways: 1. The set of edges so far selected form a tree. If A is the set of edges selected so far then A forms a tree. 2. The next edge (u,v) to be include in A is a minimum-cost edge not in A with the property that AU{(u,v)} is also a tree.
  • 7. * The algorithm is a minimum cost spanning tree includes for each vertex v a minimum-cost edge incident to v. * It support t is a minimum-cost spanning tree for G = (V,E). * These algorithms find the minimum spanning forest in a possibly disconnected graph. * The most basic form of Prim's algorithm only finds minimum spanning trees in connected graphs. mincost :=0; for i:=2 to n do near[i]:=1; // Vertex 1 is initially in t. near[i]:=0; for i:=1 to n-1 do { // Find n-1 edges for t.
  • 8. Algorithm Prim(E,cost,n,t) { Let(k,l) be an edge of minimum cost in E; mincost:=cost[k,l]; t[1,1]:=k; t[1,2]:=l; for i:=1 to n do if (cost[i,l]<cost[i,k])then near[i]:=l; Else near[i]:=k; near[k]:=near[l]:=0; for i:=2 to n-1 do { Let j be an index such that near[j]≠0 and cost[j,near[j]] is minimum; t[i,1]:=j;t[i,2]:=near[j]; mincost:=mincost+cost[j,near[j]]; near[j]:=0; for k:=1 to n do if((near[k]≠0)and(cost[k,near[k]]>cost[k,j])) then near[k]:=j; } return mincost; } Prim’s minimum-cost spanning tree algorithm
  • 9. kruskal’s Algorithm * Kruskal's algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. * 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. * If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). * Kruskal's Algorithm add edges in increasing weight, skipping those whose addition would create a cycle.
  • 12. t:=0; while (t has less than n-1 edges) and (E≠0)) do { Choose an edge(v,w) from E of lowest cost; Delete (v,w) from E; if (v,w) does not create a cycle in t then add (v,w) to t; else discard (v,w); } 1.Would create a cycle if v and w are already in the same component. 2. I We start with a component for each node. 3. I Components merge when we add an edge. 4. Need a way to: check if v and w are in same component and to merge two components into one.
  • 13. Algorithm Kruskal(E,cost,n,t) { Construct a heap out of the edge costs using Heapify; for i:=1 to n do parent[i]:=-1; i:=0; mincost:=0.0; While((i<n-1) and (heap not empty)) do { Delete a minimum cost edge (u,v) from the heap and reheapify using Adjust; j:=Find(u);k:=Find(v); if(j≠k) then { i:=i+1; t[i,1]:=u;t[i,2]:=v; mincost:=mincost+cost[u,v]; Union(j,k); } } if(i≠n-1) then write(“No spanning tree”); else return mincost; } Kruskal’s Algorithm
  • 14. An Optimal Randomized Algorithm (*) * The minimum-cost spanning tree of a graph G(V,E) to spend Ω(|V|+|E|) time in worst case. * A randomized algorithm that runs in time the Õ(|V|+|E|) can be devised : 1. Randomly sample m edges from G (for some suitable m). 2. Let G’ be the induced sub graph that is G’ has V as its node set and the sampled edges in its edge set. 3. The sub graph G’ need not be connected a minimum-cost spanning tree for each component of G;. 4. Let F be the minimum-cost spanning tree forest of G’. 5. F eliminate edges called the F-heavy edges of G that cannot possibly be in a minimum-cost spanning tree.