SlideShare a Scribd company logo
Data Structures and
Algorithms
Week 8: Minimum Spanning Trees
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 8
• Minimum Spanning Trees
• Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
Definition
• A Minimum Spanning Tree (MST) is a subgraph of
an undirected graph such that the subgraph spans
(includes) all nodes, is connected, is acyclic, and
has minimum total edge weight
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
Algorithm Characteristics
• Both Prim’s and Kruskal’s Algorithms work with
undirected graphs
• Both work with weighted and unweighted graphs but
are more interesting when edges are weighted
• Both are greedy algorithms that produce optimal
solutions
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
4
Prim’s Algorithm
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
Walk-Through
Initialize array
K dv pv
A F ∞ −
B F ∞ −
C F ∞ −
D F ∞ −
E F ∞ −
F F ∞ −
G F ∞ −
H F ∞ −
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Start with any node, say D
K dv pv
A
B
C
D T 0 −
E
F
G
H
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A
B
C 3 D
D T 0 −
E 25 D
F 18 D
G 2 D
H
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C 3 D
D T 0 −
E 25 D
F 18 D
G T 2 D
H
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A
B
C 3 D
D T 0 −
E 7 G
F 18 D
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C T 3 D
D T 0 −
E 7 G
F 18 D
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A
B 4 C
C T 3 D
D T 0 −
E 7 G
F 3 C
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B 4 C
C T 3 D
D T 0 −
E 7 G
F T 3 C
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 −
E 2 F
F T 3 C
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Table entries unchanged
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A 4 H
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected
nodes
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Table entries unchanged
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
4
A
H
B
F
E
D
C
G
2
3
4
3
3
Cost of Minimum
Spanning Tree = Σ dv = 21
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 −
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
Java Implementation
https://www.geeksforgeeks.org/prims-minimum-
spanning-tree-mst-greedy-algo-5/
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
Kruskal’s Algorithm
Work with edges, rather than nodes
Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
Walk-Through
Consider an undirected, weight graph
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
Sort the edges by increasing edge weight
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
30
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
32
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
33
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 χ
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 χ
(B,F) 4 χ
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 χ
(B,F) 4 χ
(B,H) 4 χ
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
36
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 χ
(B,F) 4 χ
(B,H) 4 χ
(A,H) 5 √
(D,F) 6
(A,B) 8
(A,F) 10
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
37
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 √
(D,G) 2 √
(E,G) 3 χ
(C,D) 3 √
(G,H) 3 √
(C,F) 3 √
(B,C) 4 √
5
1
A
H
B
F
E
D
C
G
2
3
3
3
edge dv
(B,E) 4 χ
(B,F) 4 χ
(B,H) 4 χ
(A,H) 5 √
(D,F) 6
(A,B) 8
(A,F) 10
Done
Total Cost = Σ dv = 21
4
}not
considere
d
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
38
Java Implementation
https://www.geeksforgeeks.org/kruskals-minimum-
spanning-tree-algorithm-greedy-algo-2/
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
39
Next Week
Search Algorithms
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
40

More Related Content

PDF
Data Structures and Algorithm - Week 5 - AVL Trees
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Data Structures and Algorithm - Week 3 - Stacks and Queues
PDF
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
PDF
Data Structures and Algorithm - Week 9 - Search Algorithms
PDF
Data Structures and Algorithm - Week 6 - Red Black Trees
PDF
Week 1 - Data Structures and Algorithms
PDF
Week 2 - Data Structures and Algorithms
Data Structures and Algorithm - Week 5 - AVL Trees
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 6 - Red Black Trees
Week 1 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms

What's hot (20)

PDF
geekgap.io webinar #1
PDF
Recommendation algorithm using reinforcement learning
PDF
Test for AI model
PPS
Data Structure
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PPT
Stacks in algorithems & data structure
PDF
Tracking the tracker: Time Series Analysis in Python from First Principles
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
PDF
[系列活動] Machine Learning 機器學習課程
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PDF
Week2: Programming for Data Analysis
PDF
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
PDF
林守德/Practical Issues in Machine Learning
PDF
Data structures and algorithm analysis in java
PPTX
[SIGIR17] Learning to Rank Using Localized Geometric Mean Metrics
PDF
Machine Learning and Model-Based Optimization for Heterogeneous Catalyst Desi...
PDF
Ranking and Diversity in Recommendations - RecSys Stammtisch at SoundCloud, B...
PDF
Top-K Dominating Queries on Incomplete Data with Priorities
geekgap.io webinar #1
Recommendation algorithm using reinforcement learning
Test for AI model
Data Structure
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
Stacks in algorithems & data structure
Tracking the tracker: Time Series Analysis in Python from First Principles
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
[系列活動] Machine Learning 機器學習課程
4. Recursion - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
Week2: Programming for Data Analysis
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
林守德/Practical Issues in Machine Learning
Data structures and algorithm analysis in java
[SIGIR17] Learning to Rank Using Localized Geometric Mean Metrics
Machine Learning and Model-Based Optimization for Heterogeneous Catalyst Desi...
Ranking and Diversity in Recommendations - RecSys Stammtisch at SoundCloud, B...
Top-K Dominating Queries on Incomplete Data with Priorities
Ad

Similar to Data Structures and Algorithm - Week 8 - Minimum Spanning Trees (20)

PDF
final_presentation
PDF
Comparative Study on DES and Triple DES Algorithms and Proposal of a New Algo...
PPTX
Compressing Graphs and Indexes with Recursive Graph Bisection
PDF
Sorting_project_2.pdf
PDF
Information technology syllabus
PDF
MS Thesis
PDF
MS Thesis
PPTX
FDS_dept_ppt.pptx
PDF
presentation
PPTX
Analysis of Tree in Computer Based Application
PPTX
An optimal and progressive algorithm for skyline queries slide
PPT
Boetticher Presentation Promise 2008v2
PPT
Instance Based Learning in Machine Learning
PDF
FAST Approaches to Scalable Similarity-based Test Case Prioritization
PPTX
Unsupervised learning networks
PDF
DATA STRUCTURES & ALGORITHMS MINIMUM SPANNING TREE
PPT
pattern_evaluaiton_methods.ppt
PDF
greedy algorithm the concept of Design & Analysis of Algorithms.pdf
PPTX
mod3-greedy (1).pptx design and analysis of algorithms
PDF
Multimodal Residual Learning for Visual Question-Answering
final_presentation
Comparative Study on DES and Triple DES Algorithms and Proposal of a New Algo...
Compressing Graphs and Indexes with Recursive Graph Bisection
Sorting_project_2.pdf
Information technology syllabus
MS Thesis
MS Thesis
FDS_dept_ppt.pptx
presentation
Analysis of Tree in Computer Based Application
An optimal and progressive algorithm for skyline queries slide
Boetticher Presentation Promise 2008v2
Instance Based Learning in Machine Learning
FAST Approaches to Scalable Similarity-based Test Case Prioritization
Unsupervised learning networks
DATA STRUCTURES & ALGORITHMS MINIMUM SPANNING TREE
pattern_evaluaiton_methods.ppt
greedy algorithm the concept of Design & Analysis of Algorithms.pdf
mod3-greedy (1).pptx design and analysis of algorithms
Multimodal Residual Learning for Visual Question-Answering
Ad

More from Ferdin Joe John Joseph PhD (20)

PDF
Invited Talk DGTiCon 2022
PDF
Week 12: Cloud AI- DSA 441 Cloud Computing
PDF
Week 11: Cloud Native- DSA 441 Cloud Computing
PDF
Week 10: Cloud Security- DSA 441 Cloud Computing
PDF
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
PDF
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
PDF
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
PDF
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
PDF
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
PDF
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
PDF
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
PDF
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
PDF
Hadoop in Alibaba Cloud
PDF
Cloud Computing Essentials in Alibaba Cloud
PDF
Transforming deep into transformers – a computer vision approach
PDF
Week 11: Programming for Data Analysis
PDF
Week 10: Programming for Data Analysis
PDF
Week 9: Programming for Data Analysis
PDF
Week 8: Programming for Data Analysis
Invited Talk DGTiCon 2022
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Hadoop in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
Transforming deep into transformers – a computer vision approach
Week 11: Programming for Data Analysis
Week 10: Programming for Data Analysis
Week 9: Programming for Data Analysis
Week 8: Programming for Data Analysis

Recently uploaded (20)

PDF
Fluorescence-microscope_Botany_detailed content
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Major-Components-ofNKJNNKNKNKNKronment.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Mega Projects Data Mega Projects Data
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPT
Quality review (1)_presentation of this 21
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
Supervised vs unsupervised machine learning algorithms
Fluorescence-microscope_Botany_detailed content
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
Business Acumen Training GuidePresentation.pptx
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Miokarditis (Inflamasi pada Otot Jantung)
IBA_Chapter_11_Slides_Final_Accessible.pptx
Major-Components-ofNKJNNKNKNKNKronment.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Introduction to Knowledge Engineering Part 1
Mega Projects Data Mega Projects Data
Galatica Smart Energy Infrastructure Startup Pitch Deck
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Quality review (1)_presentation of this 21
IB Computer Science - Internal Assessment.pptx
climate analysis of Dhaka ,Banglades.pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
Moving the Public Sector (Government) to a Digital Adoption
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Supervised vs unsupervised machine learning algorithms

Data Structures and Algorithm - Week 8 - Minimum Spanning Trees

  • 1. Data Structures and Algorithms Week 8: Minimum Spanning Trees Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2. Week 8 • Minimum Spanning Trees • Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3. Definition • A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 3
  • 4. Algorithm Characteristics • Both Prim’s and Kruskal’s Algorithms work with undirected graphs • Both work with weighted and unweighted graphs but are more interesting when edges are weighted • Both are greedy algorithms that produce optimal solutions Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 4
  • 5. Prim’s Algorithm Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6. Walk-Through Initialize array K dv pv A F ∞ − B F ∞ − C F ∞ − D F ∞ − E F ∞ − F F ∞ − G F ∞ − H F ∞ − 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Start with any node, say D K dv pv A B C D T 0 − E F G H 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0 − E 25 D F 18 D G 2 D H 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C 3 D D T 0 − E 25 D F 18 D G T 2 D H 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0 − E 7 G F 18 D G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C T 3 D D T 0 − E 7 G F 18 D G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B 4 C C T 3 D D T 0 − E 7 G F 3 C G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 12
  • 13. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B 4 C C T 3 D D T 0 − E 7 G F T 3 C G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0 − E 2 F F T 3 C G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H 3 G 2 Table entries unchanged Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A T 4 H B 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Table entries unchanged Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20
  • 21. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B T 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 21
  • 22. 4 A H B F E D C G 2 3 4 3 3 Cost of Minimum Spanning Tree = Σ dv = 21 K dv pv A T 4 H B T 4 C C T 3 D D T 0 − E T 2 F F T 3 C G T 2 D H T 3 G 2 Done Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 22
  • 23. Java Implementation https://www.geeksforgeeks.org/prims-minimum- spanning-tree-mst-greedy-algo-5/ Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 23
  • 24. Kruskal’s Algorithm Work with edges, rather than nodes Two steps: – Sort edges by increasing edge weight – Select the first |V| – 1 edges that do not generate a cycle Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 24
  • 25. Walk-Through Consider an undirected, weight graph 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 25
  • 26. Sort the edges by increasing edge weight edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 26
  • 27. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 27
  • 28. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 28
  • 29. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Accepting edge (E,G) would create a cycle Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 29
  • 30. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 30
  • 31. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 31
  • 32. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 32
  • 33. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 33
  • 34. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 34
  • 35. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 35
  • 36. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 36
  • 37. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 √ (D,F) 6 (A,B) 8 (A,F) 10 Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 37
  • 38. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1 √ (D,G) 2 √ (E,G) 3 χ (C,D) 3 √ (G,H) 3 √ (C,F) 3 √ (B,C) 4 √ 5 1 A H B F E D C G 2 3 3 3 edge dv (B,E) 4 χ (B,F) 4 χ (B,H) 4 χ (A,H) 5 √ (D,F) 6 (A,B) 8 (A,F) 10 Done Total Cost = Σ dv = 21 4 }not considere d Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 38
  • 39. Java Implementation https://www.geeksforgeeks.org/kruskals-minimum- spanning-tree-algorithm-greedy-algo-2/ Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 39
  • 40. Next Week Search Algorithms Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 40