SlideShare a Scribd company logo
Minimum-Cost Spanning Tree 
• weighted connected undirected graph 
• spanning tree 
• cost of spanning tree is sum of edge costs 
• find spanning tree that has minimum cost
Example 
8 10 14 
7 12 
1 3 5 7 
2 4 6 3 
2 4 6 8 
9 
• Network has 10 edges. 
• Spanning tree has only n - 1 = 7 edges. 
• Need to either select 7 edges or discard 3.
Edge Selection Greedy Strategies 
• Start with an n-vertex 0-edge forest. 
Consider edges in ascending order of cost. 
Select edge if it does not form a cycle 
together with already selected edges. 
 Kruskal’s method. 
• Start with a 1-vertex tree and grow it into an 
n-vertex tree by repeatedly adding a vertex 
and an edge. When there is a choice, add a 
least cost edge. 
 Prim’s method.
Edge Selection Greedy Strategies 
• Start with an n-vertex forest. Each 
component/tree selects a least cost edge to 
connect to another component/tree. 
Eliminate duplicate selections and possible 
cycles. Repeat until only 1 component/tree 
is left. 
 Sollin’s method.
Edge Rejection Greedy Strategies 
• Start with the connected graph. Repeatedly 
find a cycle and eliminate the highest cost 
edge on this cycle. Stop when no cycles 
remain. 
• Consider edges in descending order of cost. 
Eliminate an edge provided this leaves 
behind a connected graph.
Kruskal’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
1 3 5 7 
2 4 6 8 
• Start with a forest that has no edges. 
• Consider edges in ascending order of cost. 
• Edge (1,2) is considered first and added to 
the forest.
Kruskal’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
1 3 5 7 
7 
2 4 
6 
3 
2 4 6 8 
• Edge (7,8) is considered next and added. 
• Edge (3,4) is considered next and added. 
• Edge (5,6) is considered next and added. 
• Edge (2,3) is considered next and added. 
• Edge (1,3) is considered next and rejected 
because it creates a cycle.
Kruskal’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
10 
1 3 5 7 
7 
2 4 6 
3 
2 4 6 8 
• Edge (2,4) is considered next and rejected 
because it creates a cycle. 
• Edge (3,5) is considered next and added. 
• Edge (3,6) is considered next and rejected. 
• Edge (5,7) is considered next and added. 
14
Kruskal’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
10 
14 
1 3 5 7 
7 
2 4 6 3 
2 4 6 8 
• n - 1 edges have been selected and no cycle 
formed. 
• So we must have a spanning tree. 
• Cost is 46. 
• Min-cost spanning tree is unique when all 
edge costs are different.
Prim’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
4 
1 
2 
2 
7 
• Start with any single vertex tree. 
5 
6 
6 
3 
10 
4 
7 
14 
8 
• Get a 2-vertex tree by adding a cheapest edge. 
• Get a 3-vertex tree by adding a cheapest edge. 
• Grow the tree one edge at a time until the tree 
has n - 1 edges (and hence has all n vertices). 
3
Sollin’s Method 
10 14 
8 
1 3 5 7 
7 12 
9 
2 4 6 3 
2 4 6 8 
1 3 5 7 
4 6 3 
2 4 6 8 
2 • Start with a forest that has no edges. 
• Each component selects a least cost edge 
with which to connect to another 
component. 
• Duplicate selections are eliminated. 
• Cycles are possible when the graph has
Sollin’s Method 
8 10 14 
7 12 
9 
1 3 5 7 
2 4 6 3 
2 4 6 8 
10 14 
1 3 5 7 
7 
2 4 6 3 
2 4 6 8 
• Each component that remains selects a 
least cost edge with which to connect to 
another component. 
• Beware of duplicate selections and cycles.
Greedy Minimum-Cost Spanning Tree Methods 
• Can prove that all result in a minimum-cost 
spanning tree. 
• Prim’s method is fastest. 
 O(n2) using an implementation similar to that of 
Dijkstra’s shortest-path algorithm. 
 O(e + n log n) using a Fibonacci heap. 
• Kruskal’s uses union-find trees to run in 
O(n + e log e) time.
Pseudocode For Kruskal’s Method 
Start with an empty set T of edges. 
while (E is not empty && |T| != n-1) 
{ 
Let (u,v) be a least-cost edge in E. 
E = E - {(u,v)}. // delete edge from E 
if ((u,v) does not create a cycle in T) 
Add edge (u,v) to T. 
} 
if (| T | == n-1) T is a min-cost spanning tree. 
else Network has no spanning tree.
Data Structures For Kruskal’s Method 
Edge set E. 
Operations are: 
 Is E empty? 
 Select and remove a least-cost edge. 
Use a min heap of edges. 
 Initialize. O(e) time. 
 Remove and return least-cost edge. O(log e) time.
Data Structures For Kruskal’s Method 
Set of selected edges T. 
Operations are: 
 Does T have n - 1 edges? 
 Does the addition of an edge (u, v) to T result in a 
cycle? 
 Add an edge to T.
Data Structures For Kruskal’s Method 
Use an array linear list for the edges of T. 
 Does T have n - 1 edges? 
• Check size of linear list. O(1) time. 
 Does the addition of an edge (u, v) to T result in a 
cycle? 
• Not easy. 
 Add an edge to T. 
• Add at right end of linear list. O(1) time. 
Just use an array rather than ArrayLinearList.
Data Structures For Kruskal’s Method 
Does the addition of an edge (u, v) to T result in 
a cycle? 
1 3 5 7 
2 4 6 3 7 
2 4 6 8 
• Each component of T is a tree. 
• When u and v are in the same component, the 
addition of the edge (u,v) creates a cycle. 
• When u and v are in the different 
components, the addition of the edge (u,v) 
does not create a cycle.
Data Structures For Kruskal’s Method 
1 3 5 7 
2 4 6 3 7 
2 4 6 8 
• Each component of T is defined by the 
vertices in the component. 
• Represent each component as a set of 
vertices. 
 {1, 2, 3, 4}, {5, 6}, {7, 8} 
• Two vertices are in the same component iff 
they are in the same set of vertices.
Data Structures For Kruskal’s Method 
1 3 5 7 
2 4 6 3 7 
2 4 6 8 
• When an edge (u, v) is added to T, the two 
components that have vertices u and v 
combine to become a single component. 
• In our set representation of components, the 
set that has vertex u and the set that has 
vertex v are united. 
 {1, 2, 3, 4} + {5, 6} => {1, 2, 3, 4, 5, 6}
Data Structures For Kruskal’s Method 
• Initially, T is empty. 
1 3 5 7 
2 4 6 8 
• Initial sets are: 
 {1} {2} {3} {4} {5} {6} {7} {8} 
• Does the addition of an edge (u, v) to T result 
in a cycle? If not, add edge to T. 
s1 = find(u); s2 = find(v); 
if (s1 != s2) union(s1, s2);
Data Structures For Kruskal’s Method 
• Use FastUnionFind. 
• Initialize. 
 O(n) time. 
• At most 2e finds and n-1 unions. 
 Very close to O(n + e). 
• Min heap operations to get edges in 
increasing order of cost take O(e log e). 
• Overall complexity of Kruskal’s method is 
O(n + e log e).

More Related Content

What's hot

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
 
Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort
Varendra University Rajshahi-bangladesh
 
K means clustering | K Means ++
K means clustering | K Means ++K means clustering | K Means ++
K means clustering | K Means ++
sabbirantor
 
K means and dbscan
K means and dbscanK means and dbscan
K means and dbscan
Yan Xu
 
勾配法
勾配法勾配法
勾配法
貴之 八木
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
Alona Salva
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesStrand Life Sciences Pvt Ltd
 
Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering
Yan Xu
 
Principal component analysis
Principal component analysisPrincipal component analysis
Principal component analysis
Partha Sarathi Kar
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Delowar Hossain
 
Sorting
SortingSorting
Sorting
Zaid Shabbir
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
PCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and ToolkitsPCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and Toolkits
HopeBay Technologies, Inc.
 
Cluster analysis using k-means method in R
Cluster analysis using k-means method in RCluster analysis using k-means method in R
Cluster analysis using k-means method in R
Vladimir Bakhrushin
 

What's hot (20)

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
 
Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort
 
K means clustering | K Means ++
K means clustering | K Means ++K means clustering | K Means ++
K means clustering | K Means ++
 
K means and dbscan
K means and dbscanK means and dbscan
K means and dbscan
 
勾配法
勾配法勾配法
勾配法
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional Ones
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
08 clustering
08 clustering08 clustering
08 clustering
 
Data miningpresentation
Data miningpresentationData miningpresentation
Data miningpresentation
 
Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering
 
Principal component analysis
Principal component analysisPrincipal component analysis
Principal component analysis
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
PCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and ToolkitsPCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and Toolkits
 
Hierachical clustering
Hierachical clusteringHierachical clustering
Hierachical clustering
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Cluster analysis using k-means method in R
Cluster analysis using k-means method in RCluster analysis using k-means method in R
Cluster analysis using k-means method in R
 

Similar to Lec33

APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMDATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
ABIRAMIS87
 
MinSpanningTreespresantion.ppt
MinSpanningTreespresantion.pptMinSpanningTreespresantion.ppt
MinSpanningTreespresantion.ppt
baciprek
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
Ashwin Shiv
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
Graph 2
Graph 2Graph 2
Biological sequences analysis
Biological sequences analysisBiological sequences analysis
Biological sequences analysis
Davide Andrea Guastella
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
Graph Theory: Trees
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
Ashikur Rahman
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Counting trees.pptx
Counting trees.pptxCounting trees.pptx
Counting trees.pptx
mdshafiullah18
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
Unit3_1.pdf
Unit3_1.pdfUnit3_1.pdf
Unit3_1.pdf
Pratimakumari213460
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
Higher order elements
Higher order elementsHigher order elements
Higher order elements
Sakthivel Murugan
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 

Similar to Lec33 (20)

APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMDATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
 
MinSpanningTreespresantion.ppt
MinSpanningTreespresantion.pptMinSpanningTreespresantion.ppt
MinSpanningTreespresantion.ppt
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
 
Graph 2
Graph 2Graph 2
Graph 2
 
Biological sequences analysis
Biological sequences analysisBiological sequences analysis
Biological sequences analysis
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
 
Graph Theory: Trees
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
Counting trees.pptx
Counting trees.pptxCounting trees.pptx
Counting trees.pptx
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithm
 
Unit3_1.pdf
Unit3_1.pdfUnit3_1.pdf
Unit3_1.pdf
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
Higher order elements
Higher order elementsHigher order elements
Higher order elements
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 

More from Nikhil Chilwant

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 7
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec30
Lec30Lec30
Lec30
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 
Lec16
Lec16Lec16
Lec16
 
Lec15
Lec15Lec15
Lec15
 
Lec14
Lec14Lec14
Lec14
 

Lec33

  • 1. Minimum-Cost Spanning Tree • weighted connected undirected graph • spanning tree • cost of spanning tree is sum of edge costs • find spanning tree that has minimum cost
  • 2. Example 8 10 14 7 12 1 3 5 7 2 4 6 3 2 4 6 8 9 • Network has 10 edges. • Spanning tree has only n - 1 = 7 edges. • Need to either select 7 edges or discard 3.
  • 3. Edge Selection Greedy Strategies • Start with an n-vertex 0-edge forest. Consider edges in ascending order of cost. Select edge if it does not form a cycle together with already selected edges.  Kruskal’s method. • Start with a 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge.  Prim’s method.
  • 4. Edge Selection Greedy Strategies • Start with an n-vertex forest. Each component/tree selects a least cost edge to connect to another component/tree. Eliminate duplicate selections and possible cycles. Repeat until only 1 component/tree is left.  Sollin’s method.
  • 5. Edge Rejection Greedy Strategies • Start with the connected graph. Repeatedly find a cycle and eliminate the highest cost edge on this cycle. Stop when no cycles remain. • Consider edges in descending order of cost. Eliminate an edge provided this leaves behind a connected graph.
  • 6. Kruskal’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 1 3 5 7 2 4 6 8 • Start with a forest that has no edges. • Consider edges in ascending order of cost. • Edge (1,2) is considered first and added to the forest.
  • 7. Kruskal’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 1 3 5 7 7 2 4 6 3 2 4 6 8 • Edge (7,8) is considered next and added. • Edge (3,4) is considered next and added. • Edge (5,6) is considered next and added. • Edge (2,3) is considered next and added. • Edge (1,3) is considered next and rejected because it creates a cycle.
  • 8. Kruskal’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 10 1 3 5 7 7 2 4 6 3 2 4 6 8 • Edge (2,4) is considered next and rejected because it creates a cycle. • Edge (3,5) is considered next and added. • Edge (3,6) is considered next and rejected. • Edge (5,7) is considered next and added. 14
  • 9. Kruskal’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 10 14 1 3 5 7 7 2 4 6 3 2 4 6 8 • n - 1 edges have been selected and no cycle formed. • So we must have a spanning tree. • Cost is 46. • Min-cost spanning tree is unique when all edge costs are different.
  • 10. Prim’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 4 1 2 2 7 • Start with any single vertex tree. 5 6 6 3 10 4 7 14 8 • Get a 2-vertex tree by adding a cheapest edge. • Get a 3-vertex tree by adding a cheapest edge. • Grow the tree one edge at a time until the tree has n - 1 edges (and hence has all n vertices). 3
  • 11. Sollin’s Method 10 14 8 1 3 5 7 7 12 9 2 4 6 3 2 4 6 8 1 3 5 7 4 6 3 2 4 6 8 2 • Start with a forest that has no edges. • Each component selects a least cost edge with which to connect to another component. • Duplicate selections are eliminated. • Cycles are possible when the graph has
  • 12. Sollin’s Method 8 10 14 7 12 9 1 3 5 7 2 4 6 3 2 4 6 8 10 14 1 3 5 7 7 2 4 6 3 2 4 6 8 • Each component that remains selects a least cost edge with which to connect to another component. • Beware of duplicate selections and cycles.
  • 13. Greedy Minimum-Cost Spanning Tree Methods • Can prove that all result in a minimum-cost spanning tree. • Prim’s method is fastest.  O(n2) using an implementation similar to that of Dijkstra’s shortest-path algorithm.  O(e + n log n) using a Fibonacci heap. • Kruskal’s uses union-find trees to run in O(n + e log e) time.
  • 14. Pseudocode For Kruskal’s Method Start with an empty set T of edges. while (E is not empty && |T| != n-1) { Let (u,v) be a least-cost edge in E. E = E - {(u,v)}. // delete edge from E if ((u,v) does not create a cycle in T) Add edge (u,v) to T. } if (| T | == n-1) T is a min-cost spanning tree. else Network has no spanning tree.
  • 15. Data Structures For Kruskal’s Method Edge set E. Operations are:  Is E empty?  Select and remove a least-cost edge. Use a min heap of edges.  Initialize. O(e) time.  Remove and return least-cost edge. O(log e) time.
  • 16. Data Structures For Kruskal’s Method Set of selected edges T. Operations are:  Does T have n - 1 edges?  Does the addition of an edge (u, v) to T result in a cycle?  Add an edge to T.
  • 17. Data Structures For Kruskal’s Method Use an array linear list for the edges of T.  Does T have n - 1 edges? • Check size of linear list. O(1) time.  Does the addition of an edge (u, v) to T result in a cycle? • Not easy.  Add an edge to T. • Add at right end of linear list. O(1) time. Just use an array rather than ArrayLinearList.
  • 18. Data Structures For Kruskal’s Method Does the addition of an edge (u, v) to T result in a cycle? 1 3 5 7 2 4 6 3 7 2 4 6 8 • Each component of T is a tree. • When u and v are in the same component, the addition of the edge (u,v) creates a cycle. • When u and v are in the different components, the addition of the edge (u,v) does not create a cycle.
  • 19. Data Structures For Kruskal’s Method 1 3 5 7 2 4 6 3 7 2 4 6 8 • Each component of T is defined by the vertices in the component. • Represent each component as a set of vertices.  {1, 2, 3, 4}, {5, 6}, {7, 8} • Two vertices are in the same component iff they are in the same set of vertices.
  • 20. Data Structures For Kruskal’s Method 1 3 5 7 2 4 6 3 7 2 4 6 8 • When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component. • In our set representation of components, the set that has vertex u and the set that has vertex v are united.  {1, 2, 3, 4} + {5, 6} => {1, 2, 3, 4, 5, 6}
  • 21. Data Structures For Kruskal’s Method • Initially, T is empty. 1 3 5 7 2 4 6 8 • Initial sets are:  {1} {2} {3} {4} {5} {6} {7} {8} • Does the addition of an edge (u, v) to T result in a cycle? If not, add edge to T. s1 = find(u); s2 = find(v); if (s1 != s2) union(s1, s2);
  • 22. Data Structures For Kruskal’s Method • Use FastUnionFind. • Initialize.  O(n) time. • At most 2e finds and n-1 unions.  Very close to O(n + e). • Min heap operations to get edges in increasing order of cost take O(e log e). • Overall complexity of Kruskal’s method is O(n + e log e).