Knapsack Branch And Bound

 #include<iostream.h>  
 #include<conio.h>  
 int c,z[20],t[50][50],s[20],i,j,pt[20],wt[20],x[20],m,n,y=1;  
 class knp  
 {  
      public:  
           void get();  
           void table();  
           void select();  
           void display();  
 }k;  
 void knp:: get()  
 {  
      cout<<"\nKNAPSACK PROBLEM";  
      cout<<"Enter the number of values: ";  
        cin>>n;  
        cout<<"Enter the capacity of the bag: ";  
        cin>>m;  
        cout<<"Enter the values: ";  
        int i;  
        for(i=1;i<=n;i++)  
           cin>>wt[i];  
        cout<<"Enter the profits: ";  
        for(i=1; i<=n; i++)  
           cin>>pt[i];  
 }  
 void knp:: table()  
 {  
      for(i=1;i<=n;i++)  
      {  
           for(j=0;j<=m;j++)  
           {  
                if((i==1)&&(j<wt[i]))  
                     t[i][j]=0;  
                if((i==1)&&(j>=wt[i]))  
                     t[i][j]=pt[i];  
                if((i>1)&&(j<wt[i]))  
                     t[i][j]=t[i-1][j];  
                if((i>1)&&(j>=wt[i]))  
                     t[i][j]=pt[i]+t[i-1][j-wt[i]];  
           }  
      }  
 }  
 void knp:: select()  
 {  
      int q=0;  
      c=0;  
      int a,b,d;  
      i=n;  
      for(j=m;j>=0;j--)  
      {  
           if(t[i][j]==t[i-1][j])  
           {  
                a=i-1;  
                if(t[a][j]!=t[a-1][j-wt[a]]+pt[a])  
                {  
                     z[c]=t[a][j];  
                     c++;  
                     s[q]=a;  
                     q++;  
                }  
                b=a-1;  
                d=j-wt[a];  
                if(t[a-1][d]!=t[b-1][d-wt[b]])  
                {  
                     z[c]=t[a-1][j-wt[a]];  
                     c++;  
                     s[q]=a-1;  
                     q++;  
                }  
           }  
           else  
           {  
                a=i-1;  
                if(t[a][j]!=t[a-1][j-wt[a]]+pt[a])  
                {  
                     z[c]=t[a][j];  
                     s[q]=a;  
                     q++;  
                }  
                b=a-1;  
                d=j-wt[a];  
                if(t[a-1][d]!=t[b-1][d-wt[b]])  
                {  
                     z[c]=t[a-1][j-wt[a]];  
                     c++;  
                     s[q]=a-1;  
                     q++;  
                }  
           }  
           j=d+1;  
      }  
 }  
 void knp:: display()  
 {  
      int profit=0;  
      for(int x=0; x<c;x++)  
        profit += pt[s[x]];  
      cout<<"\nThe Solution is\n";  
      for(x=0; x<c; x++)  
        cout<<s[x]<<'\t';  
      cout<<"\n\nMaximum Profit : "<<profit;  
 }  
 void main()  
 {  
      clrscr();  
      k.get();  
      k.table();  
      k.select();  
      k.display();  
      getch();  
 }  

Share This!


No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive