0% found this document useful (0 votes)
6 views

File 2

Uploaded by

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

File 2

Uploaded by

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

DESIGN AND ANALYSIS OF ALGORITHMS Page 45

www.adroid previousaestionpapers. con wWHpreviousguestionpapers.com hups:/telegram.me jntulh

wWw.android. universityupdates.in wWw.niersivupates.in hutps:/telegram.mejntuh

0/1 knapsack problem:


Let there n items, Z1 to Zn where Z; has a value Vi and weight Wi. The maximum
weight that we can carry in the bag is W. It is common to assume that all values and weights
are nonnegative. To simplify the representation, we also assumne that the items are listed in
increasing order of weight.
n
n

I;¬{0,1}
Maximize i=1 subject to i=l

Maximize the sum of the values of the items in the knapsack so that the sum of the weights must be less
than the knapsack's capacity.
Greedy algorithm for knapsack
AlgorithmGreedyKnapsack(m,n)
l p[i:n] and [1:n] contain the profits and weights respectively
/ if the n-objects ordered such that p[iJ/w[i]=p[i+l/w[i+1], m> size of knapsack and
x[l:n]’ the solution vector

For i:=1to n do x[il:=0.0


U:=m:
For i:=l to n do

if(w[ij>U) then break;


x[i]:=1.0:
U:=U-w[il:
if(i<=n) then x{il:=U/w[il:

Ex: - Consider 3 objects whose profits and weights are defined as


(P1, P2, Pa) ( 25, 24, 15 )
Wi, W2, Ws) = |18, 15, 10)
n=3’number of objects
m=20’ Bag capacity
Consider a knapsack of capacity 20. Determine the optimum strategy for placing the objects
in to the knapsack. The problem can be solved by the greedy approach where in the inputs
are arranged according to selection process (greedy strategy) and solve the problem in
stages. The various greedy strategies for the problem could be as follows.

(X1, Xz, X,) ExW;


(1, 2/15, 0)
18xlx15 -20 25x1x 24 - 28.2
(0, 2/3, 1) 2;x 24 +15x1 -31
x15"10x1- 20
DESIGN AND ANALYSIS OF ALGORITHMS Page 46

ww droid previoISae stiopnapers comwWwpreviosauestionpaners cop httnsiteleorap eiutuh

wWW.ndroid.universitvupdates.in wWW,(iversitvupdates. n https:/telegram.mejntuh

(0, 1, Va )
1x15+xl0 - 20 12451s- 31.5
(Ya, Va, Ye ) Vhx 18+%x15+ Va x10 = 16. 5 ½ x 25+% x24+ V x15 =
12.5+8+3.75 - 24.25

Analysis: - If we do not consider the time considered for sorting the inputs then all of the
three greedy strategies complexity will be O(n).

Job Sequence with Deadline:


There is set of n-jobs. For any job i, is a integer deadling di20 and profit Pi>0, the profit Pi is
earned iff the job completed by its deadline.
Depletea
To one had to process the job on a machine for one unit of time. Only one
mac i: avilable 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 jobs in J, ie., jejP;

You might also like