|
|
|
|
|
|
|
M( v[], w[], C )
{
int[] sol, mySol, myFinalSol;
// Let us not worry about the base cases for now
/* ==============================================
Divide and conquer procedure
============================================== */
/* ---------------------------------------
Solve the appropriate smaller problems
--------------------------------------- */
for ( i = 1; i ≤ N; i++ )
{
if ( C ≥ w[i] )
sol[i] = M( v, w, C-w[i] ); // Knapsack capacity reduced by w[i]
// because it has item i packed in
// it already
else
sol[i] = 0; // Not enough space to pack item i
}
/* ---------------------------------------------
Use the solutions to the smaller problems
to solve original problem
--------------------------------------------- */
for ( i = 1; i ≤ N; i++ )
{
if ( C ≥ w[i] )
mySol[i] = sol[i] + v[i]; // Value is increased by v[i]
// because it has item i packed in
// it already
else
mySol[i] = 0; // Not enough space to pack item i
}
/* *************************
Find the best (maximum)
************************* */
myFinalSol = mySol[1];
for ( i = 2; i ≤ N; i++ )
if ( mySol[i] > myFinalSol )
myFinalSol = mySol[i];
return myFinalSol;
}
|
|
In other words:
M(v, w, 0) = 0; (can't packet anything into a 0-capacity knapsack)
|
(N = # different items)
M( v[], w[], C )
{
int[] sol, mySol, myFinalSol;
/* ***************************************
Base case
*************************************** */
if ( C == 0 )
return 0;
/* ==============================================
Divide and conquer procedure
============================================== */
/* ---------------------------------------
Solve the appropriate smaller problems
--------------------------------------- */
for ( i = 1; i ≤ N; i++ )
{
if ( C ≥ w[i] )
sol[i] = M( v, w, C-w[i] ); // Knapsack capacity reduced by w[i]
// because it has item i packed in
// it already
else
sol[i] = 0; // Not enough space to pack item i
}
/* ---------------------------------------------
Use the solutions to the smaller problems
to solve original problem
--------------------------------------------- */
for ( i = 1; i ≤ N; i++ )
{
if ( C ≥ w[i] )
mySol[i] = sol[i] + v[i]; // Value is increased by v[i]
// because it has item i packed in
// it already
else
mySol[i] = 0; // Not enough space to pack item i
}
/* *************************
Find the best (maximum)
************************* */
myFinalSol = mySol[1];
for ( i = 2; i ≤ N; i++ )
if ( mySol[i] > myFinalSol )
myFinalSol = mySol[i];
return myFinalSol;
}
|
/* ========================================================
M(C) = max value achieved by knapsack with capacity C
======================================================== */
static int M(int[] v, int[] w, int C)
{
int[] sol, mySol;
int i, myFinalSol;
sol = new int[v.length];
mySol = new int[v.length];
/* ---------------------------
Base cases
--------------------------- */
if ( C == 0 )
{
return(0);
}
/* ==============================================
Divide and conquer procedure
============================================== */
/* ---------------------------------------
Solve the appropriate smaller problems
--------------------------------------- */
for ( i = 0; i < v.length; i++ )
{
if ( C >= w[i] )
sol[i] = M( v, w, C-w[i] ); // Knapsack capacity reduced by w[i]
// because it has item i packed in
// it already
else
sol[i] = 0; // Not enough space to pack item i
}
/* ---------------------------------------------
Use the solutions to the smaller problems
to solve original problem
--------------------------------------------- */
for ( i = 0; i < v.length; i++ )
{
if ( C >= w[i] )
mySol[i] = sol[i] + v[i]; // Value is increased by v[i]
// because it has item i packed in
// it already
else
mySol[i] = 0; // Not enough space to pack item i
}
/* *************************
Find the best (maximum)
************************* */
myFinalSol = mySol[0];
for ( i = 1; i < v.length; i++ )
if ( mySol[i] > myFinalSol )
myFinalSol = mySol[i];
return myFinalSol; // Return the overal best solution
}
|
How to run the program:
|
|
Therefore, to store the results computed by the recursive method, we will a one dimensional array:
int[] M = new int[ W+1 ] ;
|
if ( C == 0 )
return 0;
|
will compute the following result in the result array M[]:
M[ 0 ] = 0 ;
|
|
/* ========================================================
M_dp(W) = max value achieved by knapsack with capacity W
======================================================== */
static int M_dp(int[] v, int[] w, int W)
{
int[] sol, mySol;
int i, myFinalSol;
int[] M; // Data structure to store results
int C; // Index to run through M[]
sol = new int[v.length];
mySol = new int[v.length];
M = new int[W + 1]; // Create array
/* ---------------------------
Base cases
--------------------------- */
M[0] = 0;
/* ==============================================
The other values M[C]
============================================== */
for ( C = 1; C <= W; C++ )
{
/* ---------------------------------------
Solve the appropriate smaller problems
--------------------------------------- */
for ( i = 0; i < v.length; i++ )
{
if ( C >= w[i] )
sol[i] = M[ C-w[i] ]; // Knapsack capacity reduced by w[i]
// because it has item i packed in
// it already
else
sol[i] = 0; // Not enough space to pack item i
}
/* ---------------------------------------------
Use the solutions to the smaller problems
to solve original problem
--------------------------------------------- */
for ( i = 0; i < v.length; i++ )
{
if ( C >= w[i] )
mySol[i] = sol[i] + v[i]; // Value is increased by v[i]
// because it has item i packed in
// it already
else
mySol[i] = 0; // Not enough space to pack item i
}
/* *************************
Find the best (maximum)
************************* */
M[C] = mySol[0];
for ( i = 1; i < v.length; i++ )
if ( mySol[i] > M[C] )
M[C] = mySol[i];
}
return M[ W ]; // Return best value for knapsack of cap = W
}
|
How to run the program:
|
for ( C = 1; C <= W; C++ )
{
for ( i = 0; i < v.length; i++ )
{
....
}
}
// W = capacity of knapsack
// v.length = # different items = N
|
Therefore:
|