B
Bubba
Greetings,
I'm attempting to solve a 0/1 knapsack problem as it follows - there are
n objects O_1, O_2, ..., O_n and a knapsack. Object O_i has weight w_i
and value (price) p_i. Capacity of the knapsack is c units of weight.
Solved by dynamic programming approach, M_i,j is denoted as maximal
value that can be obtained my choosing objects from the set {O_1, O_2,
...., O_i} with the capacity j. When M_i,j is achieved, object O_i is
either put in the knapsack or it is not. If O_i is not put in the
knapsack, then M_i,j = M_i-1,j. If O_i is put in the knapsack,
previously chooses object represents the optimal choice from the set {O_
1, O_2, ...,O_i-1} with the capacity j - w_i.
The algorithm fills the matrix with values M_i,j. The value that is
actually searched for is M_n,c. The matrix is formed with dimensions (n+
1) * (c+1), and the order of computing is row by row (since j changes
faster than i). Complexity is O(nc).
The table that should be formed by this algorithm, for values n = 3, (w_
1, w_2, w_3) = (2,3,4), (p_1, p_2, p_3) = (1,7,8) and c = 6 looks like
this:
\ j 0 1 2 3 4 5 6
i
0 0 0 0 0 0 0 0
1 0 0 1 1 1 1 1
2 0 0 1 7 7 8 8
3 0 0 1 7 8 8 9
That is, maximum value in the knapsack should be M_3,6 = 9.
Unfortunately, the code below says otherwise.
#include <stdio.h>
float M[4][7];
int w[3] = {2,3,4};
float p[3] = {1,7,8};
float zero_one_knapsack (int n, int c)
{
int i, j;
for (i=0; i<=n; i++) M[0] = 0.0;
for (j=0; j<=c; j++) M[0][j] = 0.0;
for (i=1; i<=n; i++)
for (j=1; j<=c; j++)
{
M[j] = M[i-1][j];
if ( j >= w )
if ((M[i-1][j-w] + p) > M[i-1][j]) M[j]
= M[i-1][j-w] + p;
}
return M[n][c];
}
int main (void)
{
printf ("%f\n",zero_one_knapsack (3, 6)); /* prints 8 */
return 0;
}
Any help would be appreciated (including the piece of already good code
that could fit my needs stated before
).
TIA!
I'm attempting to solve a 0/1 knapsack problem as it follows - there are
n objects O_1, O_2, ..., O_n and a knapsack. Object O_i has weight w_i
and value (price) p_i. Capacity of the knapsack is c units of weight.
Solved by dynamic programming approach, M_i,j is denoted as maximal
value that can be obtained my choosing objects from the set {O_1, O_2,
...., O_i} with the capacity j. When M_i,j is achieved, object O_i is
either put in the knapsack or it is not. If O_i is not put in the
knapsack, then M_i,j = M_i-1,j. If O_i is put in the knapsack,
previously chooses object represents the optimal choice from the set {O_
1, O_2, ...,O_i-1} with the capacity j - w_i.
The algorithm fills the matrix with values M_i,j. The value that is
actually searched for is M_n,c. The matrix is formed with dimensions (n+
1) * (c+1), and the order of computing is row by row (since j changes
faster than i). Complexity is O(nc).
The table that should be formed by this algorithm, for values n = 3, (w_
1, w_2, w_3) = (2,3,4), (p_1, p_2, p_3) = (1,7,8) and c = 6 looks like
this:
\ j 0 1 2 3 4 5 6
i
0 0 0 0 0 0 0 0
1 0 0 1 1 1 1 1
2 0 0 1 7 7 8 8
3 0 0 1 7 8 8 9
That is, maximum value in the knapsack should be M_3,6 = 9.
Unfortunately, the code below says otherwise.
#include <stdio.h>
float M[4][7];
int w[3] = {2,3,4};
float p[3] = {1,7,8};
float zero_one_knapsack (int n, int c)
{
int i, j;
for (i=0; i<=n; i++) M[0] = 0.0;
for (j=0; j<=c; j++) M[0][j] = 0.0;
for (i=1; i<=n; i++)
for (j=1; j<=c; j++)
{
M[j] = M[i-1][j];
if ( j >= w )
if ((M[i-1][j-w] + p) > M[i-1][j]) M[j]
= M[i-1][j-w] + p;
}
return M[n][c];
}
int main (void)
{
printf ("%f\n",zero_one_knapsack (3, 6)); /* prints 8 */
return 0;
}
Any help would be appreciated (including the piece of already good code
that could fit my needs stated before
TIA!