ADA_Greedy Method (1)
ADA_Greedy Method (1)
Objectives
• Greedy Method
• Applications
– Optimal Storage on Tapes
– Job Sequencing with Deadlines
– Knapsack Problem
– Single Source Shortest Path
• Previous Gate Questions
Greedy Method
• The Greedy method is a most straight forward design
technique which can be applied to a wide variety of
problems.
• This algorithm works in steps. In each step it selects the
best available options until all options are finished.
• Most of the problems have n inputs and require us to
obtain a subset that satisfies some constraints.
• Any subset that satisfies these constraints is called as a
feasible solution.
• A feasible solution that either minimizes or maximizes
a given objective function is called as Optimal
Solution. 3
Greedy
Algorithm
• The Greedy method suggest that one can devise an
algorithm that work in stages, considering one input at a
time.
• At each stage, a decision is made regarding whether a
particular input is an optimal solution or not.
• 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 results sub-
optimal/infeasible solution, then that input is not added to
the partial solution. Otherwise, it is added. The selection
procedure itself is based on some optimization measures.
Control Abstraction for Greedy
Method
Select selects an input from a[] and removes it. The selected input’s
value is assigned to x.
Feasible is a Boolean-valued function that determines whether x can be included
into the solution vector or not.
Union combines x with the solution and updates the objective function.
Types of Greedy Problems
• Subset Paradigm
• To solve a problem (or possibly find the
optimal/best solution), greedy approach generate
subset by selecting one or more available choices.
Eg. includes Knapsack problem, job sequencing
with deadlines. In both of the problems greedy
create a subset of items or jobs which satisfies all
the constraints.
• Ordering Paradigm
• In this, greedy approach generate some
arrangement/order to get the best solution. Eg.
includes: Minimum Spanning tree
Applications
• Fractional knapsack algorithm
• Optimal Storage on tapes
• Job sequencing with deadline
• Single source shortest path
– Dijkstra's SSSP algorithm
• Activity Selection Problem
• Minimum Cost Spanning Tree
• …
Knapsack Problem
• Let, we are given n objects and a Knapsack
or Bag.
• Object i has weight Wi and the Knapsack has
a capacity M.
• if a fraction Xi of object i is placed into
Knapsack, then a profit of PiXi is earned.
• The objective is to obtain a filling of
Knapsack that maximizes the total profit
earned.
• Maximiz (A)
e
• Subject (B)
• to
• And 0 ≤ Xi ≤ 1, 1 ≤i ≤n (C)
• The profit and weights are the positive numbers.
• Here, A feasible solution is any set (X1, X2,
…, Xn) satisfying above rules (B) and (C).
• And an optimal solution is feasible solution
for which rule (A) is maximized.
Here, N=3, M=20, (P1, P2, P3)=(25, 24, 15) and (W1, W2, W3)=(18, 15,
10)
Different feasible solutions are:
(X1, X2,
X3)
1. (1/2, 1/3, ¼) 16.5 24.25
2. (1, 2/15, 0) 20 28.2
3. (0, 2/3, 1) 20 31
4. (0, 1, 1/2) wrt 20 31.5
pi/wi
5. (1/2, 2/3, 1/ 10) 20 30
6. (1, 0, 2/10) 20 28
• Of these Six feasible solutions, solution 4 yields the maximum profit.
Therefore solution 4 is optimal for the given problem instance.
• Consideration 1 - In case the sum of all the weights is ≤ M, then
Xi=1, 1 ≤ i ≤n
• is an optimal solution.
Consideration 2 - All optimal solutions will fill the knapsack exactly.
The knapsack algorithm
• The greedy algorithm:
Step 1: Sort pi/wi into nonincreasing order.
Step 2: Put the objects into the knapsack according
to the sorted sequence as possible as we can.
• e. g. p[i]/w[i]≥p[i+1]≥w[i+1]
n = 3, M = 20, (p1, p2, p3) = (25, 24, 15)
(w1, w2, w3) = (18, 15, 10)
Sol: p1/w1 = 25/18 = 1.39
p2/w2 = 24/15 = 1.6
p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 =
1, x3 = 1/2
total profit = 24 + 7.5 =
31.5
Algorithm
• Algorithm GreedyKnapsack(m,n)
//order the n objects such that p[i]/w[i]≥p[i+1]≥w[i+1]
{
for i:=1 to n do x[i]:=0.0;
U:=m;
for i:=1 to n do
{
if(w[i] > U) then break;
x[i]:=1.0; U:=U-w[i];
}
if( i ≤ n) then x[i]:=U/w[i];
}
Example problem
• N=7
• M=15(15-1=14-2=12-4=8-5=3-1=2)
• (p1,p2…p7)=(10,5,15,7,6,18,3)
• (w1,w2..w7)=(2,3,5,7,1,4,1)
• The solution vector is (1,2/3,1,0,1,1,1)
• P1/w1=5 p2/w2=1.66 p3/w3=3 p4/w4=1
p5/w5=6 p6/w6=4.5 p7/w7=3
• x5,x1,x6,x3,x7,x2,x4
• Weight is
1+2+4+5+1+2/3*3+0=13+2/3*3=15
• Profit is 6+10+18+15+3+2/3*5+0=55.34
(1,2/3,1,0,1,1,1) 18
Job Sequencing with Deadlines
• We are given a set of n jobs.
• Di is a deadline given to complete ithjob for profit Pi where Pi>0 &
Di ≥ 0.
• For any job i profit Pi is earned iff the job is completed within
its deadline.
• To complete a job, one has to process the job on a machine for
one unit of 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 a feasible solution J is the sum of the profits of the
job in J.
• An optimal solution is a feasible solution with maximum value.
• Example –Suppose on a single machine four jobs
with profit values (100, 10, 15 and 27) and their
respective deadline unit values (2, 1, 2, 1) are
given. Calculate the different feasible solutions to
complete the jobs with optimal solution.
• Solution:
• Number of Jobs (n)= 4
• Profit values of four jobs (P1, P2, P3, P4)=(100, 10, 15,
27)
• Process deadlines for respective jobs are
(D1, D2, D3, D4)=(2, 1, 2, 1)
(P1, P2, P3, P4)=(100, 10, 15, 27) (D1, D2, D3, D4) =(2, 1, 2,
1)
The feasible
Feasible sol. solutions
subset and their values
Processing are –
Sequence Values
(1, 2) (2, 1) 110
(1, 3) (1, 3 or 3, 1) 115
(1, 4) (4, 1) 127
(3, 2) (2, 3) 25
(3, 4) (4, 3) 42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27
Here, Solution 3 is optimal solution. In this solution only job 1 and 4
are processed and the profit value is 127. These jobs must be
processed in the order Job 4 followed by job 1. Thus the
processing of job 4 begins at time zero and that of job 1 is
completed at time 2.
Algorithm
Approach: Greedy
1 2 3 4 5 6 7
0 inf inf inf inf inf Inf
25 inf inf inf inf inf
35 39 inf inf inf
39 inf 51 Inf
inf 51 inf
93 65
93
Previous Gate Questions
Q. No. 1
Answer is 16
16
60/10 =6
28/7 =4
20/4 =5
24/2 =12
X4, x1, x3, x2 24+20=44