SlideShare a Scribd company logo
1
4.5. Minimum Spanning Trees
•Definition of MST
•Generic MST algorithm
•Kruskal's algorithm
•Prim's algorithm
2
Definition of MST
• Let G=(V,E) be a connected, undirected graph.
• For each edge (u,v) in E, we have a weight w(u,v) specifying
the cost (length of edge) to connect u and v.
• We wish to find a (acyclic) subset T of E that connects all of
the vertices in V and whose total weight is minimized.
• Since the total weight is minimized, the subset T must be
acyclic (no circuit).
• Thus, T is a tree. We call it a spanning tree.
• The problem of determining the tree T is called the
minimum-spanning-tree problem.
3
Application of MST: an example
• In the design of electronic circuitry, it is often
necessary to make a set of pins electrically
equivalent by wiring them together.
• To interconnect n pins, we can use n-1 wires, each
connecting two pins.
• We want to minimize the total length of the wires.
• Minimum Spanning Trees can be used to model
this problem.
4
Electronic Circuits:
5
Electronic Circuits:
6
Here is an example of a connected graph
and its minimum spanning tree:
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
Notice that the tree is not unique:
replacing (b,c) with (a,h) yields another spanning tree
with the same minimum weight.
7
Growing a MST(Generic Algorithm)
• Set A is always a subset of some minimum spanning tree.
This property is called the invariant Property.
• An edge (u,v) is a safe edge for A if adding the edge to A
does not destroy the invariant.
• A safe edge is just the CORRECT edge to choose to add to
T.
GENERIC_MST(G,w)
1 A:={}
2 while A does not form a spanning tree do
3 find an edge (u,v) that is safe for A
4 A:=A∪{(u,v)}
5 return A
8
How to find a safe edge
We need some definitions and a theorem.
• A cut (S,V-S) of an undirected graph G=(V,E) is a
partition of V.
• An edge crosses the cut (S,V-S) if one of its
endpoints is in S and the other is in V-S.
• An edge is a light edge crossing a cut if its weight
is the minimum of any edge crossing the cut.
9
V-S↓
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
S↑ ↑ S
↓ V-S
• This figure shows a cut (S,V-S) of the
graph.
• The edge (d,c) is the unique light edge
crossing the cut.
10
Theorem 1
• Let G=(V,E) be a connected, undirected graph with a real-
valued weight function w defined on E.
• Let A be a subset of E that is included in some minimum
spanning tree for G.
• Let (S,V-S) be any cut of G such that for any edge (u, v) in
A, {u, v} ⊆ S or {u, v} ⊆ (V-S).
• Let (u,v) be a light edge crossing (S,V-S).
• Then, edge (u,v) is safe for A.
• Proof: Let Topt be a minimum spanning tree.(Blue)
• A --a subset of Topt and (u,v)-- a light edge crossing (S, V-S)
• If (u,v) is NOT safe, then (u,v) is not in T. (See the red edge in Figure)
• There MUST be another edge (u’, v’) in Topt crossing (S, V-S).(Green)
• We can replace (u’,v’) in Topt with (u, v) and get another treeT’opt
• Since (u, v) is light (the shortest edge connect crossing (S, V-S), T’opt is
also optimal. 1
2
1
1 1
1 1
1
11
Corollary .2
• Let G=(V,E) be a connected, undirected graph with a real-
valued weight function w defined on E.
• Let A be a subset of E that is included in some minimum
spanning tree for G.
• Let C be a connected component (tree) in the forest
GA=(V,A).
• Let (u,v) be a light edge (shortest among all edges
connecting C with others components) connecting C to
some other component in GA.
• Then, edge (u,v) is safe for A. (For Kruskal’s algorithm)
12
The algorithms of Kruskal and Prim
• The two algorithms are elaborations of the generic
algorithm.
• They each use a specific rule to determine a safe
edge in line 3 of GENERIC_MST.
• In Kruskal's algorithm,
– The set A is a forest.
– The safe edge added to A is always a least-weight edge
in the graph that connects two distinct components.
• In Prim's algorithm,
– The set A forms a single tree.
– The safe edge added to A is always a least-weight edge
connecting the tree to a vertex not in the tree.
13
Kruskal's algorithm(basic part)
1 (Sort the edges in an increasing order)
2 A:={}
3 while E is not empty do {
3 take an edge (u, v) that is shortest in E
and delete it from E
4 if u and v are in different components then
add (u, v) to A
}
Note: each time a shortest edge in E is considered.
14
Kruskal's algorithm (Fun Part, not required)
MST_KRUSKAL(G,w)
1 A:={}
2 for each vertex v in V[G]
3 do MAKE_SET(v)
4 sort the edges of E by nondecreasing weight w
5 for each edge (u,v) in E, in order by
nondecreasing weight
6 do if FIND_SET(u) != FIND_SET(v)
7 then A:=A {(u,v)}∪
8 UNION(u,v)
9 return A
(Disjoint set is discussed in Chapter 21, Page 498)
15
Disjoint-Set (Chapter 21, Page 498)
• Keep a collection of sets S1, S2, .., Sk,
– Each Si is a set, e,g, S1={v1, v2, v8}.
• Three operations
– Make-Set(x)-creates a new set whose only member is x.
– Union(x, y) –unites the sets that contain x and y, say, Sx
and Sy, into a new set that is the union of the two sets.
– Find-Set(x)-returns a pointer to the representative of the
set containing x.
– Each operation takes O(log n) time.
16
• Our implementation uses a disjoint-set data
structure to maintain several disjoint sets of
elements.
• Each set contains the vertices in a tree of the
current forest.
• 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) equals FIND_SET(v).
• The combining of trees is accomplished by the
UNION procedure.
• Running time O(|E| lg (|E|)). (The analysis is not required.)
17
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
The execution of Kruskal's algorithm (Moderate part)
•The edges are considered by the algorithm in sorted
order by weight.
•The edge under consideration at each step is shown
with a red weight number.
18
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
19
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
20
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
21
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
22
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
23
Prim's algorithm(basic part)
MST_PRIM(G,w,r)
1. A={}
2. S:={r} (r is an arbitrary node in V)
3. Q=V-{r};
4. while Q is not empty do {
5 take an edge (u, v) such that (1) u ∈S and v ∈ Q (v∉ S ) and
(u, v) is the shortest edge satisfying (1)
6 add (u, v) to A, add v to S and delete v from Q
}
24
Prim's algorithm
MST_PRIM(G,w,r)
1 for each u in Q do
2 key[u]:=∞
3 parent[u]:=NIL
4 key[r]:=0; parent[r]=NIL;
5 Q←V[Q]
6 while Q!={} do
7 u:=EXTRACT_MIN(Q); if parent[u]≠Nil print (u, parent[u])
8 for each v in Adj[u] do
9 if v in Q and w(u,v)<key[v]
10 then parent[v]:=u
11 key[v]:=w(u,v)
25
• Grow the minimum spanning tree from the root
vertex r.
• Q is a priority queue, holding all vertices that are
not in the tree now.
• key[v] is the minimum weight of any edge
connecting v to a vertex in the tree.
• parent[v] names the parent of v in the tree.
• When the algorithm terminates, Q is empty; the
minimum spanning tree A for G is thus
A={(v,parent[v]):v∈V-{r}}.
• Running time: O(||E||lg |V|).
26
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
The execution of Prim's algorithm(moderate part)
the root
vertex
27
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
28
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
29
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
30
a
b
h
c d
e
fg
i
4
8 7
9
10
144
2
2
6
1
7
11
8
Bottleneck spanning tree: A spanning tree of G whose
largest edge weight is minimum over all spanning trees of G.
The value of the bottleneck spanning tree is the weight of the
maximum-weight edge in T.
Theorem: A minimum spanning tree is also a bottleneck
spanning tree. (Challenge problem)
Ad

Recommended

Minimum spanning Tree
Minimum spanning Tree
Narendra Singh Patel
 
Kruskal Algorithm
Kruskal Algorithm
Bhavik Vashi
 
Prim's algorithm
Prim's algorithm
Pankaj Thakur
 
Spanning trees & applications
Spanning trees & applications
Tech_MX
 
Kruskal's algorithm
Kruskal's algorithm
Raj Kumar Ranabhat
 
Minimum spanning tree
Minimum spanning tree
Amit Kumar Rathi
 
Minimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 
Bellman ford algorithm
Bellman ford algorithm
MdSajjadulislamBappi
 
Kruskal’s Algorithm
Kruskal’s Algorithm
Syed Maniruzzaman Pabel
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Prims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
All pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
 
Spanning trees
Spanning trees
Shareb Ismaeel
 
Shortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Kruskal’s algorithm
Kruskal’s algorithm
Abdul Moiz Lakhani
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Kruskal Algorithm
Kruskal Algorithm
Snehasis Panigrahi
 
Minimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Travelling salesman dynamic programming
Travelling salesman dynamic programming
maharajdey
 
minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
MATCHING GRAPH THEORY
MATCHING GRAPH THEORY
garishma bhatia
 
Stressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
My presentation minimum spanning tree
My presentation minimum spanning tree
Alona Salva
 
PRIM'S ALGORITHM
PRIM'S ALGORITHM
garishma bhatia
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
Ibrahim Alfayoumi
 
Linkers
Linkers
Rahul Dhiman
 

More Related Content

What's hot (20)

Kruskal’s Algorithm
Kruskal’s Algorithm
Syed Maniruzzaman Pabel
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Prims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
All pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
 
Spanning trees
Spanning trees
Shareb Ismaeel
 
Shortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Kruskal’s algorithm
Kruskal’s algorithm
Abdul Moiz Lakhani
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Kruskal Algorithm
Kruskal Algorithm
Snehasis Panigrahi
 
Minimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Travelling salesman dynamic programming
Travelling salesman dynamic programming
maharajdey
 
minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
MATCHING GRAPH THEORY
MATCHING GRAPH THEORY
garishma bhatia
 
Stressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
My presentation minimum spanning tree
My presentation minimum spanning tree
Alona Salva
 
PRIM'S ALGORITHM
PRIM'S ALGORITHM
garishma bhatia
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Prims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
All pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Travelling salesman dynamic programming
Travelling salesman dynamic programming
maharajdey
 
Stressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
My presentation minimum spanning tree
My presentation minimum spanning tree
Alona Salva
 

Viewers also liked (11)

Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
Ibrahim Alfayoumi
 
Linkers
Linkers
Rahul Dhiman
 
Greedy minimum spanning tree- prim's algorithm
Greedy minimum spanning tree- prim's algorithm
Department of Information Management Ming Chuan University, Taiwan
 
Linkers
Linkers
Koganti Ravikumar
 
Prims Algorithm
Prims Algorithm
Sriram Raj
 
Kruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
Ifad Rahman
 
Kruskal algorithms
Kruskal algorithms
Maher Alshammari
 
Loaders
Loaders
Mohd Arif
 
Project PPT slides_student_id#104519347
Project PPT slides_student_id#104519347
savan Darji
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitra
guest1f4fb3
 
Prims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
Ad

Similar to minimum spanning trees Algorithm (20)

Daa chapter13
Daa chapter13
B.Kirron Reddi
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
Traian Rebedea
 
Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Topological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
Ppt 1
Ppt 1
Sowbhagya Lakshmi
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
lecture 16
lecture 16
sajinsc
 
The Total Strong Split Domination Number of Graphs
The Total Strong Split Domination Number of Graphs
inventionjournals
 
The Floyd-Warshall of design and analysis of Algorithm.ppt
The Floyd-Warshall of design and analysis of Algorithm.ppt
MUhammadMiladAwan
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Chapter 24 aoa
Chapter 24 aoa
Hanif Durad
 
19 Minimum Spanning Trees
19 Minimum Spanning Trees
Andres Mendez-Vazquez
 
lec6.pptx
lec6.pptx
nuredinabdellah2
 
Graph algorithms
Graph algorithms
University of Haripur
 
04 greedyalgorithmsii
04 greedyalgorithmsii
LENIN Quintero
 
Weighted graphs
Weighted graphs
Core Condor
 
Optimisation random graph presentation
Optimisation random graph presentation
Venkat Sai Sharath Mudhigonda
 
Trees amd properties slide for presentaton
Trees amd properties slide for presentaton
SHARANSASI1
 
graph.ppt
graph.ppt
RakeshPandey951330
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
Traian Rebedea
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
lecture 16
lecture 16
sajinsc
 
The Total Strong Split Domination Number of Graphs
The Total Strong Split Domination Number of Graphs
inventionjournals
 
The Floyd-Warshall of design and analysis of Algorithm.ppt
The Floyd-Warshall of design and analysis of Algorithm.ppt
MUhammadMiladAwan
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Trees amd properties slide for presentaton
Trees amd properties slide for presentaton
SHARANSASI1
 
Ad

Recently uploaded (20)

Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 

minimum spanning trees Algorithm

  • 1. 1 4.5. Minimum Spanning Trees •Definition of MST •Generic MST algorithm •Kruskal's algorithm •Prim's algorithm
  • 2. 2 Definition of MST • Let G=(V,E) be a connected, undirected graph. • For each edge (u,v) in E, we have a weight w(u,v) specifying the cost (length of edge) to connect u and v. • We wish to find a (acyclic) subset T of E that connects all of the vertices in V and whose total weight is minimized. • Since the total weight is minimized, the subset T must be acyclic (no circuit). • Thus, T is a tree. We call it a spanning tree. • The problem of determining the tree T is called the minimum-spanning-tree problem.
  • 3. 3 Application of MST: an example • In the design of electronic circuitry, it is often necessary to make a set of pins electrically equivalent by wiring them together. • To interconnect n pins, we can use n-1 wires, each connecting two pins. • We want to minimize the total length of the wires. • Minimum Spanning Trees can be used to model this problem.
  • 6. 6 Here is an example of a connected graph and its minimum spanning tree: a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 Notice that the tree is not unique: replacing (b,c) with (a,h) yields another spanning tree with the same minimum weight.
  • 7. 7 Growing a MST(Generic Algorithm) • Set A is always a subset of some minimum spanning tree. This property is called the invariant Property. • An edge (u,v) is a safe edge for A if adding the edge to A does not destroy the invariant. • A safe edge is just the CORRECT edge to choose to add to T. GENERIC_MST(G,w) 1 A:={} 2 while A does not form a spanning tree do 3 find an edge (u,v) that is safe for A 4 A:=A∪{(u,v)} 5 return A
  • 8. 8 How to find a safe edge We need some definitions and a theorem. • A cut (S,V-S) of an undirected graph G=(V,E) is a partition of V. • An edge crosses the cut (S,V-S) if one of its endpoints is in S and the other is in V-S. • An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.
  • 9. 9 V-S↓ a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 S↑ ↑ S ↓ V-S • This figure shows a cut (S,V-S) of the graph. • The edge (d,c) is the unique light edge crossing the cut.
  • 10. 10 Theorem 1 • Let G=(V,E) be a connected, undirected graph with a real- valued weight function w defined on E. • Let A be a subset of E that is included in some minimum spanning tree for G. • Let (S,V-S) be any cut of G such that for any edge (u, v) in A, {u, v} ⊆ S or {u, v} ⊆ (V-S). • Let (u,v) be a light edge crossing (S,V-S). • Then, edge (u,v) is safe for A. • Proof: Let Topt be a minimum spanning tree.(Blue) • A --a subset of Topt and (u,v)-- a light edge crossing (S, V-S) • If (u,v) is NOT safe, then (u,v) is not in T. (See the red edge in Figure) • There MUST be another edge (u’, v’) in Topt crossing (S, V-S).(Green) • We can replace (u’,v’) in Topt with (u, v) and get another treeT’opt • Since (u, v) is light (the shortest edge connect crossing (S, V-S), T’opt is also optimal. 1 2 1 1 1 1 1 1
  • 11. 11 Corollary .2 • Let G=(V,E) be a connected, undirected graph with a real- valued weight function w defined on E. • Let A be a subset of E that is included in some minimum spanning tree for G. • Let C be a connected component (tree) in the forest GA=(V,A). • Let (u,v) be a light edge (shortest among all edges connecting C with others components) connecting C to some other component in GA. • Then, edge (u,v) is safe for A. (For Kruskal’s algorithm)
  • 12. 12 The algorithms of Kruskal and Prim • The two algorithms are elaborations of the generic algorithm. • They each use a specific rule to determine a safe edge in line 3 of GENERIC_MST. • In Kruskal's algorithm, – The set A is a forest. – The safe edge added to A is always a least-weight edge in the graph that connects two distinct components. • In Prim's algorithm, – The set A forms a single tree. – The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in the tree.
  • 13. 13 Kruskal's algorithm(basic part) 1 (Sort the edges in an increasing order) 2 A:={} 3 while E is not empty do { 3 take an edge (u, v) that is shortest in E and delete it from E 4 if u and v are in different components then add (u, v) to A } Note: each time a shortest edge in E is considered.
  • 14. 14 Kruskal's algorithm (Fun Part, not required) MST_KRUSKAL(G,w) 1 A:={} 2 for each vertex v in V[G] 3 do MAKE_SET(v) 4 sort the edges of E by nondecreasing weight w 5 for each edge (u,v) in E, in order by nondecreasing weight 6 do if FIND_SET(u) != FIND_SET(v) 7 then A:=A {(u,v)}∪ 8 UNION(u,v) 9 return A (Disjoint set is discussed in Chapter 21, Page 498)
  • 15. 15 Disjoint-Set (Chapter 21, Page 498) • Keep a collection of sets S1, S2, .., Sk, – Each Si is a set, e,g, S1={v1, v2, v8}. • Three operations – Make-Set(x)-creates a new set whose only member is x. – Union(x, y) –unites the sets that contain x and y, say, Sx and Sy, into a new set that is the union of the two sets. – Find-Set(x)-returns a pointer to the representative of the set containing x. – Each operation takes O(log n) time.
  • 16. 16 • Our implementation uses a disjoint-set data structure to maintain several disjoint sets of elements. • Each set contains the vertices in a tree of the current forest. • 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) equals FIND_SET(v). • The combining of trees is accomplished by the UNION procedure. • Running time O(|E| lg (|E|)). (The analysis is not required.)
  • 17. 17 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 The execution of Kruskal's algorithm (Moderate part) •The edges are considered by the algorithm in sorted order by weight. •The edge under consideration at each step is shown with a red weight number.
  • 18. 18 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 19. 19 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 20. 20 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 21. 21 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 22. 22 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 23. 23 Prim's algorithm(basic part) MST_PRIM(G,w,r) 1. A={} 2. S:={r} (r is an arbitrary node in V) 3. Q=V-{r}; 4. while Q is not empty do { 5 take an edge (u, v) such that (1) u ∈S and v ∈ Q (v∉ S ) and (u, v) is the shortest edge satisfying (1) 6 add (u, v) to A, add v to S and delete v from Q }
  • 24. 24 Prim's algorithm MST_PRIM(G,w,r) 1 for each u in Q do 2 key[u]:=∞ 3 parent[u]:=NIL 4 key[r]:=0; parent[r]=NIL; 5 Q←V[Q] 6 while Q!={} do 7 u:=EXTRACT_MIN(Q); if parent[u]≠Nil print (u, parent[u]) 8 for each v in Adj[u] do 9 if v in Q and w(u,v)<key[v] 10 then parent[v]:=u 11 key[v]:=w(u,v)
  • 25. 25 • Grow the minimum spanning tree from the root vertex r. • Q is a priority queue, holding all vertices that are not in the tree now. • key[v] is the minimum weight of any edge connecting v to a vertex in the tree. • parent[v] names the parent of v in the tree. • When the algorithm terminates, Q is empty; the minimum spanning tree A for G is thus A={(v,parent[v]):v∈V-{r}}. • Running time: O(||E||lg |V|).
  • 26. 26 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 The execution of Prim's algorithm(moderate part) the root vertex
  • 27. 27 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 28. 28 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 29. 29 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8
  • 30. 30 a b h c d e fg i 4 8 7 9 10 144 2 2 6 1 7 11 8 Bottleneck spanning tree: A spanning tree of G whose largest edge weight is minimum over all spanning trees of G. The value of the bottleneck spanning tree is the weight of the maximum-weight edge in T. Theorem: A minimum spanning tree is also a bottleneck spanning tree. (Challenge problem)