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 Treezhaokatherine
 
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 treeoneous
 
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 Sahil Kumar
 
Disk scheduling algorithms
Disk scheduling algorithms Disk scheduling algorithms
Disk scheduling algorithms Paresh Parmar
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning treeAlona Salva
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comHemant Gautam
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmAcad
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
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 algorithmGaurav Kolekar
 
Prims Algorithm
Prims AlgorithmPrims Algorithm
Prims AlgorithmSriram Raj
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmIfad Rahman
 
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)Mohanlal Sukhadia University (MLSU)
 
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_alfayoumiIbrahim Alfayoumi
 

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

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
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)Jisc
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
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.pdfAdmir Softic
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
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.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
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)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
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
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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)