SlideShare a Scribd company logo
GRAPH
APPLICATION - MST
o Prim’s
o Kruskal’s
1
WHAT IS SPANNING TREE
 A spanning tree of a graph is a tree that has all the
vertices of the graph connected by some edge
 A graph can have one or more number of spanning
trees.
 If the graph has N vertices then the spanning tree
will have n-1 edges
2
SPANNING TREE EXAMPLE
3
SPANNING TREE - CREATION
 Either DFS or BFS can be used to create a spanning
tree
 When DFS is used, the resulting spanning tree is
known as a depth first spanning tree
 When BFS is used, the resulting spanning tree is
known as a breadth first spanning tree
 While adding a non-tree edge into any spanning tree,
this will create a cycle
4
MINIMUM SPANNING TREE
 A minimum spanning tree is a spanning tree that has
the minimum weight than all other spanning trees of a
graph
 Three different algorithms can be used
 Prim’s algorithm
 Kruskal’s algorithm 5
PRIM’S ALGORITHM - INTRODUCTION
 Prim's algorithm is a greedy algorithm.
 It finds a minimum spanning tree for a weighted
undirected 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.
6
PRIM’S STEPS
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
7
PRIM’S ALGORITHM
8
Initialization:
 Pick a vertex r to be the root
 Set D(r) = 0, parent(r) = null
 For all vertices v  V, v  r, set D(v) = 
 Insert all vertices into priority queue P, using
distances as the keys
PRIM’S ALGORITHM (CONT..)
9
a
c
e
d
b
2
45
9
6
4
5
5
e a b c d
0    
Vertex Parent
e -
Vertex
Distance
PRIM’S ALGORITHM (CONT..)
While P is not empty:
 Select the next vertex u to add to the tree
u = P.deleteMin()
 Update the weight of each vertex w adjacent to u
which is not in the tree (i.e., w  P)
If weight(u,w) < D(w),
a. parent(w) = u
b. D(w) = weight(u,w)
c. Update the priority queue to
reflect new distance for w
10
PRIM’S ALGORITHM (CONT..)
11
d b c a
4 5 5 
Vertex Parent
e -
b e
c e
d e
Vertex Parent
e -
b -
c -
d -
d b c a
   
e
0
PRIM’S ALGORITHM (CONT..)
12
a c b
2 4 5
Vertex Parent
e -
b e
c d
d e
a d
d b c a
4 5 5 
Vertex Parent
e -
b e
c e
d e
PRIM’S ALGORITHM (CONT..)
13
c b
4 5
Vertex Parent
e -
b e
c d
d e
a d
a c b
2 4 5
Vertex Parent
e -
b e
c d
d e
a d
PRIM’S ALGORITHM (CONT..)
14
b
5
Vertex Parent
e -
b e
c d
d e
a d
c b
4 5
Vertex Parent
e -
b e
c d
d e
a d
PRIM’S ALGORITHM (CONT..)
15
Vertex Parent
e -
b e
c d
d e
a d
b
5
Vertex Parent
e -
b e
c d
d e
a d
MST- USING PRIM’S ALGORITHM
16
a
c
e
d
b
2
45
9
6
4
5
5
RUNNING TIME OF PRIM’S ALGORITHM
 Initialization of priority queue (array): O(|V|)
 Update loop:
• Choosing vertex with minimum cost edge: O(|V|)
• Updating distance values of unconnected vertices:
each edge is considered only once during entire
execution, for a total of O(|E|) updates
 Overall cost :
17
O(|E| + |V| 2)
KRUSKAL’S ALGORITHM - INTRODUCTION
 Create a forest of trees from the vertices
 Repeatedly merge trees by adding “safe edges”
until only one tree remains
 A “safe edge” is an edge of minimum weight which
does not create a cycle
18
19
a
c
e
d
b
2
45
9
6
4
5
5
forest: {a}, {b}, {c}, {d}, {e}
DEFINING FOREST
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
20
KRUSKAL’S STEPS
21
Initialization
 a. Create a set for each vertex v  V
 b. Initialize the set of “safe edges” A comprising
the MST to the empty set
 c. Sort edges by increasing weight
KRUSKAL’S ALGORITHM
22
a
c
e
d
b
2
45
9
6
4
5
5
F = {a}, {b}, {c}, {d}, {e}
A = 
E = {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
KRUSKAL’S ALGORITHM (CONT.)
23
 For each edge (u,v)  E in increasing order
while more than one set remains:
 If u and v, belong to different sets U and V
a. add edge (u,v) to the safe edge set
A = A  {(u,v)}
b. merge the sets U and V
F = F - U - V + (U  V)
 Return A
KRUSKAL’S ALGORITHM (CONT.)
24
E = {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
Forest
{a}, {b}, {c}, {d}, {e}
{a,d}, {b}, {c}, {e}
{a,d,c}, {b}, {e}
{a,d,c,e}, {b}
{a,d,c,e,b}
A

{(a,d)}
{(a,d), (c,d)}
{(a,d), (c,d), (d,e)}
{(a,d), (c,d), (d,e), (b,e)}
KRUSKAL’S ALGORITHM (CONT.)
25
 Running time bounded by sorting (or findMin)
O(|E|log|E|) , or equivalently, O(|E|log|V|)
RUNNING TIME OF KRUSKAL’S ALGORITHM
26
APPLICATION OF MST
 Any time you want to visit all vertices in a graph at
minimum cost (e.g., wire routing on printed circuit boards,
sewer pipe layout, road planning…)
 Internet content distribution
 Idea: publisher produces web pages, content
distribution network replicates web pages to many
locations so consumers can access at higher speed
 MST may not be good enough!
content distribution on minimum cost tree may take a
long time!
27

More Related Content

PPTX
Prims & kruskal algorithms
PPTX
Prims and kruskal algorithms
PDF
Prims Algorithm
PPT
Single source stortest path bellman ford and dijkstra
PPTX
Bellman ford algorithm
PPTX
Dijkstra’s algorithm
PPTX
Dijkstra s algorithm
PDF
All pairs shortest path algorithm
Prims & kruskal algorithms
Prims and kruskal algorithms
Prims Algorithm
Single source stortest path bellman ford and dijkstra
Bellman ford algorithm
Dijkstra’s algorithm
Dijkstra s algorithm
All pairs shortest path algorithm

What's hot (20)

PPTX
Kruskal Algorithm
PDF
Minimum spanning tree
PPT
minimum spanning tree
PPTX
A presentation on prim's and kruskal's algorithm
PPTX
Dijkstra’S Algorithm
PPTX
Prim's algorithm
PPTX
Kruskal's algorithm
PPTX
Minimum Spanning Tree
PPT
KRUSKAL'S algorithm from chaitra
PPTX
Minimum spanning tree
PPT
Prim Algorithm and kruskal algorithm
PPT
Spanning trees
PPT
Minimum spanning tree
PPT
Prim's Algorithm on minimum spanning tree
PDF
Shortest Path in Graph
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Dijkstra's algorithm presentation
PPT
minimum spanning trees Algorithm
PPTX
Bfs and Dfs
PPTX
Dijkstra's algorithm
Kruskal Algorithm
Minimum spanning tree
minimum spanning tree
A presentation on prim's and kruskal's algorithm
Dijkstra’S Algorithm
Prim's algorithm
Kruskal's algorithm
Minimum Spanning Tree
KRUSKAL'S algorithm from chaitra
Minimum spanning tree
Prim Algorithm and kruskal algorithm
Spanning trees
Minimum spanning tree
Prim's Algorithm on minimum spanning tree
Shortest Path in Graph
Greedy Algorithm - Knapsack Problem
Dijkstra's algorithm presentation
minimum spanning trees Algorithm
Bfs and Dfs
Dijkstra's algorithm
Ad

Viewers also liked (20)

PPTX
Android based application for graph analysis final report
PPT
Kruskals prims shared by: geekssay.com
PDF
Greedy minimum spanning tree- prim's algorithm
PPTX
Static Spatial Graph Features
PPTX
Kruskal & Prim's Algorithm
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
PPT
9 cm402.18
PPT
Minimum spanning tree
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PDF
Prim algorithm
PPTX
Application of greedy method prim
PPTX
Smoothing Filters in Spatial Domain
PPTX
Prim's algorithm
PPTX
Kruskal Algorithm
PPT
2.4 mst prim’s algorithm
PPTX
Minimum spanning Tree
PPTX
Graph theory and life
PPTX
Spanning trees & applications
PPTX
graph theory
PPTX
Interesting applications of graph theory
Android based application for graph analysis final report
Kruskals prims shared by: geekssay.com
Greedy minimum spanning tree- prim's algorithm
Static Spatial Graph Features
Kruskal & Prim's Algorithm
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
9 cm402.18
Minimum spanning tree
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Prim algorithm
Application of greedy method prim
Smoothing Filters in Spatial Domain
Prim's algorithm
Kruskal Algorithm
2.4 mst prim’s algorithm
Minimum spanning Tree
Graph theory and life
Spanning trees & applications
graph theory
Interesting applications of graph theory
Ad

Similar to GRAPH APPLICATION - MINIMUM SPANNING TREE (MST) (20)

PPTX
Minimum spanning tree
PPT
Minimum Spanning Tree
PPT
Greedy Approach in Design Analysis and Algorithms
PPTX
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
PDF
Ijciras1101
PPTX
Ram minimum spanning tree
PPTX
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
PPTX
7. Spanning trees
PPT
Unit27_MinimumSpanningTree.ppt data structure programming
PPTX
8_MST_pptx.pptx
PPTX
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
PPTX
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
PPTX
DM Min SPan Tree Minimum spanning tree .pptx
PDF
DATA STRUCTURES & ALGORITHMS MINIMUM SPANNING TREE
PPTX
PPT
Unit 5 graphs minimum spanning trees
PPTX
prim's and kruskal's algorithm
PPTX
Minimum Spanning Tree (Data Structure and Algorithm)
PDF
15 chapter9 graph_algorithms_mst
Minimum spanning tree
Minimum Spanning Tree
Greedy Approach in Design Analysis and Algorithms
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
Ijciras1101
Ram minimum spanning tree
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
7. Spanning trees
Unit27_MinimumSpanningTree.ppt data structure programming
8_MST_pptx.pptx
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
DM Min SPan Tree Minimum spanning tree .pptx
DATA STRUCTURES & ALGORITHMS MINIMUM SPANNING TREE
Unit 5 graphs minimum spanning trees
prim's and kruskal's algorithm
Minimum Spanning Tree (Data Structure and Algorithm)
15 chapter9 graph_algorithms_mst

More from Madhu Bala (8)

PPTX
Internet of Things (IoT)
PPTX
Digital logic
PPTX
Operating system
PPTX
Divide and conquer - Quick sort
PPTX
GPRS Technology
PPTX
Algorithm - Introduction
PPTX
4G technology
PPTX
Data structure - Graph
Internet of Things (IoT)
Digital logic
Operating system
Divide and conquer - Quick sort
GPRS Technology
Algorithm - Introduction
4G technology
Data structure - Graph

Recently uploaded (20)

PPTX
“Next-Gen AI: Trends Reshaping Our World”
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
web development for engineering and engineering
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
“Next-Gen AI: Trends Reshaping Our World”
Chapter 6 Design in software Engineeing.ppt
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CH1 Production IntroductoryConcepts.pptx
Simulation of electric circuit laws using tinkercad.pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Sustainable Sites - Green Building Construction
bas. eng. economics group 4 presentation 1.pptx
Internship_Presentation_Final engineering.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Lesson 3_Tessellation.pptx finite Mathematics
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Internet of Things (IOT) - A guide to understanding
Lecture Notes Electrical Wiring System Components
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
web development for engineering and engineering
Operating System & Kernel Study Guide-1 - converted.pdf
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd

GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)

  • 1. GRAPH APPLICATION - MST o Prim’s o Kruskal’s 1
  • 2. WHAT IS SPANNING TREE  A spanning tree of a graph is a tree that has all the vertices of the graph connected by some edge  A graph can have one or more number of spanning trees.  If the graph has N vertices then the spanning tree will have n-1 edges 2
  • 4. SPANNING TREE - CREATION  Either DFS or BFS can be used to create a spanning tree  When DFS is used, the resulting spanning tree is known as a depth first spanning tree  When BFS is used, the resulting spanning tree is known as a breadth first spanning tree  While adding a non-tree edge into any spanning tree, this will create a cycle 4
  • 5. MINIMUM SPANNING TREE  A minimum spanning tree is a spanning tree that has the minimum weight than all other spanning trees of a graph  Three different algorithms can be used  Prim’s algorithm  Kruskal’s algorithm 5
  • 6. PRIM’S ALGORITHM - INTRODUCTION  Prim's algorithm is a greedy algorithm.  It finds a minimum spanning tree for a weighted undirected 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. 6
  • 7. PRIM’S STEPS 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 7
  • 8. PRIM’S ALGORITHM 8 Initialization:  Pick a vertex r to be the root  Set D(r) = 0, parent(r) = null  For all vertices v  V, v  r, set D(v) =   Insert all vertices into priority queue P, using distances as the keys
  • 9. PRIM’S ALGORITHM (CONT..) 9 a c e d b 2 45 9 6 4 5 5 e a b c d 0     Vertex Parent e - Vertex Distance
  • 10. PRIM’S ALGORITHM (CONT..) While P is not empty:  Select the next vertex u to add to the tree u = P.deleteMin()  Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w  P) If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w 10
  • 11. PRIM’S ALGORITHM (CONT..) 11 d b c a 4 5 5  Vertex Parent e - b e c e d e Vertex Parent e - b - c - d - d b c a     e 0
  • 12. PRIM’S ALGORITHM (CONT..) 12 a c b 2 4 5 Vertex Parent e - b e c d d e a d d b c a 4 5 5  Vertex Parent e - b e c e d e
  • 13. PRIM’S ALGORITHM (CONT..) 13 c b 4 5 Vertex Parent e - b e c d d e a d a c b 2 4 5 Vertex Parent e - b e c d d e a d
  • 14. PRIM’S ALGORITHM (CONT..) 14 b 5 Vertex Parent e - b e c d d e a d c b 4 5 Vertex Parent e - b e c d d e a d
  • 15. PRIM’S ALGORITHM (CONT..) 15 Vertex Parent e - b e c d d e a d b 5 Vertex Parent e - b e c d d e a d
  • 16. MST- USING PRIM’S ALGORITHM 16 a c e d b 2 45 9 6 4 5 5
  • 17. RUNNING TIME OF PRIM’S ALGORITHM  Initialization of priority queue (array): O(|V|)  Update loop: • Choosing vertex with minimum cost edge: O(|V|) • Updating distance values of unconnected vertices: each edge is considered only once during entire execution, for a total of O(|E|) updates  Overall cost : 17 O(|E| + |V| 2)
  • 18. KRUSKAL’S ALGORITHM - INTRODUCTION  Create a forest of trees from the vertices  Repeatedly merge trees by adding “safe edges” until only one tree remains  A “safe edge” is an edge of minimum weight which does not create a cycle 18
  • 19. 19 a c e d b 2 45 9 6 4 5 5 forest: {a}, {b}, {c}, {d}, {e} DEFINING FOREST
  • 20. 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 20 KRUSKAL’S STEPS
  • 21. 21 Initialization  a. Create a set for each vertex v  V  b. Initialize the set of “safe edges” A comprising the MST to the empty set  c. Sort edges by increasing weight KRUSKAL’S ALGORITHM
  • 22. 22 a c e d b 2 45 9 6 4 5 5 F = {a}, {b}, {c}, {d}, {e} A =  E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} KRUSKAL’S ALGORITHM (CONT.)
  • 23. 23  For each edge (u,v)  E in increasing order while more than one set remains:  If u and v, belong to different sets U and V a. add edge (u,v) to the safe edge set A = A  {(u,v)} b. merge the sets U and V F = F - U - V + (U  V)  Return A KRUSKAL’S ALGORITHM (CONT.)
  • 24. 24 E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} Forest {a}, {b}, {c}, {d}, {e} {a,d}, {b}, {c}, {e} {a,d,c}, {b}, {e} {a,d,c,e}, {b} {a,d,c,e,b} A  {(a,d)} {(a,d), (c,d)} {(a,d), (c,d), (d,e)} {(a,d), (c,d), (d,e), (b,e)} KRUSKAL’S ALGORITHM (CONT.)
  • 25. 25  Running time bounded by sorting (or findMin) O(|E|log|E|) , or equivalently, O(|E|log|V|) RUNNING TIME OF KRUSKAL’S ALGORITHM
  • 26. 26
  • 27. APPLICATION OF MST  Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…)  Internet content distribution  Idea: publisher produces web pages, content distribution network replicates web pages to many locations so consumers can access at higher speed  MST may not be good enough! content distribution on minimum cost tree may take a long time! 27