SlideShare a Scribd company logo
Graphs
19CSE212 : Data Structures and Algorithms
Dr. Chandan Kumar
Department of Computer Science and Engineering
Jan - Jun 2024
Dr Chandan Kumar Jan - Jun 2024
MinimumSpanningTree
TREE
B
A
F
E
C
D
Connected acyclic graph
Tree with n nodes contains
exactly n-1 edges.
GRAPH
Graph with n nodes contains
less than or equal to n(n-1)/2
edges.
A connected,
undirected
graph
Four of the spanning trees of the graph
SPANNING TREE...
Suppose you have a connected undirected graph
Connected: every node is reachable from every
other node
Undirected: edges do not have an associated
direction
...then a spanning tree of the graph is a connected
subgraph in which there are no cycles
EXAMPLE..
Minimizing costs
Suppose you want to supply a set of houses (say, in a new
subdivision) with:
electric power
water
sewage lines
telephone lines
To keep costs down, you could connect these houses with a
spanning tree (of, for example, power lines)
However, the houses are not all equal distances apart
To reduce costs even further, you could connect the
houses with a minimum-cost spanning tree
A cable company want to connect five villages to their network
which currently extends to the market town of Avonford. What
is the minimum length of cable needed?
Avonford Fingley
Brinleigh Cornwel
l
Donster
Edan
2
7
4
5
8 6
4
5
3
8
Example
MINIMUM SPANNING
TREE
Let G = (N, A) be a connected, undirected graph where
N is the set of nodes and A is the set of edges. Each
edge has a given nonnegative length. The problem is to
find a subset T of the edges of G such that all the
nodes remain connected when only the edges in T are
used, and the sum of the lengths of the edges in T is
as small as possible possible. Since G is connected, at
least one solution must exist.
Finding Spanning
Trees
There are two basic algorithms for finding minimum-cost
spanning trees, and both are greedy algorithms
Kruskal’s algorithm:
Created in 1957 by Joseph Kruskal
Prim’s algorithm
Created by Robert C. Prim
We model the situation as a network, then the problem is
to find the minimum connector for the network
A F
B C
D
E
2
7
4
5
8 6
4
5
3
8
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
List the edges in
order of size:
ED 2
AB 3
AE 4
CD 4
BC 5
EF 5
CF 6
AF 7
BF 8
CF 8
Kruskal’s Algorithm
Select the shortest
edge in the network
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest
edge which does not
create a cycle
ED 2
AB 3
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4 (or AE 4)
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest edge
which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
BC 5 – forms a cycle
EF 5
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
All vertices have
been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree:
18
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Algorithm
function Kruskal (G=(N,A): graph ; length : AR+):set of edges
{initialisation}
sort A by increasing length
N  the number of nodes in N
T  Ø {will contain the edges of the minimum spanning
tree}
initialise n sets, each containing the different element
of N
{greedy loop}
repeat
e  {u , v}  shortest edge not yet considered
ucomp  find(u)
vcomp  find(v)
if ucomp ≠ vcomp then
merge(ucomp , vcomp)
T  T Ú {e}
until T contains n-1 edges
return T
Kruskal’s Algorithm: complexity
Sorting loop:
O(a log n)
Initialization of components:
O(n)
Finding and merging: O(a
log n)
O(a log n)
Prim’s Algorithm
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe
vertices
Step 3: Select an edge e connecting the tree vertex
and fringe vertex that has minimum weight
Step 4: Add the selected edge and the vertex to the
minimum spanning tree T
[END OF LOOP]
Step 5: EXIT
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select any vertex
A
Select the
shortest edge
connected to that
vertex
AB 3
Prim’s Algorithm
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select the
shortest
edge connected to
any vertex already
connected.
AE 4
Prim’s Algorithm
Select the
shortest
edge connected to
any vertex already
connected.
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
Select the
shortest
edge connected to
any vertex already
connected.
DC 4
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
Select the
shortest
edge connected to
any vertex already
connected.
EF 5
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
All vertices have
been
connected.
The solution is
AB 3
AE 4
ED 2
DC 4
EF 5
Total weight of tree:
18
Prim’s Algorithm
Prim’s Algorithm
Complexity:
Outer loop: n-1 times
Inner loop: n times
O(n2)
a
b
h
c d
e
f
g
i
4
8 7
9
10
14
4
2
2
6
1
7
11
8
Example
a
b
h
c d
e
f
g
i
4
8 7
9
10
14
4
2
2
6
1
7
11
8
Solution
Minimum Connector Algorithms
Kruskal’s algorithm
1. Select the shortest
edge in a network
2. Select the next shortest
edge which does not
create a cycle
3. Repeat step 2 until all
vertices have been
connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest
edge connected to that
vertex
3. Select the shortest
edge connected to any
vertex already
connected
4. Repeat step 3 until all
vertices have been
connected
List of References
Dr Chandan Kumar Jan - Jun 2024
https://www.javatpoint.com/
https://www.tutorialspoint.com
https://www.geeksforgeeks.org/
https://www.programiz.com/dsa/
https://chat.openai.com/
Ad

Recommended

Minimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Minimum Spanning Tree
Minimum Spanning Tree
Balamurugan M
 
Spanning trees
Spanning trees
Shareb Ismaeel
 
minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
Minimum Spinning Tree Full Explaination pptx
Minimum Spinning Tree Full Explaination pptx
TayyabArif8
 
Graph Theory PPT presentation created by Selvam.
Graph Theory PPT presentation created by Selvam.
selfcinima
 
Minimum spanning tree
Minimum spanning tree
STEFFY D
 
Ram minimum spanning tree
Ram minimum spanning tree
Rama Prasath A
 
7. Spanning trees
7. Spanning trees
Mandeep Singh
 
Minimum Spanning Tree
Minimum Spanning Tree
Md. Shafiuzzaman Hira
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
17 prims-kruskals (1)
17 prims-kruskals (1)
MOHAMMADATHARKHAN2
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
RavanGulmetov
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Data Structures and Algorithms Kruskals algorithm
Data Structures and Algorithms Kruskals algorithm
deeps805023
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Animesh Chaturvedi
 
Use of the Tree.
Use of the Tree.
Nareender Kumar
 
MST
MST
A. S. M. Shafi
 
Skiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning trees
zukun
 
Tree
Tree
Simran Kaur
 
Data structure
Data structure
SangeethaSasi1
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Unit 5 graphs minimum spanning trees
Unit 5 graphs minimum spanning trees
kalyanineve
 
Minimum spanning tree
Minimum spanning tree
AhmedMalik74
 
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
chandankumar364348
 
Introduction and basic of Trees and Binary Trees
Introduction and basic of Trees and Binary Trees
chandankumar364348
 

More Related Content

Similar to Minimum Spanning Tree (Data Structure and Algorithm) (20)

7. Spanning trees
7. Spanning trees
Mandeep Singh
 
Minimum Spanning Tree
Minimum Spanning Tree
Md. Shafiuzzaman Hira
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
17 prims-kruskals (1)
17 prims-kruskals (1)
MOHAMMADATHARKHAN2
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
RavanGulmetov
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Data Structures and Algorithms Kruskals algorithm
Data Structures and Algorithms Kruskals algorithm
deeps805023
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Animesh Chaturvedi
 
Use of the Tree.
Use of the Tree.
Nareender Kumar
 
MST
MST
A. S. M. Shafi
 
Skiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning trees
zukun
 
Tree
Tree
Simran Kaur
 
Data structure
Data structure
SangeethaSasi1
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Unit 5 graphs minimum spanning trees
Unit 5 graphs minimum spanning trees
kalyanineve
 
Minimum spanning tree
Minimum spanning tree
AhmedMalik74
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
RavanGulmetov
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Data Structures and Algorithms Kruskals algorithm
Data Structures and Algorithms Kruskals algorithm
deeps805023
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Animesh Chaturvedi
 
Skiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning trees
zukun
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Unit 5 graphs minimum spanning trees
Unit 5 graphs minimum spanning trees
kalyanineve
 
Minimum spanning tree
Minimum spanning tree
AhmedMalik74
 

More from chandankumar364348 (6)

Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
chandankumar364348
 
Introduction and basic of Trees and Binary Trees
Introduction and basic of Trees and Binary Trees
chandankumar364348
 
Level of Program Correctness_Program_Reasoning.pptx
Level of Program Correctness_Program_Reasoning.pptx
chandankumar364348
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
Multi-class Alzheimer’s Disease Classification Using Deep Learning Techniques
chandankumar364348
 
Introduction and basic of Trees and Binary Trees
Introduction and basic of Trees and Binary Trees
chandankumar364348
 
Level of Program Correctness_Program_Reasoning.pptx
Level of Program Correctness_Program_Reasoning.pptx
chandankumar364348
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Ad

Recently uploaded (20)

Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
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
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
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
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Ad

Minimum Spanning Tree (Data Structure and Algorithm)

  • 1. Graphs 19CSE212 : Data Structures and Algorithms Dr. Chandan Kumar Department of Computer Science and Engineering Jan - Jun 2024 Dr Chandan Kumar Jan - Jun 2024
  • 3. TREE B A F E C D Connected acyclic graph Tree with n nodes contains exactly n-1 edges. GRAPH Graph with n nodes contains less than or equal to n(n-1)/2 edges.
  • 4. A connected, undirected graph Four of the spanning trees of the graph SPANNING TREE... Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction ...then a spanning tree of the graph is a connected subgraph in which there are no cycles
  • 6. Minimizing costs Suppose you want to supply a set of houses (say, in a new subdivision) with: electric power water sewage lines telephone lines To keep costs down, you could connect these houses with a spanning tree (of, for example, power lines) However, the houses are not all equal distances apart To reduce costs even further, you could connect the houses with a minimum-cost spanning tree
  • 7. A cable company want to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum length of cable needed? Avonford Fingley Brinleigh Cornwel l Donster Edan 2 7 4 5 8 6 4 5 3 8 Example
  • 8. MINIMUM SPANNING TREE Let G = (N, A) be a connected, undirected graph where N is the set of nodes and A is the set of edges. Each edge has a given nonnegative length. The problem is to find a subset T of the edges of G such that all the nodes remain connected when only the edges in T are used, and the sum of the lengths of the edges in T is as small as possible possible. Since G is connected, at least one solution must exist.
  • 9. Finding Spanning Trees There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms Kruskal’s algorithm: Created in 1957 by Joseph Kruskal Prim’s algorithm Created by Robert C. Prim
  • 10. We model the situation as a network, then the problem is to find the minimum connector for the network A F B C D E 2 7 4 5 8 6 4 5 3 8
  • 11. A F B C D E 2 7 4 5 8 6 4 5 3 8 List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 Kruskal’s Algorithm
  • 12. Select the shortest edge in the network ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 13. Select the next shortest edge which does not create a cycle ED 2 AB 3 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 14. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 15. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 16. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 17. All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 18. Algorithm function Kruskal (G=(N,A): graph ; length : AR+):set of edges {initialisation} sort A by increasing length N  the number of nodes in N T  Ø {will contain the edges of the minimum spanning tree} initialise n sets, each containing the different element of N {greedy loop} repeat e  {u , v}  shortest edge not yet considered ucomp  find(u) vcomp  find(v) if ucomp ≠ vcomp then merge(ucomp , vcomp) T  T Ú {e} until T contains n-1 edges return T
  • 19. Kruskal’s Algorithm: complexity Sorting loop: O(a log n) Initialization of components: O(n) Finding and merging: O(a log n) O(a log n)
  • 20. Prim’s Algorithm Step 1: Select a starting vertex Step 2: Repeat Steps 3 and 4 until there are fringe vertices Step 3: Select an edge e connecting the tree vertex and fringe vertex that has minimum weight Step 4: Add the selected edge and the vertex to the minimum spanning tree T [END OF LOOP] Step 5: EXIT
  • 21. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select any vertex A Select the shortest edge connected to that vertex AB 3 Prim’s Algorithm
  • 22. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. AE 4 Prim’s Algorithm
  • 23. Select the shortest edge connected to any vertex already connected. ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 24. Select the shortest edge connected to any vertex already connected. DC 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 25. Select the shortest edge connected to any vertex already connected. EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 26. A F B C D E 2 7 4 5 8 6 4 5 3 8 All vertices have been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 Prim’s Algorithm
  • 27. Prim’s Algorithm Complexity: Outer loop: n-1 times Inner loop: n times O(n2)
  • 30. Minimum Connector Algorithms Kruskal’s algorithm 1. Select the shortest edge in a network 2. Select the next shortest edge which does not create a cycle 3. Repeat step 2 until all vertices have been connected Prim’s algorithm 1. Select any vertex 2. Select the shortest edge connected to that vertex 3. Select the shortest edge connected to any vertex already connected 4. Repeat step 3 until all vertices have been connected
  • 31. List of References Dr Chandan Kumar Jan - Jun 2024 https://www.javatpoint.com/ https://www.tutorialspoint.com https://www.geeksforgeeks.org/ https://www.programiz.com/dsa/ https://chat.openai.com/