|
|
|
|
|
|
Solution:
|
|
Solution:
|
|
|
|
|
int M( k, C )
{
// Let us not worry about the base bases for now....
/* ==========================================
Divide and conquer procedure
========================================== */
if ( wk > C )
{
/* --------------------------------
Item k does not fit !!!
-------------------------------- */
myFinalSol = M( k-1, C ); // You have to leave item k out !!!
}
else
{
sol1 = M( k-1, C ); // Smaller problem when we leave out item k
sol2 = M( k-1, C-wk ); // Smaller problem when we pack item k
mySol1 = sol1; // I did not pack item k, no additional value
mySol2 = sol2 + vk; // I PACKED item k !!!
myFinalSol = min ( mySol1, mySol2 );
}
return ( myFinalSol );
}
|
|
Summary:
M(k, 0) = 0; for all k (can't packet anything into a 0-capacity knapsack)
M(0, C) = 0; for all C (can't packet anything if you have no items left !)
|
/* ===========================================================
M(k, C) = max. achievable value packet into a knapsack
of capacity C using items 1, 2, ..., k
=========================================================== */
int M(int k, int C)
{
/* ==============================
Base cases
============================== */
if ( k == 0 )
{
return(0); // no items left
}
if ( C == 0 )
{
return(0); // 0 capacity
}
/* ==========================================
Divide and conquer procedure
========================================== */
if ( wk > C )
{
/* --------------------------------
Item k does not fit !!!
-------------------------------- */
myFinalSol = M( k-1, C ); // You have to leave item k out !!!
}
else
{
sol1 = M( k-1, C ); // Smaller problem when we leave out item k
sol2 = M( k-1, C-wk ); // Smaller problem when we pack item k
mySol1 = sol1; // I did not pack item k, no additional value
mySol2 = sol2 + vk; // I PACKED item k !!!
myFinalSol = min ( mySol1, mySol2 );
}
return ( myFinalSol );
}
|
static int M(int[] v, int[] w, int k, int C)
{
int sol1, sol2, mySol1, mySol2, myFinalSol;
/* ---------------------------
Base cases
--------------------------- */
if ( k == 0 )
{
return(0); // No more items to pick from
}
if ( C == 0 )
{
return(0); // No more space
}
/* --------------------------------------------
Check if we can use the n-th typed item
-------------------------------------------- */
if ( w[k-1] > C ) // Item k-1 does not fit...
{
myFinalSol = M(v, w, k-1, C); // We must leave item k-1 out
}
else
{
/* ====================================
Solve smaller problems
==================================== */
sol1 = M(v, w, k-1, C); // Try leaving item k out
sol2 = M(v, w, k-1, C - w[k-1]); // Try packing item k
/* ==========================================================
Use solution of smaller problems to solve my problem
========================================================== */
mySol1 = sol1; // I did not pack item k-1, so no added value
mySol2 = sol2 + v[k-1]; // I packed item k-1, so add v[k-1] !
if ( mySol1 > mySol2 )
myFinalSol = mySol1;
else
myFinalSol = mySol2;
}
return(myFinalSol); // Return my solution
}
|
How to run the program:
|
|
|
|
/* ===========================================================
M(n, W) = max value achieved by knapsack with capacity W
filled with items 0, 1, ..., n-1
=========================================================== */
static int M(int[] v, int[] w, int n, int W)
{
int sol1, sol2, mySol1, mySol2;
int k, C;
int[][] M;
M = new int[n+1][W+1]; // Data structure
/* ---------------------------
Base cases
--------------------------- */
for ( int j = 0; j <= W; j++ )
M[0][j] = 0; // (k=0) No more items to pick from
for ( int i = 0; i <= n; i++ )
M[i][0] = 0; // (C=0) No more space
/* ====================================
Compute M[k][C]:
==================================== */
for ( k = 1; k <= n; k++ )
{
for ( C = 1; C <= W; C++ )
{
/* --------------------------------------------
Check if we can use the n-th typed item
-------------------------------------------- */
if ( w[k-1] > C ) // Item k-1 does not fit...
{
M[k][C] = M[k-1][C]; // We must leave item k-1 out
}
else
{
/* ====================================
Solve smaller problems
==================================== */
sol1 = M[k-1][C]; // Try leaving item k out
sol2 = M[k-1][C-w[k-1]]; // Try packing item k
/* ==========================================================
Use solution of smaller problems to solve my problem
========================================================== */
mySol1 = sol1; // I did not pack item k-1, so no added value
mySol2 = sol2 + v[k-1]; // I packed item k-1, so add v[k-1] !
if ( mySol1 > mySol2 )
M[k][C] = mySol1;
else
M[k][C] = mySol2;
}
}
}
return(M[n][W]); // Max value to pack n item in knapsack cap W
}
|
How to run the program:
|