Professional Documents
Culture Documents
Assignment (Daa) PSEUDO CODE For Knapsack Problem:: Example
Assignment (Daa) PSEUDO CODE For Knapsack Problem:: Example
ASSIGNMENT (DAA)
PSEUDO CODE for knapsack problem:
Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)
for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x
EXAMPLE-
(p1, p2, p3) = (25,24,15)
(w1, w2, w3) = (18,15,10)
Achieves a balance between the rate at which profit increases with the rate at
which the capacity is used.
Analysis= If we do not consider the time considered for sorting the inputs then all of the three
greedy strategies complexity will be 0(n).