SlideShare a Scribd company logo
1 of 11
CSE 326: Data Structures
Minimum Spanning Trees
and
The Dynamic Duo (Prim and Kruskal)
Hannah Tang and Brian Tjaden
Summer Quarter 2002
Some Applications:
Moving Around Washington
What’s the fastest way from Seattle to Spokane?
Use Dijkstra’s Algorithm!
Some Applications:
Communication in Washington
What’s the cheapest inter-city network?
Spanning tree: a subset of the edges from a connected graph
that…
…touches all vertices in the graph (spans the graph)
…forms a tree (is connected and contains no cycles)
Minimum spanning tree: the spanning tree with the least total
edge cost.
Spanning Tree
4 7
1 5
9
2
Two Different Algorithms
Prim’s Algorithm
Almost identical to Dijkstra’s
Kruskals’s Algorithm
Completely different!
Prim’s Algorithm for
Minimum Spanning Trees
A node-oriented greedy algorithm (builds an MST by
greedily adding nodes)
Select a node to be the “root” and mark it as known
While there are unknown nodes left in the graph
Select the unknown node n with the smallest cost from
some known node m
Mark n as known
Add (m, n) to our MST
Runtime:
Prim’s Algorithm In Action
A
C
B
D
F H
G
E
2 2 3
2
1
4
10
8
1
94
2
7
Kruskal’s Algorithm for
Minimum Spanning Trees
An edge-oriented greedy algorithm (builds an MST
by greedily adding edges)
Initialize all vertices to unconnected
While there are still unmarked edges
Pick the lowest cost edge e = (u, v) and mark it
If u and v are not already connected, add e to the
minimum spanning tree and connect u and v
Sound familiar?
(Think maze generation.)
Kruskal’s Algorithm In Action
A
C
B
D
F H
G
E
2 2 3
2
1
4
10
8
1
94
2
7
Proof of Correctness
We already showed this finds a spanning tree:
That was part of our definition of a good maze.
Proof by contradiction that Kruskal’s finds the minimum:
Assume another spanning tree has lower cost than Kruskal’s
Pick an edge e1 = (u, v) in that tree that’s not in Kruskal’s
Kruskal’s tree connects u’s and v’s sets with another edge e2
But, e2 must have at most the same cost as e1!
So, swap e2 for e1 (at worst keeping the cost the same)
Repeat until the tree is identical to Kruskal’s: contradiction!
QED: Kruskal’s algorithm finds a minimum spanning tree.
Data Structures
for Kruskal’s Algorithm
Pick the lowest cost edge…
findMin/deleteMin
If u and v are not already connected…
…connect u and v.
find representative
union
|E| times:
|E| times:
Runtime:

More Related Content

What's hot

What's hot (20)

MST
MSTMST
MST
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Spectral clustering
Spectral clusteringSpectral clustering
Spectral clustering
 
Transfer learningforclp
Transfer learningforclpTransfer learningforclp
Transfer learningforclp
 
IJCAI13 Paper review: Large-scale spectral clustering on graphs
IJCAI13 Paper review: Large-scale spectral clustering on graphsIJCAI13 Paper review: Large-scale spectral clustering on graphs
IJCAI13 Paper review: Large-scale spectral clustering on graphs
 
Data structure
Data structureData structure
Data structure
 
Daa chapter13
Daa chapter13Daa chapter13
Daa chapter13
 
Prim's and Kruskal's Algorithm
Prim's and Kruskal's AlgorithmPrim's and Kruskal's Algorithm
Prim's and Kruskal's Algorithm
 
Notes on Spectral Clustering
Notes on Spectral ClusteringNotes on Spectral Clustering
Notes on Spectral Clustering
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Tree_Chain
Tree_ChainTree_Chain
Tree_Chain
 
Tensor Spectral Clustering
Tensor Spectral ClusteringTensor Spectral Clustering
Tensor Spectral Clustering
 
Ram minimum spanning tree
Ram   minimum spanning treeRam   minimum spanning tree
Ram minimum spanning tree
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithm
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explained
 
Shortest route and mst
Shortest route and mstShortest route and mst
Shortest route and mst
 
fast-matmul-cse15
fast-matmul-cse15fast-matmul-cse15
fast-matmul-cse15
 
Manifold Blurring Mean Shift algorithms for manifold denoising, presentation,...
Manifold Blurring Mean Shift algorithms for manifold denoising, presentation,...Manifold Blurring Mean Shift algorithms for manifold denoising, presentation,...
Manifold Blurring Mean Shift algorithms for manifold denoising, presentation,...
 

Similar to 17 prims-kruskals (1)

Ireducible core and equal remaining obligations rule for mcst games
Ireducible core and equal remaining obligations rule for mcst gamesIreducible core and equal remaining obligations rule for mcst games
Ireducible core and equal remaining obligations rule for mcst games
vinnief
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
chidabdu
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
KRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitraKRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitra
guest1f4fb3
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
zefergaming
 
Proportional and decentralized rule mcst games
Proportional and decentralized rule mcst gamesProportional and decentralized rule mcst games
Proportional and decentralized rule mcst games
vinnief
 
CLIQUE Automatic subspace clustering of high dimensional data for data mining...
CLIQUE Automatic subspace clustering of high dimensional data for data mining...CLIQUE Automatic subspace clustering of high dimensional data for data mining...
CLIQUE Automatic subspace clustering of high dimensional data for data mining...
Raed Aldahdooh
 

Similar to 17 prims-kruskals (1) (20)

Ireducible core and equal remaining obligations rule for mcst games
Ireducible core and equal remaining obligations rule for mcst gamesIreducible core and equal remaining obligations rule for mcst games
Ireducible core and equal remaining obligations rule for mcst games
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
8_MST_pptx.pptx
8_MST_pptx.pptx8_MST_pptx.pptx
8_MST_pptx.pptx
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
Graph in data structures
Graph in data structuresGraph in data structures
Graph in data structures
 
KRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitraKRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitra
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101
 
Clustering Algorithms.pdf
Clustering Algorithms.pdfClustering Algorithms.pdf
Clustering Algorithms.pdf
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Combinatorial Optimization
Combinatorial OptimizationCombinatorial Optimization
Combinatorial Optimization
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
dijkstra algo.ppt
dijkstra algo.pptdijkstra algo.ppt
dijkstra algo.ppt
 
barrera.ppt
barrera.pptbarrera.ppt
barrera.ppt
 
barrera.ppt
barrera.pptbarrera.ppt
barrera.ppt
 
Proportional and decentralized rule mcst games
Proportional and decentralized rule mcst gamesProportional and decentralized rule mcst games
Proportional and decentralized rule mcst games
 
CLIQUE Automatic subspace clustering of high dimensional data for data mining...
CLIQUE Automatic subspace clustering of high dimensional data for data mining...CLIQUE Automatic subspace clustering of high dimensional data for data mining...
CLIQUE Automatic subspace clustering of high dimensional data for data mining...
 
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
 
Data structure
Data structureData structure
Data structure
 
(148065320) dijistra algo
(148065320) dijistra algo(148065320) dijistra algo
(148065320) dijistra algo
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
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
KarakKing
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
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
 
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
 
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.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
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Ữ Â...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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
 

17 prims-kruskals (1)

  • 1. CSE 326: Data Structures Minimum Spanning Trees and The Dynamic Duo (Prim and Kruskal) Hannah Tang and Brian Tjaden Summer Quarter 2002
  • 2. Some Applications: Moving Around Washington What’s the fastest way from Seattle to Spokane? Use Dijkstra’s Algorithm!
  • 3. Some Applications: Communication in Washington What’s the cheapest inter-city network?
  • 4. Spanning tree: a subset of the edges from a connected graph that… …touches all vertices in the graph (spans the graph) …forms a tree (is connected and contains no cycles) Minimum spanning tree: the spanning tree with the least total edge cost. Spanning Tree 4 7 1 5 9 2
  • 5. Two Different Algorithms Prim’s Algorithm Almost identical to Dijkstra’s Kruskals’s Algorithm Completely different!
  • 6. Prim’s Algorithm for Minimum Spanning Trees A node-oriented greedy algorithm (builds an MST by greedily adding nodes) Select a node to be the “root” and mark it as known While there are unknown nodes left in the graph Select the unknown node n with the smallest cost from some known node m Mark n as known Add (m, n) to our MST Runtime:
  • 7. Prim’s Algorithm In Action A C B D F H G E 2 2 3 2 1 4 10 8 1 94 2 7
  • 8. Kruskal’s Algorithm for Minimum Spanning Trees An edge-oriented greedy algorithm (builds an MST by greedily adding edges) Initialize all vertices to unconnected While there are still unmarked edges Pick the lowest cost edge e = (u, v) and mark it If u and v are not already connected, add e to the minimum spanning tree and connect u and v Sound familiar? (Think maze generation.)
  • 9. Kruskal’s Algorithm In Action A C B D F H G E 2 2 3 2 1 4 10 8 1 94 2 7
  • 10. Proof of Correctness We already showed this finds a spanning tree: That was part of our definition of a good maze. Proof by contradiction that Kruskal’s finds the minimum: Assume another spanning tree has lower cost than Kruskal’s Pick an edge e1 = (u, v) in that tree that’s not in Kruskal’s Kruskal’s tree connects u’s and v’s sets with another edge e2 But, e2 must have at most the same cost as e1! So, swap e2 for e1 (at worst keeping the cost the same) Repeat until the tree is identical to Kruskal’s: contradiction! QED: Kruskal’s algorithm finds a minimum spanning tree.
  • 11. Data Structures for Kruskal’s Algorithm Pick the lowest cost edge… findMin/deleteMin If u and v are not already connected… …connect u and v. find representative union |E| times: |E| times: Runtime: