Oct-01-2020, 08:00 AM
I tried so many times but i couldn't do it :(
The steps are all below, someone please help me!
Algorithm 1 Greedy Algorithm for the Continuous Knapsack Problem
1: Input: n items with size si, value vi
2: Input: Knapsack capacity K
3: Set solution vector x equal to 0 for all xi
4: Set current objective function value z = 0
5: Calculate ri =vi/si for all items i = 1, . . . , n
6: Sort the ri values in non-increasing order, place in vector R
7: Set u, representing the used knapsack capacity, to zero
8: Set iterator i to 1
9: while u < K do
10: if si + u ≤ K then
11: Set xi = 1
12: Set u = u + si
13: Set z = z + vi
14: else if si + u > K then
15: Set xi
to the maximum amount of item i that can be placed in the knapsack
16: Set u = u + sixi
(Note, this should equal K)
17: Set z = z + vixi
18: end if
19: Set i = i + 1
20: end while
21: Return the solution vector x and objective function value z
The steps are all below, someone please help me!
Algorithm 1 Greedy Algorithm for the Continuous Knapsack Problem
1: Input: n items with size si, value vi
2: Input: Knapsack capacity K
3: Set solution vector x equal to 0 for all xi
4: Set current objective function value z = 0
5: Calculate ri =vi/si for all items i = 1, . . . , n
6: Sort the ri values in non-increasing order, place in vector R
7: Set u, representing the used knapsack capacity, to zero
8: Set iterator i to 1
9: while u < K do
10: if si + u ≤ K then
11: Set xi = 1
12: Set u = u + si
13: Set z = z + vi
14: else if si + u > K then
15: Set xi
to the maximum amount of item i that can be placed in the knapsack
16: Set u = u + sixi
(Note, this should equal K)
17: Set z = z + vixi
18: end if
19: Set i = i + 1
20: end while
21: Return the solution vector x and objective function value z