SlideShare a Scribd company logo
Greedy method
• A greedy algorithm is an approach for solving a problem by
selecting the best option available at the moment, without
worrying about the future result it would bring.
• In other words, the locally best choices aim at producing
globally best results.
Advantages
1.The algorithm is easier to describe.
2.This algorithm can perform better than other algorithms
(but, not in all cases).
Greedy Algorithm
1.To begin with, the solution set (containing answers) is
empty.
2.At each step, an item is added into the solution set.
3.If the solution set is feasible, the current item is kept.
4.Else, the item is rejected and never considered again.
Basic Concepts
Spanning Trees: A subgraph T of a undirected graph G = ( V, E ) is a
spanning tree of G if it is a tree and contains every vertex of G.
a
b
c
d
e
a
b
c
d
e
a
b
c
d
e
a
b
c
e
d
Graph
Spanning Tree 1 Spanning Tree 2 Spanning Tree 3
• Every connected graph has a spanning tree.
• May have multiple spanning tree.
• For example see this graph.
Spanning Tree Cont…
Basic Concepts Cont…..
Weighted Graph: A weighted graph is a graph, in which each edge has
a weight (some real number ) Example:
a
b
c
10
9
e
d
Weighted Graph
7 32
23
Basic Concepts Cont….
Minimum Spanning Tree in an undirected connected weighted graph is
a spanning tree of minimum weight. Example:
b
c
10
9
e
d
Weighted Graph
a 7 32
23
a
c
d
Spanning Tree 1,
w=74
10
9 e
32
23
a
c
d
w=71
9 e
7 32
23
b b a
c
e
d
Spanning Tree 3,
w=72
7 32
10 23
b
Spanning Tree 2,
(Minimum Spanning Tree)
Minimum Spanning Tree Problem
MST Problem : Given a connected weighted undirected graph G,
design an algorithm that outputs a minimum spanning tree (MST) of
graph G.
• How to find Minimum Spanning Tree ?
• Generic solution to MST
Two
Algorithms
Kruskal’s
algorithm
Prim’s
algorithm
Applications of Minimum Spanning Tree
1.Consider n stations are to be linked using a communication network
& laying of communication links between any two stations involves a
cost.
The ideal solution would be to extract a subgraph termed as
minimum cost spanning tree.
2.Suppose you want to construct highways or railroads spanning
several cities then we can use the concept of minimum spanning
trees.
3.Designing Local Area Networks.
4.Laying pipelines connecting offshore drilling sites, refineries and
consumer markets.
5.Suppose you want to apply a set of houses with
1. Electric Power
2. Water
3. Telephone lines
4. Sewage lines
To reduce cost, you can connect houses with minimum cost spanning
trees.
Kruskal’s Algorithm
• The set of edges (T) is initially empty.
• As the algorithm progresses, edges are added to T at every instance.
• The partial graph formed by the nodes of G, and the edges in T
consists of several connected components.
• At the end of the algorithm, only the connected component remains,
so that T is then a minimum spanning tree of all nodes of G.
Example
Graph
The steps for implementing Kruskal's algorithm are as
follows:
1.Sort all the edges from low weight to high
2.Take the edge with the lowest weight and add it to
the spanning tree. If adding the edge created a cycle,
then reject this edge.
3.Keep adding edges until we reach all vertices.
• First step to solve is to arrange the edges in the increasing order of
their costs.
Edge Cost
1 - 2 5
1 - 3 10
2 – 6 20
3 - 6 25
4 - 5 35
2 - 4 45
5 - 6 45
4 - 6 50
2 - 5 55
• Next step is to create the following table
Step Edge Considered Connected
component
Graph
Initial - [1] [2] [3] [4] [5] [6] -
1. [1 – 2] [12] [3] [4] [5] [6]
Accepted because
no cycle
2. [1 – 3] [1 2 3] [4] [5] [6]
Accepted because
no cycle
1 2
5
1 2
3
5
10
Step Edge Considered Connected
Component
Graph
3. [2 – 6] [1 2 3 6] [4] [5]
Accepted because
no cycle
4. [3 – 6] Rejected because it
forms a cycle
-
5. [4 – 5] [1 2 3 6] [4 5]
Accepted because
no cycle
1 2
3
6
5
10
20
1 2
3
4 5
5
10 20 35
Step Edge Considered Connected
Component
Graph
6. [2 – 4] [1 2 3 4 5 6]
Accepted because no
cycle
7. [5 – 6] rejected -
8. [4 – 6] rejected
5
10
20
45
35
Step Edge
Considered
Connected
Component
Graph
8. [4 - 6] Rejected -
9. [2 – 5] Rejected -
Algorithm Kruskal(T,E,n)
• T is the spanning tree, E is the list of edges, n is the number of nodes in given graph G
{
initially T=0;
while ( T <> n-1 and E <> 0 )
{
find the min-cost edge in E and call it as (U,V)
if (U,V) does not form a cycle
T = T + (U,V)
else
delete(U,V);
}
if ( |T| = n-1 )
display “Spanning Tree,T”
else
display “No Spanning Tree”
}
Ad

Recommended

Minimum Spinning Tree Full Explaination pptx
Minimum Spinning Tree Full Explaination pptx
TayyabArif8
 
Data structure
Data structure
SangeethaSasi1
 
Graph Theory PPT presentation created by Selvam.
Graph Theory PPT presentation created by Selvam.
selfcinima
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
Greedy technique - Algorithm design techniques using data structures
Greedy technique - Algorithm design techniques using data structures
divyammo
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
Minimum Spanning Tree (Data Structure and Algorithm)
Minimum Spanning Tree (Data Structure and Algorithm)
chandankumar364348
 
Unit3_1.pdf
Unit3_1.pdf
Pratimakumari213460
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm
Mrunal Patil
 
Minimum Spanning Tree
Minimum Spanning Tree
Balamurugan M
 
19 Minimum Spanning Trees
19 Minimum Spanning Trees
Andres Mendez-Vazquez
 
Minimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
7. Algorithm Design and analysis ppt.pptx
7. Algorithm Design and analysis ppt.pptx
deivasigamani9
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
OTP, Phishing, QR code, Shares, Visual Cryptography.
OTP, Phishing, QR code, Shares, Visual Cryptography.
IJERA Editor
 
MST
MST
A. S. M. Shafi
 
Data structure
Data structure
kavitha muneeshwaran
 
Ram minimum spanning tree
Ram minimum spanning tree
Rama Prasath A
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
ABIRAMIS87
 
kruskal and prims algorithm _
kruskal and prims algorithm _
swati463221
 
1 sollins algorithm
1 sollins algorithm
Muhammad Salman
 
7. Spanning trees
7. Spanning trees
Mandeep Singh
 
Minimum Spanning Tree in design and analysis
Minimum Spanning Tree in design and analysis
2022imscs011
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
Ijciras1101
Ijciras1101
zhendy94
 
Lec33
Lec33
Nikhil Chilwant
 
Spanning trees
Spanning trees
Shareb Ismaeel
 
Boost Business Efficiency with Professional Data Entry Services
Boost Business Efficiency with Professional Data Entry Services
eloiacs eloiacs
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 

More Related Content

Similar to Data Structures and Algorithms Kruskals algorithm (20)

prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm
Mrunal Patil
 
Minimum Spanning Tree
Minimum Spanning Tree
Balamurugan M
 
19 Minimum Spanning Trees
19 Minimum Spanning Trees
Andres Mendez-Vazquez
 
Minimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
7. Algorithm Design and analysis ppt.pptx
7. Algorithm Design and analysis ppt.pptx
deivasigamani9
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
OTP, Phishing, QR code, Shares, Visual Cryptography.
OTP, Phishing, QR code, Shares, Visual Cryptography.
IJERA Editor
 
MST
MST
A. S. M. Shafi
 
Data structure
Data structure
kavitha muneeshwaran
 
Ram minimum spanning tree
Ram minimum spanning tree
Rama Prasath A
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
ABIRAMIS87
 
kruskal and prims algorithm _
kruskal and prims algorithm _
swati463221
 
1 sollins algorithm
1 sollins algorithm
Muhammad Salman
 
7. Spanning trees
7. Spanning trees
Mandeep Singh
 
Minimum Spanning Tree in design and analysis
Minimum Spanning Tree in design and analysis
2022imscs011
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
Ijciras1101
Ijciras1101
zhendy94
 
Lec33
Lec33
Nikhil Chilwant
 
Spanning trees
Spanning trees
Shareb Ismaeel
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
Minimum Spanning Tree using Kruskal's Algorithm
Minimum Spanning Tree using Kruskal's Algorithm
Mrunal Patil
 
Minimum Spanning Tree
Minimum Spanning Tree
Balamurugan M
 
7. Algorithm Design and analysis ppt.pptx
7. Algorithm Design and analysis ppt.pptx
deivasigamani9
 
Minimum spanning tree.pptx data structure programming
Minimum spanning tree.pptx data structure programming
Arjunkrish9
 
OTP, Phishing, QR code, Shares, Visual Cryptography.
OTP, Phishing, QR code, Shares, Visual Cryptography.
IJERA Editor
 
Ram minimum spanning tree
Ram minimum spanning tree
Rama Prasath A
 
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
ABIRAMIS87
 
kruskal and prims algorithm _
kruskal and prims algorithm _
swati463221
 
Minimum Spanning Tree in design and analysis
Minimum Spanning Tree in design and analysis
2022imscs011
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
Ijciras1101
Ijciras1101
zhendy94
 

Recently uploaded (20)

Boost Business Efficiency with Professional Data Entry Services
Boost Business Efficiency with Professional Data Entry Services
eloiacs eloiacs
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
FME Beyond Data Processing: Creating a Dartboard Accuracy App
FME Beyond Data Processing: Creating a Dartboard Accuracy App
jacoba18
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
Grote OSM datasets zonder kopzorgen bij Reijers
Grote OSM datasets zonder kopzorgen bij Reijers
jacoba18
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
Shifting Focus on AI: How it Can Make a Positive Difference
Shifting Focus on AI: How it Can Make a Positive Difference
1508 A/S
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Data Visualisation in data science for students
Data Visualisation in data science for students
confidenceascend
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
Taqyea
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
lecture12.pdf Introduction to bioinformatics
lecture12.pdf Introduction to bioinformatics
SergeyTsygankov6
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
25 items quiz for practical research 1 in grade 11
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
Boost Business Efficiency with Professional Data Entry Services
Boost Business Efficiency with Professional Data Entry Services
eloiacs eloiacs
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
FME Beyond Data Processing: Creating a Dartboard Accuracy App
FME Beyond Data Processing: Creating a Dartboard Accuracy App
jacoba18
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
Grote OSM datasets zonder kopzorgen bij Reijers
Grote OSM datasets zonder kopzorgen bij Reijers
jacoba18
 
presentation4.pdf Intro to mcmc methodss
presentation4.pdf Intro to mcmc methodss
SergeyTsygankov6
 
llm_presentation and deep learning methods
llm_presentation and deep learning methods
sayedabdussalam11
 
MCB Internship report for the year of 2025
MCB Internship report for the year of 2025
PakistanPrinting
 
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
Veilig en vlot fietsen in Oost-Vlaanderen: Fietssnelwegen geoptimaliseerd met...
jacoba18
 
Shifting Focus on AI: How it Can Make a Positive Difference
Shifting Focus on AI: How it Can Make a Positive Difference
1508 A/S
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Data Visualisation in data science for students
Data Visualisation in data science for students
confidenceascend
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
Taqyea
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
英国毕业证范本利物浦约翰摩尔斯大学成绩单底纹防伪LJMU学生证办理学历认证
taqyed
 
lecture12.pdf Introduction to bioinformatics
lecture12.pdf Introduction to bioinformatics
SergeyTsygankov6
 
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
最新版美国芝加哥大学毕业证(UChicago毕业证书)原版定制
taqyea
 
25 items quiz for practical research 1 in grade 11
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
Ad

Data Structures and Algorithms Kruskals algorithm

  • 2. • A greedy algorithm is an approach for solving a problem by selecting the best option available at the moment, without worrying about the future result it would bring. • In other words, the locally best choices aim at producing globally best results.
  • 3. Advantages 1.The algorithm is easier to describe. 2.This algorithm can perform better than other algorithms (but, not in all cases).
  • 4. Greedy Algorithm 1.To begin with, the solution set (containing answers) is empty. 2.At each step, an item is added into the solution set. 3.If the solution set is feasible, the current item is kept. 4.Else, the item is rejected and never considered again.
  • 5. Basic Concepts Spanning Trees: A subgraph T of a undirected graph G = ( V, E ) is a spanning tree of G if it is a tree and contains every vertex of G. a b c d e a b c d e a b c d e a b c e d Graph Spanning Tree 1 Spanning Tree 2 Spanning Tree 3 • Every connected graph has a spanning tree. • May have multiple spanning tree. • For example see this graph.
  • 7. Basic Concepts Cont….. Weighted Graph: A weighted graph is a graph, in which each edge has a weight (some real number ) Example: a b c 10 9 e d Weighted Graph 7 32 23
  • 8. Basic Concepts Cont…. Minimum Spanning Tree in an undirected connected weighted graph is a spanning tree of minimum weight. Example: b c 10 9 e d Weighted Graph a 7 32 23 a c d Spanning Tree 1, w=74 10 9 e 32 23 a c d w=71 9 e 7 32 23 b b a c e d Spanning Tree 3, w=72 7 32 10 23 b Spanning Tree 2, (Minimum Spanning Tree)
  • 9. Minimum Spanning Tree Problem MST Problem : Given a connected weighted undirected graph G, design an algorithm that outputs a minimum spanning tree (MST) of graph G. • How to find Minimum Spanning Tree ? • Generic solution to MST Two Algorithms Kruskal’s algorithm Prim’s algorithm
  • 10. Applications of Minimum Spanning Tree 1.Consider n stations are to be linked using a communication network & laying of communication links between any two stations involves a cost. The ideal solution would be to extract a subgraph termed as minimum cost spanning tree. 2.Suppose you want to construct highways or railroads spanning several cities then we can use the concept of minimum spanning trees. 3.Designing Local Area Networks. 4.Laying pipelines connecting offshore drilling sites, refineries and consumer markets. 5.Suppose you want to apply a set of houses with 1. Electric Power 2. Water 3. Telephone lines 4. Sewage lines To reduce cost, you can connect houses with minimum cost spanning trees.
  • 11. Kruskal’s Algorithm • The set of edges (T) is initially empty. • As the algorithm progresses, edges are added to T at every instance. • The partial graph formed by the nodes of G, and the edges in T consists of several connected components. • At the end of the algorithm, only the connected component remains, so that T is then a minimum spanning tree of all nodes of G.
  • 13. The steps for implementing Kruskal's algorithm are as follows: 1.Sort all the edges from low weight to high 2.Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then reject this edge. 3.Keep adding edges until we reach all vertices.
  • 14. • First step to solve is to arrange the edges in the increasing order of their costs.
  • 15. Edge Cost 1 - 2 5 1 - 3 10 2 – 6 20 3 - 6 25 4 - 5 35 2 - 4 45 5 - 6 45 4 - 6 50 2 - 5 55
  • 16. • Next step is to create the following table
  • 17. Step Edge Considered Connected component Graph Initial - [1] [2] [3] [4] [5] [6] - 1. [1 – 2] [12] [3] [4] [5] [6] Accepted because no cycle 2. [1 – 3] [1 2 3] [4] [5] [6] Accepted because no cycle 1 2 5 1 2 3 5 10
  • 18. Step Edge Considered Connected Component Graph 3. [2 – 6] [1 2 3 6] [4] [5] Accepted because no cycle 4. [3 – 6] Rejected because it forms a cycle - 5. [4 – 5] [1 2 3 6] [4 5] Accepted because no cycle 1 2 3 6 5 10 20 1 2 3 4 5 5 10 20 35
  • 19. Step Edge Considered Connected Component Graph 6. [2 – 4] [1 2 3 4 5 6] Accepted because no cycle 7. [5 – 6] rejected - 8. [4 – 6] rejected 5 10 20 45 35
  • 20. Step Edge Considered Connected Component Graph 8. [4 - 6] Rejected - 9. [2 – 5] Rejected -
  • 21. Algorithm Kruskal(T,E,n) • T is the spanning tree, E is the list of edges, n is the number of nodes in given graph G { initially T=0; while ( T <> n-1 and E <> 0 ) { find the min-cost edge in E and call it as (U,V) if (U,V) does not form a cycle T = T + (U,V) else delete(U,V); } if ( |T| = n-1 ) display “Spanning Tree,T” else display “No Spanning Tree” }