KEMBAR78
Data Structures and Algorithms Kruskals algorithm | PPTX
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”
}

Data Structures and Algorithms Kruskals algorithm

  • 1.
  • 2.
    • A greedyalgorithm 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 iseasier to describe. 2.This algorithm can perform better than other algorithms (but, not in all cases).
  • 4.
    Greedy Algorithm 1.To beginwith, 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.
  • 6.
  • 7.
    Basic Concepts Cont….. WeightedGraph: 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…. MinimumSpanning 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 TreeProblem 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 MinimumSpanning 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 • Theset 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.
  • 12.
  • 13.
    The steps forimplementing 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 stepto 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 stepis to create the following table
  • 17.
    Step Edge ConsideredConnected 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 ConsideredConnected 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 ConsideredConnected 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) • Tis 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” }