Greedy Method
Greedy Method
Bheekya D
Greedy Method
● A greedy algorithm is the most straightforward design technique.
● The problems have N inputs and require us to obtain a subset that satisfies
some constraints.
● Any subset that satisfies those constraints is called a feasible solution.
● Find the feasible solution that either maximizes or minimizes a given
objective function.
● A feasible solution that satisfies the objective function is called an optimal
solution.
● Greedy algorithm suggest us to design an algorithm in stages, considering
one input at each stage.
Problems of Greedy method
● Knapsack problem.
● Job sequencing with deadlines.
● Minimum cost spanning tree
● Single source shortest path
Spanning Tree
● Let G = (V, E) be an undirected connected graph. A subgraph T = (V,
E’) of G is a spanning tree of G iff T is a tree.
● In a graph, there may exist more than one spanning tree.
● Example: In the below graph, the highlighted edges form a spanning
tree.
Spanning Tree
Minimum Cost Spanning Tree (MST)
● A Minimum Spanning Tree (MST) is a subset of edges of a connected
weighted undirected graph that connects all the vertices together with the
minimum possible total edge weight.
2. Determine the edges from vertex 1 and select minimum cost edge from them.
There are 2 edges from 1
a. (1,6): cost is 10
b. (1,2): cost is 28
c. Minimum cost edge is (1,6). Add this edge to MST.
Prim’s algorithm : steps
1. Determine the non selected edges from the vertex 1 and 6 and select minimum
cost edge from them.
2. There is one edge from 1 and one edge from 6
a. (1,2): cost is 28
b. (6,5): cost is 25
c. Minimum cost edge is (6, 5). Add this edge to MST.
Prim’s algorithm : steps
1. Determine the non selected edges from vertex 1, 6 and 5 and select minimum cost edge from
them.
2. There is one edge from 1, two edges from 5 and zero edge from 6
a. (1,2): cost is 28
b. (5,4): cost is 22
c. (5,7): cost is 24
d. Minimum cost edge is (5,4). Add this edge to MST.
Prim’s algorithm : steps
1. Determine the non selected edges from vertex 1, 6, 5 and 4 and select minimum cost edge
from them.
2. There is one edge from 1, one edge from 5, 2 edges from 4 and zero edge from 6
a. (1,2): cost is 28
b. (5,7): cost is 24
c. (4,3): cost is 12 and (4,7) cost is 18
d. Minimum cost edge is (4,3). Add this edge to MST.
Prim’s algorithm : steps
1. Determine the non selected edges from vertex 1, 6, 5, 4 and 3 and select minimum cost edge from
them.
2. There is one edge from 1, one edge from 5, 1 edges from 4, 1 edge from 3 and zero edge from 6
a. (1,2): cost is 28
b. (5,7): cost is 24
c. (4,3): cost is 18
d. (3,2): cost is 16
e. Minimum cost edge is (3,2). Add this edge to MST.
Prim’s algorithm : steps
1. Determine the non selected edges from vertex 1, 6, 5, 4,3 and 2 and select minimum cost edge from
them.
2. There is one edge from 1, one edge from 5, 1 edges from 4, 1 edge from 2 and zero edge from 6 and
3
a. (1,2): cost is 28
b. (5,7): cost is 24
c. (4,3): cost is 18
d. (2,7): cost is 14
e. Minimum cost edge is (2,7). Add this edge to MST.
1 28
10 2 1
14 16 2
6 7 3
24 6 7
25 18 3
12
5
22 4
5
4
4 -23
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
10 2 1
14 16 2
6 7 3
24 6 7
25 18 3
12
5
22 4
5
4
4 -24
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
14 16
10 2
6 7 3
24 6 7
25 18 3
12
5
22 4
5
4
4 -25
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
14 16
10 2
6 7 3
24 6 7
25 18 3
12
5
22 4
5
4
4 -26
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
14 16
10 2
6 7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -27
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
14 16
10 2
6 7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -28
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
16 2
10
6 14
7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -29
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
16 2
10
6 14
7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -30
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -31
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -32
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 3
18
5 12
22 4
5
4
4 -33
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 18 6 7
25 3
5 12
22 4
5
4
4 -34
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
22 4
5
4
4 -35
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
4
5
22 4
4 -36
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
4
5
22 4
4 -37
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
6 7
25 18 3
24
5 12
4
5
22 4
4 -38
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
4
5
22 4
4 -39
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
25 18 3
5 12
4
5
22 4
4 -40
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
1 28
2 1
10 2
6 14 16
7 3
24 6 7
18 3
5 25
12
4
5
22 4
4 -41
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
Algorithm kruskal(E,cost,n,t) while((i<n-1) and (heap not empty)) do
// E is the set of edges in G.G has n {
vertices.cost[u,v] is the cost of delete a minimum cost edge (u,v) from the heap
and reheapify using Adjust;
edge(u,v). j=Find(u);
//t is the set of edges in the minimum– cost K=Find(v);
//spanning tree. the final cost is if(j!=k) then
returned. {
{ i=i+1;
Construct a heap out of the edge costs t[I,1]=u;
using Hepify; t[I,2]=v;
for i=1 to n do parent[i]=-1; mincost=mincost+cos[u,v];
Union(j, k);
//each vertex is in a different set. }
i=0; } 42
mincost=0; If(i!=n-1) then
write(“no spanning tree”);
else
return mincost;
}
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
4 -44
Difference between Prim’s and Kruskal’s Algorithm