Well , it's been three days since my last logged on and posted a blog . Though I knew that no one would read my
blog (for now). I would post them for myself . Sorry , my point was that I felt so sorry for missing for three days !
I did not come back emptied hands ! Finally , the knapsack problem was solved ! Even though not by myself , but I
felt happy for knowing the answer . The knapsack problem was solved by the technique of Dynamic Programming.
For the main problem's result could be obtained by its sub-problems . It's always easier to say than done . I was stuck
in the initilization of a two dimensional array in the first place . After a mind storm , I reviewed the mechinism of two
dimensional array's placement. For an instance of a two dimensional array : a[i][j] , which can be transformed into
a one dimensional array a[i*j] . So , how can we addressing the two dimensional array by using a two dimensional
array pointer ? In order to addressing the two dimensional array , you need to understand how this kind of array was
arranged in the memory. About that , I suggest you read <C How to Program> if you don't have previous knowledge about
arrays in C language .
All right , let me show you what I had been working these days . (Actually , I'd been playing games these days... Shame on me !)
The algorithm that I implemented was named Knapsack . Oh , it seemed I had already said it early in this passage ...
Forgive my short memory ,please . This problem aims finding the most valuable combination of items which put into
a bag whose capacity is C .Each item has distinct weight and value . Talk is cheap , show me the code ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* these two pointers point to weights and values*/
int *weights=0,*values=0;
/* F is the item's choice, p stores sub problem's result */
int *F,*p;
/* n is number of items , c is the capacity of the bag */
int n,c;
/* algorithm for solviing knapsack problem */
int knapsack(const int i,const int j );
int main()
{
/* get number of items and capacity of bag */
int i,j;
scanf("%d %d",&n,&c);
j=c;
/* initilize the array */
weights=(int*)malloc(sizeof(int)*(n+1));
F =(int*)malloc(sizeof(int)*(n+1));
values =(int*)malloc(sizeof(int)*(n+1));
p=(int*)malloc(sizeof(int)*(n+1) * (j+1));
weights[0]=0;
values[0]=0;
/* get items' weights and values */
for(i=1;i<=n;i++){
scanf("%d %d",&weights[i],&values[i]);
}
/* initilize the sub problems' results */
for(i=0;i<=n;i++)
for(j=0;j<=c;j++){
if(i==0){
(&p[i])[j]=0;
}else if(j==0){
(&p[i*(c+1)])[j]=0;
}else{
(&p[i*(c+1)])[j]=-1;
}
}
/* output the maximum value found */
printf("%d\n",knapsack(n,c));
/* compute whice item should be put into the bag */
for(j=c,i=n;i>0;i--){
if( (&p[i*(c+1)])[j] > (&p[(i-1)*(c+1)])[j]){
F[i]=1;j-=weights[i];
}else
F[i]=0;
}
/* out put the result computed above */
for(i=1;i<=n;i++)
printf("%d\n",F[i]);
return 0;
}
int knapsack(const int i,const int j )
{
int value;
/* if the result is yet to be computed */
if((&p[i*(c+1)])[j]<0){
/* if the bag's current capacity is not capable of storing current item */
if(j<weights[i]){
value=knapsack(i-1,j);
}
/* if the bag 's current capacity is capable of storing current item */
else{
int tmp=knapsack(i-1,j); // comput the one without current item
int tmp1=knapsack(i-1,j-weights[i])+values[i];//comput the one with current item
value=(tmp>tmp1? tmp:tmp1);
}
(&p[i*(c+1)])[j]=value;
}
return (&p[i*(c+1)])[j];
}
I put many comments in my codes. So it won't be difficult to understand what it does . Then I don't explain it to you .
All right , got to sleep . Good night fellows .