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

Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comHemant Gautam
 
IRJET - On Strong Blast Domination of Graphs
 IRJET - On Strong Blast Domination of Graphs IRJET - On Strong Blast Domination of Graphs
IRJET - On Strong Blast Domination of GraphsIRJET Journal
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms SortingManishPrajapati78
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithmevil eye
 
Radix sort concept
Radix sort conceptRadix sort concept
Radix sort conceptZeeshan Ali
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6DrkhanchanaR
 
10 merge sort
10 merge sort10 merge sort
10 merge sortirdginfo
 
IMACS 2015
IMACS 2015IMACS 2015
IMACS 2015Jun Xia
 
More multiplication properties of exponents
More multiplication properties of exponents More multiplication properties of exponents
More multiplication properties of exponents chrystal_brinson
 
Unit 2 dsuc(Stacks and Queue)
Unit 2 dsuc(Stacks and Queue)Unit 2 dsuc(Stacks and Queue)
Unit 2 dsuc(Stacks and Queue)Sweta Singh
 
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...AI Frontiers
 
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 PrismKyle Pearce
 
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 problemsAnish 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
 
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.pdfCS With Logic
 
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 slideWooSung Choi
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...KUSHDHIRRA2111026030
 
141222 graphulo ingraphblas
141222 graphulo ingraphblas141222 graphulo ingraphblas
141222 graphulo ingraphblasMIT
 
141205 graphulo ingraphblas
141205 graphulo ingraphblas141205 graphulo ingraphblas
141205 graphulo ingraphblasgraphulo
 
BDC-presentation
BDC-presentationBDC-presentation
BDC-presentationPavel Popa
 
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 answersAnimesh Chaturvedi
 
Hidden surface removal
Hidden surface removalHidden surface removal
Hidden surface removalAnkit Garg
 
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 queriesGábor Szárnyas
 

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...
 
141222 graphulo ingraphblas
141222 graphulo ingraphblas141222 graphulo ingraphblas
141222 graphulo ingraphblas
 
141205 graphulo ingraphblas
141205 graphulo ingraphblas141205 graphulo ingraphblas
141205 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 (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

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

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