SlideShare a Scribd company logo
1 of 23
Data Structures
Lecture 28: Minimum Spanning Tree
Outlines
 Minimum Spanning Tree
 Prim’s Algorithm
 Kruskal’s Algorithm
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Minimum Spanning Tree
 Input: A connected, undirected graph G = (V, E)
with weight function w : E R.
• For simplicity, we assume that all edge weights are
distinct.
• Output: A spanning tree T, a tree that connects all
vertices, of minimum weight:
Tvu
vuwTw
),(
),()(
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of MST
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Prim’s Algorithm
IDEA: Maintain V – A as a priority queue Q. Key
each vertex in Q with the weight of the least-
weight edge connecting it to a vertex in A.
Q V
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v) ⊳ DECREASE-KEY
[v] u
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
7
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
7
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
5 7
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
5 7
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
0
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
0
8
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
0
8
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
3 0
8
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
3 0
8
9
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example of Prim’s algorithm
A
V – A
6
5 7
3 0
8
9
15
6 12
5
14
3
8
10
15
9
7
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm
It is a greedy algorithm.
 In Kruskal’s algorithm, the set A is a forest.
 The safe edge is added to A is always a least-weight edge in the
graph that connects two distinct Components.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm
 The operation FIND-SET(u) returns a representative element
from the set that contains u.
 Thus we can determine whether two vertices u and v belong to
the same tree by testing whether
FIND-SET(u) = FIND-SET(v)
 The combining of trees is accomplished by the
UNION procedure
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Kruskal’s Algorithm
1. A
2. for each vertex v V[G]
3.do MAKE-SET(v)
4.short the edges of E into non-decreasing order
by weight.
5.for each edge (u, v) E, taken in non-decreasing order
by weight.
6.do if FIND-SET(u) != FIND-SET(v)
7. then, A  A U {(u, v)}
8. UNION(u, v)
9.return A
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Complexity
Prim’s Algorithm: O(E logV)
Kruskal’s Algorithm: O(E logV)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

More Related Content

Viewers also liked

Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
Tech_MX
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims Algorithm
Sriram Raj
 

Viewers also liked (20)

Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
9 cm402.18
9 cm402.189 cm402.18
9 cm402.18
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Disk scheduling algorithms
Disk scheduling algorithms Disk scheduling algorithms
Disk scheduling algorithms
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.com
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Greedy minimum spanning tree- prim's algorithm
Greedy minimum spanning tree- prim's algorithmGreedy minimum spanning tree- prim's algorithm
Greedy minimum spanning tree- prim's algorithm
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims Algorithm
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Minimum spanning tree

  • 1. Data Structures Lecture 28: Minimum Spanning Tree
  • 2. Outlines  Minimum Spanning Tree  Prim’s Algorithm  Kruskal’s Algorithm Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Minimum Spanning Tree  Input: A connected, undirected graph G = (V, E) with weight function w : E R. • For simplicity, we assume that all edge weights are distinct. • Output: A spanning tree T, a tree that connects all vertices, of minimum weight: Tvu vuwTw ),( ),()( Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Example of MST 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Prim’s Algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A. Q V key[v] for all v V key[s] 0 for some arbitrary s V while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if v Q and w(u, v) < key[v] then key[v] w(u, v) ⊳ DECREASE-KEY [v] u Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Example of Prim’s algorithm A V – A 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Example of Prim’s algorithm A V – A 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. Example of Prim’s algorithm A V – A 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. Example of Prim’s algorithm A V – A 7 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Example of Prim’s algorithm A V – A 7 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Example of Prim’s algorithm A V – A 5 7 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Example of Prim’s algorithm A V – A 5 7 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Example of Prim’s algorithm A V – A 6 5 7 0 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Example of Prim’s algorithm A V – A 6 5 7 0 8 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Example of Prim’s algorithm A V – A 6 5 7 0 8 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Example of Prim’s algorithm A V – A 6 5 7 3 0 8 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Example of Prim’s algorithm A V – A 6 5 7 3 0 8 9 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. Example of Prim’s algorithm A V – A 6 5 7 3 0 8 9 15 6 12 5 14 3 8 10 15 9 7 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Kruskal’s Algorithm It is a greedy algorithm.  In Kruskal’s algorithm, the set A is a forest.  The safe edge is added to A is always a least-weight edge in the graph that connects two distinct Components. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Kruskal’s Algorithm  The operation FIND-SET(u) returns a representative element from the set that contains u.  Thus we can determine whether two vertices u and v belong to the same tree by testing whether FIND-SET(u) = FIND-SET(v)  The combining of trees is accomplished by the UNION procedure Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Kruskal’s Algorithm 1. A 2. for each vertex v V[G] 3.do MAKE-SET(v) 4.short the edges of E into non-decreasing order by weight. 5.for each edge (u, v) E, taken in non-decreasing order by weight. 6.do if FIND-SET(u) != FIND-SET(v) 7. then, A  A U {(u, v)} 8. UNION(u, v) 9.return A Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Complexity Prim’s Algorithm: O(E logV) Kruskal’s Algorithm: O(E logV) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)