SlideShare a Scribd company logo
1 of 8
Download to read offline
Introduction
A graph problem is given a set of vertices and (weighted) edges, find a
subset of edges that connects all the vertices and has minimum total weight giving a
minimum spanning tree (MST). An example of such a problem is determining the
minimum amount of copper needed to produce a common ground in an electronic
circuit. There are two well known algorithms for solving MST problems - Kruskal's
algorithm and Prim's algorithm.
4.2.1. Minimum Spanning Trees
Problem
Given a connected, undirected graph G(V,E) with weighted edges w(u,v),
find an acyclic subset of edges T ⊆ E that connect (or span) all the vertices and has
minumum total weight, i.e.
We call T a minimum spanning tree of G. While the value w(T) is unique, the tree
itself may not be.
Generic Algorithm
If A is a subset of an MST, then a safe edge is one such that
A' = A ∪ (u,v)
is also a subset of an MST.
We can then find an MST by beginning with any vertex and adding safe
edges until A' forms a spanning tree. This tree will contain |V| - 1 edges and be a
mininum spanning tree. Hence we simply need a way to determine safe edges.
A cut is a partition of V into two subsets {S} and {V-S}. An edge crosses the
cut if one vertex is in {S} and the other is in {V-S}. A set of edges A, respects a cut
if none of the edges in A crosses the cut. A light edge is an edge that crosses a cut
with minimum weight.
Using these definitions, we can prove the following theorem:
Given a connected, undirected graph G(V,E) with edge weights w(u,v)
If A is a subset of an MST, for any cut that respects A ⇒ a light edge is a safe edge
for A. (Note the converse is not true.)
An immediate corrolary is that if we have a forest of connected components (trees)
each with a set of minimum spanning edges, then any light edge between the trees is
a safe edge. Hence we can grow the MST by simply adding |V| - 1 light edges
between subsets of the MST.
4.2.2. Kruskal's Algorithm
A simple way to implement the generic algorithm is to grow the MST by
adding edges between trees in the forest in order of increasing weights until all trees
are connected. This approach is known as Kruskal's algorithm.
Algorithm
MST-KRUSKAL(G,w)
1. A = ∅
2. for each vertex v ∈ G.V
3. MAKE-SET(v)
4. sort the edges of G.E into nondecreasing order by weight w
5. for each edge (u,v) ∈ G.E, taken in nondecreasing order by weight
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
Basically the algorithm works as follows:
1. Make each vertex a separate tree
2. Sort the edges in nondecreasing order
3. Add an edge if it connects different trees and merge the trees together
The run time of Kruskal's algorithm depends on the implementation of the set
operations, but can be made to run in O(E log V).
Example
Given the following undirected graph
We first make each vertex a separate tree and sort the edges in no decreasing order
Step 1: Add edge (u1,u3) and merge u1 and u3
Step 2: Add edge (u2,u5) and merge u2 and u5
Step 3: Add edge (u1,u2) and merge the tree from step 1 with the tree from step 2
Step 4: Add edge (u3,u4) (note edge (u2,u3) does not connect separate trees) and
merge u4
The final edge (u1,u4) does not connect separate trees. Thus the final MST is
With total weight 11.
An alternative algorithm for finding MST's is Prim's algorithm which runs
asymptotically faster than Kruskal's algorithm, but at the price of requiring a queue
data structure.
4.2.3. Prim's Algorithm
Prim's algorithm finds MST's by growing the MST by adding light edges
between the current tree and other vertices. The vertices are stored in a minimum
priority queue based on the smallest weigh edge connecting vertices currently not
in the tree with a vertex in the tree.
Algorithm
MST-PRIM(G,w,r)
1. for each u ∈ G.V
2. u.key = ∞
3. u.pi = NIL
4. r.key = 0
5. Q = G.V
6. while Q ≠ ∅
7. u = EXTRACT-MIN(Q)
8. for each v ∈ G.Adj[u]
9. if v ∈ Q and w(u,v) < v.key
10. v.pi = u
11. v.key = w(u,v)
Basically the algorithm works as follows:
1. Initialize Q and set the source (root) key to 0
2. While Q is not empty, dequeue the vertex with minimum weight edge and
add it to the tree by adding edge (u.π,u) to T
3. For each vertex v in Adj[u] that is still in Q, check if w(u,v) (the edge
weights from u for all vertices not in T) are less than the current v.key (the
current smallest edge weight) and if so update the predecessor and key
fields
The run time of Prim's algorithm depends on the implementation of the priority
queue, but can be made to run in O(E + V lg V) using a Fibonacci heap for the
priority queue.
Example
Using the same undirected graph from here
We will (arbitrarily) initialize Q with vertex 1 giving
Step 1: Dequeue vertex 1 and update Q (and reprioritizing) by changing u3.key = 2
(edge (u1,u3)), u2.key = 3 (edge (u1,u2)), u4.key = 6 (edge (u1,u4))
Step 2: Dequeue vertex 3 (adding edge (u1,u3) to T) and update Q (and
reprioritizing) by changing u4.key = 4 (edge (u3,u4))
Step 3: Dequeue vertex 2 (adding edge (u1,u2) to T) and update Q (and
reprioritizing) by changing u5.key = 2 (edge (u2,u5))
Step 4: Dequeue vertex 5 (adding edge (u2,u5) to T) with no updates to Q
Step 5: Dequeue vertex 4 (adding edge (u3,u4) to T) with no updates to Q
At this point Q = ∅ giving the final MST
With total weight 11.

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
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint setAbirami A
 
Neil Lambert - From D-branes to M-branes
Neil Lambert - From D-branes to M-branesNeil Lambert - From D-branes to M-branes
Neil Lambert - From D-branes to M-branesSEENET-MTP
 
IRJET - On Strong Blast Domination of Graphs
 IRJET - On Strong Blast Domination of Graphs IRJET - On Strong Blast Domination of Graphs
IRJET - On Strong Blast Domination of GraphsIRJET Journal
 
Bounds on inverse domination in squares of graphs
Bounds on inverse domination in squares of graphsBounds on inverse domination in squares of graphs
Bounds on inverse domination in squares of graphseSAT Journals
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123Ankita Goyal
 
Eh2 piezoelectric energy harvesting due to harmonic excitations
Eh2   piezoelectric energy harvesting due to harmonic excitationsEh2   piezoelectric energy harvesting due to harmonic excitations
Eh2 piezoelectric energy harvesting due to harmonic excitationsUniversity of Glasgow
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithmshreeuva
 
Phase transition and casimir effect
Phase transition and casimir effectPhase transition and casimir effect
Phase transition and casimir effectnguyenthamhn
 
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAs
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAsSuperconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAs
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAsEditor IJCATR
 
Shortest route and mst
Shortest route and mstShortest route and mst
Shortest route and mstAlona Salva
 
Very brief highlights on some key details 2
Very brief highlights on some key details 2Very brief highlights on some key details 2
Very brief highlights on some key details 2foxtrot jp R
 
EH1 - Reduced-order modelling for vibration energy harvesting
EH1 - Reduced-order modelling for vibration energy harvestingEH1 - Reduced-order modelling for vibration energy harvesting
EH1 - Reduced-order modelling for vibration energy harvestingUniversity of Glasgow
 
Eh3 analysis of nonlinear energy harvesters
Eh3   analysis of nonlinear energy harvestersEh3   analysis of nonlinear energy harvesters
Eh3 analysis of nonlinear energy harvestersUniversity of Glasgow
 
Complimentary Energy Method in structural analysis
Complimentary Energy Method in structural analysisComplimentary Energy Method in structural analysis
Complimentary Energy Method in structural analysisMahdi Damghani
 

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
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
 
Disjoint set
Disjoint setDisjoint set
Disjoint set
 
Neil Lambert - From D-branes to M-branes
Neil Lambert - From D-branes to M-branesNeil Lambert - From D-branes to M-branes
Neil Lambert - From D-branes to M-branes
 
IRJET - On Strong Blast Domination of Graphs
 IRJET - On Strong Blast Domination of Graphs IRJET - On Strong Blast Domination of Graphs
IRJET - On Strong Blast Domination of Graphs
 
Bounds on inverse domination in squares of graphs
Bounds on inverse domination in squares of graphsBounds on inverse domination in squares of graphs
Bounds on inverse domination in squares of graphs
 
07. disjoint set
07. disjoint set07. disjoint set
07. disjoint set
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123
 
20 Single Source Shorthest Path
20 Single Source Shorthest Path20 Single Source Shorthest Path
20 Single Source Shorthest Path
 
Eh2 piezoelectric energy harvesting due to harmonic excitations
Eh2   piezoelectric energy harvesting due to harmonic excitationsEh2   piezoelectric energy harvesting due to harmonic excitations
Eh2 piezoelectric energy harvesting due to harmonic excitations
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithm
 
Phase transition and casimir effect
Phase transition and casimir effectPhase transition and casimir effect
Phase transition and casimir effect
 
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAs
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAsSuperconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAs
Superconductivity and Spin Density Wave (SDW) in NaFe1-xCoxAs
 
17 prims-kruskals (1)
17 prims-kruskals (1)17 prims-kruskals (1)
17 prims-kruskals (1)
 
PhotonModel
PhotonModelPhotonModel
PhotonModel
 
Shortest route and mst
Shortest route and mstShortest route and mst
Shortest route and mst
 
Very brief highlights on some key details 2
Very brief highlights on some key details 2Very brief highlights on some key details 2
Very brief highlights on some key details 2
 
EH1 - Reduced-order modelling for vibration energy harvesting
EH1 - Reduced-order modelling for vibration energy harvestingEH1 - Reduced-order modelling for vibration energy harvesting
EH1 - Reduced-order modelling for vibration energy harvesting
 
Eh3 analysis of nonlinear energy harvesters
Eh3   analysis of nonlinear energy harvestersEh3   analysis of nonlinear energy harvesters
Eh3 analysis of nonlinear energy harvesters
 
Complimentary Energy Method in structural analysis
Complimentary Energy Method in structural analysisComplimentary Energy Method in structural analysis
Complimentary Energy Method in structural analysis
 

Similar to Daa chapter13

minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm sachin varun
 
lecture 16
lecture 16lecture 16
lecture 16sajinsc
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Traian Rebedea
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101zhendy94
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEA NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEijscmcj
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEA NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEijscmc
 
test pre
test pretest pre
test prefarazch
 
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
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Applied Graph Theory Applications
Applied Graph Theory ApplicationsApplied Graph Theory Applications
Applied Graph Theory Applicationsvipin3195
 
spanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfspanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfYasirAli74993
 

Similar to Daa chapter13 (20)

minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
lecture 16
lecture 16lecture 16
lecture 16
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
19 Minimum Spanning Trees
19 Minimum Spanning Trees19 Minimum Spanning Trees
19 Minimum Spanning Trees
 
MST
MSTMST
MST
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEA NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
 
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREEA NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
A NEW PARALLEL ALGORITHM FOR COMPUTING MINIMUM SPANNING TREE
 
8_MST_pptx.pptx
8_MST_pptx.pptx8_MST_pptx.pptx
8_MST_pptx.pptx
 
test pre
test pretest pre
test pre
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Data structure
Data structureData structure
Data structure
 
project final
project finalproject final
project final
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
Data structure
Data structureData structure
Data structure
 
Applied Graph Theory Applications
Applied Graph Theory ApplicationsApplied Graph Theory Applications
Applied Graph Theory Applications
 
spanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfspanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdf
 

More from B.Kirron Reddi

More from B.Kirron Reddi (17)

What after graduation_-_mba
What after graduation_-_mbaWhat after graduation_-_mba
What after graduation_-_mba
 
What after graduation_-_banks
What after graduation_-_banksWhat after graduation_-_banks
What after graduation_-_banks
 
What after graduation_-_mca
What after graduation_-_mcaWhat after graduation_-_mca
What after graduation_-_mca
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
 
Daa chapter10
Daa chapter10Daa chapter10
Daa chapter10
 
Daa chapter9
Daa chapter9Daa chapter9
Daa chapter9
 
Daa chapter8
Daa chapter8Daa chapter8
Daa chapter8
 
Daa chapter7
Daa chapter7Daa chapter7
Daa chapter7
 
Daa chapter6
Daa chapter6Daa chapter6
Daa chapter6
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Daa chapter4
Daa chapter4Daa chapter4
Daa chapter4
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
Daa contents by B.Kirron Reddi
Daa contents by B.Kirron ReddiDaa contents by B.Kirron Reddi
Daa contents by B.Kirron Reddi
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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 . pdfQucHHunhnh
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

Daa chapter13

  • 1. Introduction A graph problem is given a set of vertices and (weighted) edges, find a subset of edges that connects all the vertices and has minimum total weight giving a minimum spanning tree (MST). An example of such a problem is determining the minimum amount of copper needed to produce a common ground in an electronic circuit. There are two well known algorithms for solving MST problems - Kruskal's algorithm and Prim's algorithm. 4.2.1. Minimum Spanning Trees Problem Given a connected, undirected graph G(V,E) with weighted edges w(u,v), find an acyclic subset of edges T ⊆ E that connect (or span) all the vertices and has minumum total weight, i.e. We call T a minimum spanning tree of G. While the value w(T) is unique, the tree itself may not be. Generic Algorithm If A is a subset of an MST, then a safe edge is one such that A' = A ∪ (u,v) is also a subset of an MST. We can then find an MST by beginning with any vertex and adding safe edges until A' forms a spanning tree. This tree will contain |V| - 1 edges and be a mininum spanning tree. Hence we simply need a way to determine safe edges. A cut is a partition of V into two subsets {S} and {V-S}. An edge crosses the cut if one vertex is in {S} and the other is in {V-S}. A set of edges A, respects a cut if none of the edges in A crosses the cut. A light edge is an edge that crosses a cut with minimum weight. Using these definitions, we can prove the following theorem: Given a connected, undirected graph G(V,E) with edge weights w(u,v)
  • 2. If A is a subset of an MST, for any cut that respects A ⇒ a light edge is a safe edge for A. (Note the converse is not true.) An immediate corrolary is that if we have a forest of connected components (trees) each with a set of minimum spanning edges, then any light edge between the trees is a safe edge. Hence we can grow the MST by simply adding |V| - 1 light edges between subsets of the MST. 4.2.2. Kruskal's Algorithm A simple way to implement the generic algorithm is to grow the MST by adding edges between trees in the forest in order of increasing weights until all trees are connected. This approach is known as Kruskal's algorithm. Algorithm MST-KRUSKAL(G,w) 1. A = ∅ 2. for each vertex v ∈ G.V 3. MAKE-SET(v) 4. sort the edges of G.E into nondecreasing order by weight w 5. for each edge (u,v) ∈ G.E, taken in nondecreasing order by weight 6. if FIND-SET(u) ≠ FIND-SET(v) 7. A = A ∪ {(u,v)} 8. UNION(u,v) 9. return A Basically the algorithm works as follows: 1. Make each vertex a separate tree 2. Sort the edges in nondecreasing order 3. Add an edge if it connects different trees and merge the trees together The run time of Kruskal's algorithm depends on the implementation of the set operations, but can be made to run in O(E log V). Example Given the following undirected graph
  • 3. We first make each vertex a separate tree and sort the edges in no decreasing order Step 1: Add edge (u1,u3) and merge u1 and u3 Step 2: Add edge (u2,u5) and merge u2 and u5 Step 3: Add edge (u1,u2) and merge the tree from step 1 with the tree from step 2
  • 4. Step 4: Add edge (u3,u4) (note edge (u2,u3) does not connect separate trees) and merge u4 The final edge (u1,u4) does not connect separate trees. Thus the final MST is With total weight 11. An alternative algorithm for finding MST's is Prim's algorithm which runs asymptotically faster than Kruskal's algorithm, but at the price of requiring a queue data structure.
  • 5. 4.2.3. Prim's Algorithm Prim's algorithm finds MST's by growing the MST by adding light edges between the current tree and other vertices. The vertices are stored in a minimum priority queue based on the smallest weigh edge connecting vertices currently not in the tree with a vertex in the tree. Algorithm MST-PRIM(G,w,r) 1. for each u ∈ G.V 2. u.key = ∞ 3. u.pi = NIL 4. r.key = 0 5. Q = G.V 6. while Q ≠ ∅ 7. u = EXTRACT-MIN(Q) 8. for each v ∈ G.Adj[u] 9. if v ∈ Q and w(u,v) < v.key 10. v.pi = u 11. v.key = w(u,v) Basically the algorithm works as follows: 1. Initialize Q and set the source (root) key to 0 2. While Q is not empty, dequeue the vertex with minimum weight edge and add it to the tree by adding edge (u.π,u) to T 3. For each vertex v in Adj[u] that is still in Q, check if w(u,v) (the edge weights from u for all vertices not in T) are less than the current v.key (the current smallest edge weight) and if so update the predecessor and key fields The run time of Prim's algorithm depends on the implementation of the priority queue, but can be made to run in O(E + V lg V) using a Fibonacci heap for the priority queue. Example Using the same undirected graph from here
  • 6. We will (arbitrarily) initialize Q with vertex 1 giving Step 1: Dequeue vertex 1 and update Q (and reprioritizing) by changing u3.key = 2 (edge (u1,u3)), u2.key = 3 (edge (u1,u2)), u4.key = 6 (edge (u1,u4)) Step 2: Dequeue vertex 3 (adding edge (u1,u3) to T) and update Q (and reprioritizing) by changing u4.key = 4 (edge (u3,u4))
  • 7. Step 3: Dequeue vertex 2 (adding edge (u1,u2) to T) and update Q (and reprioritizing) by changing u5.key = 2 (edge (u2,u5)) Step 4: Dequeue vertex 5 (adding edge (u2,u5) to T) with no updates to Q Step 5: Dequeue vertex 4 (adding edge (u3,u4) to T) with no updates to Q
  • 8. At this point Q = ∅ giving the final MST With total weight 11.