0% found this document useful (0 votes)
24 views13 pages

DAA - 2.1-Gen - Greedy - Knapsack

Uploaded by

y22cs094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

DAA - 2.1-Gen - Greedy - Knapsack

Uploaded by

y22cs094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

CSE- 312

Design and Analysis of Algorithms


III Year 1st Semester
UNIT – II
Topics: Greedy Programming: General Method,
Knapsack problem
GREEDY METHOD
• Greedy method is the most straightforward
designed technique.
• A problem with n inputs will have some
constraints .any subsets that satisfy these
constraints are called a feasible solution.
• A feasible solution that either maximize can
minimize a given objectives function is called
an optimal solution.
• The Greedy method suggests that one can devise an
algorithm that works in stage.
• At each stage a decision is made whether a particular
input is in the optimal solution. This is called subset
paradigm.
• Example problems:
 Knapsack
 Job sequencing with deadlines
 Minimum cost Spanning Trees
• For the problems that make decisions by considering
the inputs in some order, each decision is made
using an optimization criterion that can be computed
using decisions already made.
• This version of greedy method is ordering paradigm.
• Example problems:
 optimal storage on tapes
 optimal merge patterns
 single source shortest path
Control Abstraction
Algorithm Greedy (a, n)
// a(1 : n) contains the ‘n’ inputs
{
solution := 0; // initialize the solution to empty
for i:=1 to n do
{
x := select (a);
if feasible (solution, x) then
solution := Union (Solution, x);
}
return solution;
}
The Knapsack Problem
• n objects, each with a weight wi > 0
a profit pi > 0
capacity of knapsack: M
 pi xi
Maximize 1i n

 w i x i M
Subject to 1i n

where 0  xi  1, 1  i  n
3 -7
Example:
• Consider the following instance of the knapsack
problem: n = 3, m = 20, (p1, p2, p3) = (25, 24, 15) and
(w1, w2, w3) = (18, 15, 10).
1. First, we try to fill the knapsack by selecting the objects
in some order:
x1 x2 x3
1/2 1/3 1/4
wi xi = 8 x 1/2 + 15 x 1/3 + 10 x 1/4 = 16.5
pi xi = 25 x 1/2 + 24 x 1/3 + 15 x 1/4 = 24.25
2. Select the object with the maximum profit
first (p = 25). So, x1 = 1 and profit earned is 25.
Now, only 2 units of space is left, select the
object with next largest profit (p = 24). So, x2
= 2/15
x1 x2 x3
1 2/15 0
wi xi = 18 x 1 + 15 x 2/15 = 20
pi xi = 25 x 1 + 24 x 2/15 = 28.2
3. Considering the objects in the order of non-
decreasing weights wi.
x1 x2 x3
0 2/3 1
wi xi = 15 x 2/3 + 10 x 1 = 20
pi xi = 24 x 2/3 + 15 x 1 = 31

4. Considered the objects in the order of the ratio pi / wi


p1/w1 p2/w2 p3/w3
25/18 24/15 15/10
1.4 1.6 1.5
• Sort the objects in order of the non-increasing
order of the ratio pi / xi. Select the object with the
maximum pi / xi ratio, so, x2 = 1 and profit earned
is 24.
• Now, only 5 units of space is left, select the object
with next largest pi / xi ratio, so x3 = ½ and the
profit earned is 7.5.
x1 x2 x3
0 1 1/2
wi xi = 15 x 1 + 10 x 1/2 = 20
pi xi = 24 x 1 + 15 x 1/2 = 31.5
This solution is the optimal solution.
The knapsack algorithm
Algorithm GreedyKnapsack (m, n)
// P[1 : n] and w[1 : n] contain the profits and weights respectively of
// Objects ordered so that p[i] / w[i] > p[i + 1] / w[i + 1].
// m is the knapsack size and x[1: n] is the solution vector.
{
for i := 1 to n do x[i] := 0.0 // initialize x
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];
} 3 -12
Poll Question?
• If sum of the all weights is < knapsack size
then xi = 1.(Yes/No)
Home Work
1. n=4,m=60, (p1,p2,p3,p4)=(280,100,120,120)
(w1,w2,w3,w4)=(40,10,20,24)
2. n=7,m=15,
(p1,p2,p3,p4,p5,p6,p7)=(10,5,15,7,6,18,3)
(w1,w2,w3,w4,w5,w6,w7)=(2,3,5,7,1,4,1)

You might also like