Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 52

Prepared By: R.Pallavi Reddy , Asst.Prof.

, CSE, GNITS
GREEDY METHOD
UNIT-2
GREEDY METHOD
 It is the most straight forward design technique, and it can be
applied to a wide variety of problems.
 These problems have ‘n’ inputs and require us to obtain a

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
subset that satisfies some constraints.
 Any subset that satisfies these constraints is called a Feasible
solution.
 We need to find a feasible solution that either maximizes or
minimizes a given objective function.
 A feasible solution that does this is called an Optimal
Solution.
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
GREEDY METHOD
GREEDY METHOD
 The Greedy method suggests that one can devise an algorithm
that works in stages, considering one input at a time.
 At each stage, a decision is made regarding whether a

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
particular input is in an optimal solution.
 This is done by considering the inputs in an order determined
by some selection procedure.
 If the inclusion of the next input into the partially constructed
optimal solution will result in an infeasible solution, then this
input is not added to the partial solution. Otherwise, it is added.
 The selection procedure itself is based on some optimization
measure. This measure may be the objective function.
GREEDY METHOD
 Several different optimization measures may be possible for a
given problem. Most of them, however, will result in
algorithms that generate suboptimal solution.

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
 This version of the greedy technique is called the Subset
Paradigm.
 We can describe the subset paradigm by considering the
control abstraction.
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
CONTROL ABSTRACTION OF GREEDY METHOD
KNAPSACK PROBLEM
 The greedy method is applied to solve the knapsack problem.
 We are given ‘n’ objects and a knapsack or bag.

 Object ‘i’ has a weight ‘wi’ and the knapsack has a capacity

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
‘m’.
 If a fraction xi , 0  x  1, of object ‘i’ is placed into a
i
knapsack, then a profit of ‘PiXi’ is earned.
 The objective is to obtain a filling of the knapsack that
maximizes the total profit earned.
 Since the knapsack capacity is m, we require the total weight
of all chosen objects to be atmost ‘m’.
THE KNAPSACK PROBLEM
 Formally, the problem can be stated as:
‘n’ objects, each with a weight wi > 0
a profit pi > 0

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
capacity of knapsack: M
Maximize  pi xi - 1
1 i  n
Subject to  w i xi  M - 2
1 i  n

and 0  xi  1, 1  i  n - 3
The profit and weights are positive numbers.
 A Feasible solution is any set (x 1,x2,…,xn) satisfying Eq’s 2 and 3.

 An Optimal solution is a feasible solution for which Eq.1 is


maximized.
THE KNAPSACK PROBLEM
Ex: If we have 2 units of space left and 2 objects with (P i=4,Wi=4) and
(Pj=3,Wj=2) remaining, then using j is better than using half of i.

Consider the following instance of Knapsack problem. n=3,m=20,


(p1,p2,p3)=(25,24,15) and (w1,w2,w3)=(18,15,10)

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
The four feasible solutions are:
(X1,X2,X3) Σ Xi Wi Σ P i Xi

1 ( 1/2, 1/3, 1/4 ) 16.5 24.25


2 ( 1, 2/15, 0 ) 20 28.2
3 ( 0, 1, 1/2 ) 20 31.5
4 ( 0, 2/3, 1 ) 20 31.5

Solution -3 yields the maximum profit. This solution is optimal for the
given problem instance.
THE KNAPSACK PROBLEM

We can formulate at least two other greedy approaches attempting to


obtain optimal solutions.
 Considering objects in order of decreasing profit values
 does not yield an optimal solution because even though the objective
function value takes on large increases at each step, the number of steps

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
is few as the knapsack capacity is used up at a rapid rate.
 So let us try with to be greedy with capacity and use it up as slowly as
possible. This requires us to consider the objects in order of increasing
weights Wi.
 This too is suboptimal. This time, even though capacity is used slowly,
profits aren’t coming in rapidly enough.
o Our next attempt is an algorithm that strives to achieve a balance between

the rate at which profit increases and the rate at which capacity is used. This
means that the objects are considered in decreasing order of the ratio Pi/Wi.
 This results in an optimal value.


EX:1 Let n=3, m=20, (p1,p2,p3) = (25,24,15) ,
(w1,w2,w3)= (18,15,10)
 Case 1: Order objects in increasing order of their weights
Increasing order of 10 15 18
Weights ‘W’
Objects 3 2 1

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Profit 15 24 25
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=20 3 10 1
20-10 = 10 2 15 Remaining capacity/Weight =
10/15 = 2/3
0 - -

Solution vector = (x1,x2,x3) = (0, 2/3, 1)


Profit earned = 0*25 + 2/3 * 24 + 1*15 = 31
EX:1 Let n=3, m=20, (p1,p2,p3) = (25,24,15) ,
(w1,w2,w3)= (18,15,10)
 Case 2: Order objects in decreasing order of their profit.
Decreasing order of 25 24 15
Pi
Weights 18 15 10

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Objects 1 2 3
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=20 1 18 1
20-18 = 2 2 15 Remaining capacity/Weight =
2/15
0 - - -

Solution vector = (x1,x2,x3) = (1, 2/15, 0)


Profit earned = 1*25 + 2/15 * 24 + 0*15 = 25 + 3.2 = 28.2
EX:1 Let n=3, m=20, (p1,p2,p3) = (25,24,15) ,
(w1,w2,w3)= (18,15,10)
 Case 3: Order objects in decreasing order of Pi/Wi.

Profits (Pi) 25 24 15 Decreasing 1.6 1.5 1.3


order of Pi/Wi

Weights (Wi) 18 15 10 Object 2 3 1

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Pi / Wi 25/18 = 1.3 24/15 = 1.6 15/10 = 1.5 Weight 15 10 18
Capacity=20

Remaining Object Weight Fraction of Object Selected


Capacity Selected
Initial=20 2 15 1
20-15 = 5 3 10 Remaining capacity/Weight = 5/10=1/2
0 - - -

Solution vector = (x1,x2,x3) = (0, 1, 1/2)


Profit earned = 0*25 + 1 * 24 + 1/2*15 = 24 + 7.5 = 31.5(Maximum Profit Earned)
Ex-2: Let n=4, m=40, Pi(20,30,40,5), Wi(20,15,25,10). Solve
the problem using Greedy strategy.
 Case 1: Increasing order of weights.

Increasing order 10 15 20 25
of Weights ‘W’
Objects 4 2 1 3

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Capacity=40 Profit 5 30 20 40

Remaining Object Weight Fraction of Object Selected


Capacity Selected
Initial=40 4 10 1
40-10 = 30 2 15 1
30-15=15 1 20 15 / 20 = 3/4
0 - - -

Solution vector = (x1,x2,x3,x4) = (3/4, 1,0, 1)


Profit earned = ¾*20+1*30+0*40+1*5 = 50
Ex-2: Let n=4, m=40, Pi(20,30,40,5), Wi(20,15,25,10). Solve
the problem using Greedy strategy.
 Case 2: Order objects in decreasing order of their profit.
Decreasing order 40 30 20 5
of Pi
Weights 3 2 1 4

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Objects 25 15 20 10
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=40 3 25 1
40-25 = 15 2 15 1
0 - - -

Solution vector = (x1,x2,x3,x4) = (0, 1,1, 0)


Profit earned = 0*20 + 1*30 + 1*40 +0*5= 70
Ex-2: Let n=4, m=40, Pi(20,30,40,5), Wi(20,15,25,10). Solve
the problem using Greedy strategy.
 Case 3: Order objects in decreasing order of Pi/Wi.
(Pi) 20 30 40 5 Decreasing 2 1.6 1 0.5
order of
Pi/Wi
(Wi) 20 15 25 10

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Object 2 3 1 4
Pi / Wi 25/20 30/15 40/25 5/10 Weight 15 25 20 10
Capacity=20= 1 =2 = 1.6 = 0.5

Remaining Object Weight Fraction of Object Selected


Capacity Selected
Initial=40 2 15 1
40-15 = 25 3 25 1
25-25=0 - - -

Solution vector = (x1,x2,x3,x4) = (0, 1, 1,0)


Profit earned = 0*20 + 1 * 30 + 1*40+0*5 = 70(Maximum Profit Earned)
Ex-3: Let n=4, m=40, Pi(20,25,15,30), Wi(15,10,40,50).
Solve the problem using Greedy strategy.

 Case 1: Increasing order of weights. Pi = 57

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
 Case 2: Order objects in decreasing order of their profit. Pi=76.6

 Case 3: Order objects in decreasing order of Pi/Wi. Pi = 81.5


Ex-4: Let n=7, m=15, Pi(10,5,15,7,6,18,3),
Wi(2,3,5,7,1,4,1). Solve the problem using Greedy
strategy.
 Case 1: Increasing order of weights. Pi = 54

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
 Case 2: Order objects in decreasing order of their profit. Pi= 47

 Case 3: Order objects in decreasing order of Pi/Wi. Pi = 55.3


ALGORITHM
Algorithm GreedyKnapsack( m , n )
{ // P[1..n] & W[1..n] contain the profits and //
weights respectively of the n objects
for i:= 1 to n do // ordered such that
x[i]:=0; // Initialize x // P[i]/W[i] >= P[i+1] / W[i+1]

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
U:=m;
for i:=1 to n do // m is the knapsack size and
// x[1..n] is the solution vector
{
if (w[i] > U) then break;
x[i]:=1.0;
U:=U-w[i];
}
if (i<=n) then
x[i]:=U/w[i] ;
}
JOB SEQUENCING WITH DEADLINES
 Given: Set of ‘n’ jobs. Associated with job ‘i’ is an integer deadline di >= 0
and a profit Pi>0.
 For any job i the profit Pi is earned iff the job is completed by its deadline.
 To complete a job, one has to process the job on a machine for one unit of

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
time.
 Only one machine is available for processing jobs.
 A feasible solution for this problem is a subset J of jobs such that each job
in this subset can be completed by its deadline.
 The value of feasible solution J is the sum of the profits of the jobs in J, or
ΣiЄJ Pi.
 An optimal solution is a feasible solution with maximum value. Since the
problem involves the identification of a subset. It fits the subset paradigm.
JOB SEQUENCING WITH DEADLINES

 A feasible solution is a subset of jobs J such that each job is


completed by its deadline.
 An optimal solution is a feasible solution with maximum profit
value.

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27), (d1,d2,d3,d4)
= (2,1,2,1)
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1)
S.No. Feasible Processing Profit value
Solution Sequence
0 1 2
(i) (1,2) (2,1) 110

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
(ii) (1,3) (1,3) or (3,1) 115
(iii) (1,4) (4,1) 127 is the optimal one
(iv) (2,3) (2,3) 25
(v) (3,4) (4,3) 42
(vi) (1) (1) 100
(vii) (2) (2) 10
(viii) (3) (3) 15
(ix) (4) (4) 27
 To formulate a greedy algorithm, to obtain an optimal solution,
we must formulate an optimization measure to determine how
the next job is chosen.
 As a first attempt, we can choose the objective function Σ iЄJ Pi

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
as our optimization measure.
 Using this measure, the next job to include is the one that
increases Σ iЄJ Pi the most, subject to the constraint that the
resulting J is a feasible solution,
 This requires us to consider the jobs in decreasing order of the
Pi’s.
GREEDY ALGORITHM TO OBTAIN AN OPTIMAL
SOLUTION
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1). Solve the problem using Greedy
Method.

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Sol: Consider the jobs in the decreasing order of profits
(p1,p2,p3,p4) = (100, 27, 15, 10)
0 1 2
(d1,d2,d3,d4) = ( 2, 1, 2, 1)
J = { 1 } is a feasible one
J = { 1, 4 } is a feasible one with processing sequence(4,1)
J = { 1, 3, 4 } is not feasible
J = { 1, 2, 4 } is not feasible
J = { 1, 4 } is optimal
EX:2 Job 1 2 3 4 5 6 7
Deadline 3 1 1 3 1 3 2
Profit 40 35 30 25 20 15 10

0 J2
J1- Max. time is 3, so select the slot 2-3.
1

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
J2- Max. time is 1, so select the slot 0-1.
J3- Max. time is 1,slot 0-1 is not free , so discard it. J4
J4- Max. time is 3, so select the slot 1-2.
J5- Max. time is 1, slot 0-1 is not free, so discard it. 2
J6- Max. time is 3, slot 0-1,1-2,and 2-3 are not free so 3
discard. J1

J7- Max. time is 2, slot 0-1,and 1-2 is not free so discard.


Therefore, The Job sequence ={2,4,1}
Profit earned= 35+25+40 = 100
Job 1 2 3 4 5 6
EX:3 Deadline 2 1 2 3 2 4
Profit 20 25 30 40 35 20

Consider decreasing order of profits


Job 4 5 3 2 1 6
Deadline 3 2 2 1 2 4

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Profit 40 35 30 25 20 20 0 J3
1
J5

J4
Solution vector = {3,5,4,6}
Pi=30+35+40+20 = 125 3
4 J6
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
JOB SEQUENCING WITH DEADLINES
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
JOB SEQUENCING WITH DEADLINES
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
MINIMUM COST SPANNING TREE
 There are 2 ways to find the minimum cost spanning tree.
 Kruskal’s Algorithm

 Prim’s algorithm

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
PRIM’S ALGORITHM
 A Greedy method to obtain a minimum cost spanning tree
builds this tree edge by edge.
 The next edge to include is chosen according to some
optimization criterion.

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
 The simplest such criterion is to choose an edge that results in
a minimum increase in the sum of the costs of the edges so far
included.
 There are 2 possible ways to interpret this criterion.

1. The set of edges so far selected form a tree. Thus, if A is the


set of edges selected so far, then A forms a tree.
2. The next edge ( u, v) to be included in A is the minimum cost
edge not in A with the property that A U {(u,v)} is also a tree.
This is known as Prim’s Algorithm.
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
PRIM’S ALGORITHM
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
PRIM’S ALGORITHM
Steps:

1. Find the cost adjacency matrix.


2. Take the minimum cost edge in the graph and write in the tree edge and

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
also in Near[j].
3. Now consider the tree edge (1,6) and check ‘j’, i.e., j is near to which
vertex?
the cost of (2,1) is ? , the cost of (2,6) is ? And check which is minimum?
Accordingly update Near[j].

If ( cost[ i, l] < cost[ i, k]) then near[i]=l else near[i]=k;


4. Check among all the near[j] which has minimum cost and consider that as next
edge and repeat i.e. (5,6) is min. Now, check whether (2,1) is min or (2,5) is min
for all values and again repeat the procedure.
PRIM’S ALGORITHM 1 2 3 4 5 6 7
6,0 1 ∞ 28 ∞ ∞ ∞ 10 ∞
1,3,0 2 28 ∞ 16 ∞ ∞ ∞ 14
1,4,0 3 ∞ 16 ∞ 12 ∞ ∞ ∞
1,5,0
4 ∞ ∞ 12 ∞ 22 ∞ 18
6,0
5 ∞ ∞ ∞ 22 ∞ 25 24
1,0

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
1,5,4,2 6 10 ∞ ∞ ∞ 25 ∞ ∞
,0 7 ∞ 14 ∞ 18 24 ∞ ∞
Tree Edge
J Near[j]
i K,l
1 6 0 0 0 0 0
1 (1,6)
2 2 1 1 1 3 0 1 2
(5,6)
1 1 4 0 0 10 14 16
3 (4,5) 3
(3,4) 4 1 5 0 0 0 6 7
4 3
5 (2,3) 5 6 0 0 0 0
25 12
6 (7,2) 6 1 0 0 0 0 0 5 4
5 22
7 1 4 4 2
Min Cost(T)= 99
EX:2 1 2 3 4 5 6
10 50 1 ∞ 10 ∞ 30 ∞ ∞
1 2
40 3 2 10 ∞ 50 ∞ 40 25
30
25 3 ∞ 50 ∞ ∞ 35 15
35
5
4 4 30 ∞ ∞ ∞ ∞ 20
6 55
15 5 ∞ 40 35 ∞ ∞ 55
20

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
6 ∞ 25 15 20 55 ∞

Tree Edge J Near[j]


i K,l 10
1 2,0 1 2
1 (1,2) 2 1, 0
2 (6,2) 3 2,6,0 25 5 35
3 (3,6) 4 1,6,0 4 3
4 (4,6) 5 2,3,0 6
20 15
5 (5,3) 6 2,0

Minimum Cost (T) = 105


Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Time Complexity of Prim’s Algorithm
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
KRUSKAL’S ALGORITHM
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Time Complexity of Kruskal’s Algorithm is
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
KRUSKAL’S ALGORITHM
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Single Source Shortest Path
 Graphs can be used to represent the highway structure of a state or country with
vertices representing the cities and edges representing the sections of the
highway.
 The edges can then be assigned weights which may be either the distance
between the 2 cities connected by the edge or the average time to derive along

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
that section of highway.
 EX: To travel from A to B
 Is there a path from A to B?
 Is there any other path from A to B which is the shortest path?

 Given a directed graph G=( V, E), a weighting function cost for the edges of
G, and a source vertex ‘V0’. The problem is to determine the shortest paths
from V0 to all the remaining vertices of G. It is assumed that all the
weights are positive.
 The shortest path between V 0 and some other node ‘V’ is an ordering among a
subset of the edges. Hence, this problem fits the Ordering paradigm.
Single Source Shortest Path
 To formulate a greedy –based algorithm to generate the shortest paths, we
must conceive of a multi-stage solution to the problem and also of an
optimization measure.
 One possibility is to build shortest paths one by one. As an optimization

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
measure, we can use the sum of the lengths of all paths so far generated.
 For this measure, to be minimized, each individual path must be of
minimum length.
 If we have already constructed ‘i’ shortest paths, then using this
optimization measure, the next path to be constructed should be the next
shortest minimum length path.
Single Source Shortest Path
 The Greedy way to generate the shortest paths from V 0 to the remaining

vertices is to generate these paths in increasing order of path length.


 First, the shortest path to the nearest vertex is generated. Then a shortest
path to the second nearest vertex is generated and so on.

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Find the cost adjacency matrix
if i=j, then mark it as -1
if there is no edge mark it as ∞
V=1 S[1] S[2] S[3] S[4] S[5] S[6]
F F F F F F

GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Cost Adjacency Matrix Dist:
1 2 3 4 5 6 1 2 3 4 5 6
1 -1 50 45 10 ∞ ∞ -1 50 45 10 ∞ ∞
2 ∞ -1 10 15 ∞ ∞ 45 25

3 ∞ ∞ -1 ∞ 30 ∞

4 20 ∞ ∞ -1 15 ∞

5 ∞ 20 35 ∞ -1 ∞

6 ∞ ∞ ∞ ∞ 3 -1
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS

You might also like