SlideShare a Scribd company logo
1 of 40
Minimum Spanning Tree
Prepared by
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
A graph may have many spanning trees.
or or or
Some Spanning Trees from Graph AGraph A
Spanning Trees
All 16 of its Spanning TreesComplete Graph
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the Spanning Tree of
minimum cost for that graph.
5
7
2
1
3
4
2
1
3
Complete Graph Minimum Spanning Tree
Algorithms for Obtaining the Minimum Spanning Tree
• Kruskal's Algorithm
• Prim's Algorithm
Kruskal's Algorithm
 This algorithm creates a forest of trees.
 Initially the forest consists of n single node trees (and no edges).
 At each step, we add one edge (the cheapest one) so that it joins two
trees together.
 If it were to form a cycle, it would simply link two nodes that were
already part of a single connected tree, so that this edge would not be
needed.
The steps are:
1. The forest is constructed - with each node in a separate tree.
2. The edges are placed in a priority queue.
3. Until we've added n-1 edges,
1. Extract the cheapest edge from the queue,
2. If it forms a cycle, reject it,
3. Else add it to the forest. Adding it to the forest will join two
trees together.
Every step will have joined two trees in the forest together, so that at
the end, there will only be one tree in T.
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Complete Graph
1
4
2
5
2
5
4
3
4
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
A AB D
B B
B
C D
J C
C
E
F
D
D H
J E G
F FG I
G GI J
H J JI
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Sort Edges
(in reality they are placed in a priority
queue - not sorted - but sorting them
makes the algorithm easier to visualize)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Cycle
Don’t Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Cycle
Don’t Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
JI
1A D
4B C
4A B
Add Edge
4
1
2
2 1
3
32
4
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Minimum Spanning Tree Complete Graph
Analysis of Kruskal's Algorithm
Running Time = O(m log n) (m = edges, n = nodes)
Testing if an edge creates a cycle can be slow unless a complicated data
structure called a “union-find” structure is used.
It usually only has to check a small fraction of the edges, but in some
cases (like if there was a vertex connected to the graph by only one edge
and it was the longest edge) it would have to check all the edges.
This algorithm works best, of course, if the number of edges is kept to a
minimum.
Prim's Algorithm
 This algorithm starts with one node.
 It then, one by one, adds a node that is unconnected to the new graph
 Each time selecting the node whose connecting edge has the smallest
weight out of the available nodes’ connecting edges.
The steps are:
1. The new graph is constructed - with one node from the old graph.
2. While new graph has fewer than n nodes,
1. Find the node from the old graph with the smallest connecting
edge to the new graph,
2. Add it to the new graph
Every step will have joined one node, so that at the end we will have
one graph with all the nodes and it will be a minimum spanning tree of
the original graph.
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Complete Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
4
1
2
2 1
3
32
4
A
B C
D
E F
G
H
I
J
Complete Graph Minimum Spanning Tree
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Analysis of Prim's Algorithm
Running Time = O(m + n log n) (m = edges, n = nodes)
If a heap is not used, the run time will be O(n^2) instead of
O(m + n log n). However, using a heap complicates the code since
you’re complicating the data structure. A Fibonacci heap is the best kind
of heap to use, but again, it complicates the code.
Unlike Kruskal’s, it doesn’t need to see all of the graph at once. It can
deal with it one piece at a time. It also doesn’t need to worry if adding
an edge will create a cycle since this algorithm deals primarily with the
nodes, and not the edges.
For this algorithm the number of nodes needs to be kept to a minimum
in addition to the number of edges. For small graphs, the edges matter
more, while for large graphs the number of nodes matters more.
Assignment
Given a weighted undirected graph. Find the
sum of weights of edges of a Minimum
Spanning Tree using:
• Krushkal’s Algorithm
• Prim’s Algorithm
Sample Input and Output
• 4 5
• 1 2 7
• 1 4 6
• 4 2 9
• 4 3 8
• 2 3 6
• 19

More Related Content

What's hot

10 merge sort
10 merge sort10 merge sort
10 merge sort
irdginfo
 
IMACS 2015
IMACS 2015IMACS 2015
IMACS 2015
Jun Xia
 
More multiplication properties of exponents
More multiplication properties of exponents More multiplication properties of exponents
More multiplication properties of exponents
chrystal_brinson
 
Succinct Summarisation of Large Networks via Small Synthetic Representative G...
Succinct Summarisation of Large Networks via Small Synthetic Representative G...Succinct Summarisation of Large Networks via Small Synthetic Representative G...
Succinct Summarisation of Large Networks via Small Synthetic Representative G...
Jérôme KUNEGIS
 
actel fpga problems
actel fpga problemsactel fpga problems
actel fpga problems
Anish Gupta
 

What's hot (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.com
 
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
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
 
Radix sort concept
Radix sort conceptRadix sort concept
Radix sort concept
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Graphs
GraphsGraphs
Graphs
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
Hprec7.1
Hprec7.1Hprec7.1
Hprec7.1
 
IMACS 2015
IMACS 2015IMACS 2015
IMACS 2015
 
Unit 5 dsuc
Unit 5 dsucUnit 5 dsuc
Unit 5 dsuc
 
More multiplication properties of exponents
More multiplication properties of exponents More multiplication properties of exponents
More multiplication properties of exponents
 
Unit 2 dsuc(Stacks and Queue)
Unit 2 dsuc(Stacks and Queue)Unit 2 dsuc(Stacks and Queue)
Unit 2 dsuc(Stacks and Queue)
 
Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...
Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...
Training at AI Frontiers 2018 - LaiOffer Self-Driving-Car-lecture 2: Incremen...
 
Deriving the Formula for Volume of a Triangular Prism
Deriving the Formula for Volume of a Triangular PrismDeriving the Formula for Volume of a Triangular Prism
Deriving the Formula for Volume of a Triangular Prism
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Succinct Summarisation of Large Networks via Small Synthetic Representative G...
Succinct Summarisation of Large Networks via Small Synthetic Representative G...Succinct Summarisation of Large Networks via Small Synthetic Representative G...
Succinct Summarisation of Large Networks via Small Synthetic Representative G...
 
actel fpga problems
actel fpga problemsactel fpga problems
actel fpga problems
 

Similar to Minimum Spanning Tree

Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
BDC-presentation
BDC-presentationBDC-presentation
BDC-presentation
Pavel Popa
 
Hidden surface removal
Hidden surface removalHidden surface removal
Hidden surface removal
Ankit Garg
 

Similar to Minimum Spanning Tree (20)

MST2.ppt
MST2.pptMST2.ppt
MST2.ppt
 
MST
MSTMST
MST
 
MST
MSTMST
MST
 
LalitBDA2015V3
LalitBDA2015V3LalitBDA2015V3
LalitBDA2015V3
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
 
A* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdfA* and Min-Max Searching Algorithms in AI , DSA.pdf
A* and Min-Max Searching Algorithms in AI , DSA.pdf
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
An optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slideAn optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slide
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
141205 graphulo ingraphblas
141205 graphulo ingraphblas141205 graphulo ingraphblas
141205 graphulo ingraphblas
 
141222 graphulo ingraphblas
141222 graphulo ingraphblas141222 graphulo ingraphblas
141222 graphulo ingraphblas
 
Unit3_1.pdf
Unit3_1.pdfUnit3_1.pdf
Unit3_1.pdf
 
Counting trees.pptx
Counting trees.pptxCounting trees.pptx
Counting trees.pptx
 
Ch06
Ch06Ch06
Ch06
 
Graph theory
Graph theory Graph theory
Graph theory
 
BDC-presentation
BDC-presentationBDC-presentation
BDC-presentation
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
Hpc sys
Hpc sysHpc sys
Hpc sys
 
Hidden surface removal
Hidden surface removalHidden surface removal
Hidden surface removal
 
GraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queriesGraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queries
 

More from Md. Shafiuzzaman Hira

More from Md. Shafiuzzaman Hira (20)

Introduction to Web development
Introduction to Web developmentIntroduction to Web development
Introduction to Web development
 
Software measurement and estimation
Software measurement and estimationSoftware measurement and estimation
Software measurement and estimation
 
Why do we test software?
Why do we test software?Why do we test software?
Why do we test software?
 
Software Requirements engineering
Software Requirements engineeringSoftware Requirements engineering
Software Requirements engineering
 
Software architectural patterns
Software architectural patternsSoftware architectural patterns
Software architectural patterns
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
 
Class diagram
Class diagramClass diagram
Class diagram
 
State diagram
State diagramState diagram
State diagram
 
Use case Modeling
Use case ModelingUse case Modeling
Use case Modeling
 
User stories
User storiesUser stories
User stories
 
Dev ops
Dev opsDev ops
Dev ops
 
Agile Methodology
Agile MethodologyAgile Methodology
Agile Methodology
 
Software Process Model
Software Process ModelSoftware Process Model
Software Process Model
 
Introduction to Software Engineering Course
Introduction to Software Engineering CourseIntroduction to Software Engineering Course
Introduction to Software Engineering Course
 
C files
C filesC files
C files
 
C pointers
C pointersC pointers
C pointers
 
C structures
C structuresC structures
C structures
 
How to Create Python scripts
How to Create Python scriptsHow to Create Python scripts
How to Create Python scripts
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
 
Password locker project
Password locker project Password locker project
Password locker project
 

Recently uploaded

Recently uploaded (20)

Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
The UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, OcadoThe UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, Ocado
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 

Minimum Spanning Tree

  • 1. Minimum Spanning Tree Prepared by Md. Shafiuzzaman Lecturer, Dept. of CSE, JUST
  • 2. A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. or or or Some Spanning Trees from Graph AGraph A Spanning Trees
  • 3. All 16 of its Spanning TreesComplete Graph
  • 4. Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 Complete Graph Minimum Spanning Tree
  • 5. Algorithms for Obtaining the Minimum Spanning Tree • Kruskal's Algorithm • Prim's Algorithm
  • 6. Kruskal's Algorithm  This algorithm creates a forest of trees.  Initially the forest consists of n single node trees (and no edges).  At each step, we add one edge (the cheapest one) so that it joins two trees together.  If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.
  • 7. The steps are: 1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.
  • 8. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Complete Graph
  • 9. 1 4 2 5 2 5 4 3 4 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J A AB D B B B C D J C C E F D D H J E G F FG I G GI J H J JI
  • 10. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize)
  • 11. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 12. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 13. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 14. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 15. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 16. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Cycle Don’t Add Edge
  • 17. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 18. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 19. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 20. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Cycle Don’t Add Edge
  • 21. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge
  • 22. 4 1 2 2 1 3 32 4 A B C D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Minimum Spanning Tree Complete Graph
  • 23. Analysis of Kruskal's Algorithm Running Time = O(m log n) (m = edges, n = nodes) Testing if an edge creates a cycle can be slow unless a complicated data structure called a “union-find” structure is used. It usually only has to check a small fraction of the edges, but in some cases (like if there was a vertex connected to the graph by only one edge and it was the longest edge) it would have to check all the edges. This algorithm works best, of course, if the number of edges is kept to a minimum.
  • 24. Prim's Algorithm  This algorithm starts with one node.  It then, one by one, adds a node that is unconnected to the new graph  Each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges.
  • 25. The steps are: 1. The new graph is constructed - with one node from the old graph. 2. While new graph has fewer than n nodes, 1. Find the node from the old graph with the smallest connecting edge to the new graph, 2. Add it to the new graph Every step will have joined one node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph.
  • 26. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Complete Graph
  • 27. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 28. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 29. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 30. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 31. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 32. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 33. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 34. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 35. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 36. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 37. 4 1 2 2 1 3 32 4 A B C D E F G H I J Complete Graph Minimum Spanning Tree 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J
  • 38. Analysis of Prim's Algorithm Running Time = O(m + n log n) (m = edges, n = nodes) If a heap is not used, the run time will be O(n^2) instead of O(m + n log n). However, using a heap complicates the code since you’re complicating the data structure. A Fibonacci heap is the best kind of heap to use, but again, it complicates the code. Unlike Kruskal’s, it doesn’t need to see all of the graph at once. It can deal with it one piece at a time. It also doesn’t need to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges. For this algorithm the number of nodes needs to be kept to a minimum in addition to the number of edges. For small graphs, the edges matter more, while for large graphs the number of nodes matters more.
  • 39. Assignment Given a weighted undirected graph. Find the sum of weights of edges of a Minimum Spanning Tree using: • Krushkal’s Algorithm • Prim’s Algorithm
  • 40. Sample Input and Output • 4 5 • 1 2 7 • 1 4 6 • 4 2 9 • 4 3 8 • 2 3 6 • 19