Advertisement
rootUser

KNAPSACK (0,1) (OWN)

Jul 26th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int sackCapacity;
  5. int totalItems;
  6. int weight_array[100],benifit_array[100];
  7. int V_knapSack_0_1[100][100];
  8.  
  9. void input();
  10. void print();
  11. int findMaximum(int,int);
  12. void knapSack01ArrayDisplay();
  13.  
  14. int main(void)
  15. {
  16.     input();
  17.     print();
  18.     knapSack01ArrayDisplay();
  19.     return 0;
  20. }
  21. void input()
  22. {
  23.     cout<<"Enter sack capacity : ";
  24.     cin>>sackCapacity;
  25.     cout<<"Enter total items : ";
  26.     cin>>totalItems;
  27.     int i;
  28.     for(i=1;i<=totalItems;i++)
  29.     {
  30.         cout<<"Enter item "<<i<<"'s weight and benifit  : ";
  31.         int weight_i,benifit_i;
  32.         cin>>weight_i>>benifit_i;
  33.         weight_array[i]=weight_i;
  34.         benifit_array[i]=benifit_i;
  35.     }
  36. }
  37. void print()
  38. {
  39.     int i;
  40.     cout<<"Items (i) : "<<endl;
  41.     for(i=1;i<=totalItems;i++)
  42.     {
  43.         cout<<i<<"\t";
  44.     }
  45.     cout<<endl;
  46.     cout<<"Value/Befinit (val) : "<<endl;
  47.     for(i=1;i<=totalItems;i++)
  48.     {
  49.         cout<<benifit_array[i]<<"\t";
  50.     }
  51.     cout<<endl;
  52.     cout<<"Weight (wt) : "<<endl;
  53.     for(i=1;i<=totalItems;i++)
  54.     {
  55.         cout<<weight_array[i]<<"\t";
  56.     }
  57.     cout<<endl;
  58. }
  59. int findMaximum(int a,int b)
  60. {
  61.     if(a>b)
  62.     {
  63.         return a;
  64.     }
  65.     return b;
  66. }
  67. void knapSack01ArrayDisplay()
  68. {
  69.     int i,w;
  70.     int row_i=totalItems;
  71.     int column_w=sackCapacity;
  72.     for(i=0;i<=row_i;i++)
  73.     {
  74.         for(w=0;w<=column_w;w++)
  75.         {
  76.             if(i==0||w==0)
  77.             {
  78.                 V_knapSack_0_1[i][w]=0;
  79.             }
  80.         }
  81.     }
  82.     for(i=0;i<=row_i;i++)
  83.     {
  84.         for(w=0;w<=column_w;w++)
  85.         {
  86.             if(weight_array[i]>w)
  87.             {
  88.                 V_knapSack_0_1[i][w]=V_knapSack_0_1[i-1][w];
  89.  
  90.             }
  91.             else if(weight_array[i]<=w)
  92.             {
  93.                 V_knapSack_0_1[i][w]=findMaximum( V_knapSack_0_1[i-1][w] , benifit_array[i]+V_knapSack_0_1[i-1][w-weight_array[i]] );
  94.             }
  95.         }
  96.     }
  97.     cout<<"Knapsack_0_1 matrix : "<<endl;
  98.     for(i=0;i<=row_i;i++)
  99.     {
  100.         for(w=0;w<=column_w;w++)
  101.         {
  102.             cout<<V_knapSack_0_1[i][w]<<"\t";
  103.         }
  104.         cout<<endl;
  105.     }
  106.     cout<<"Final result : "<<V_knapSack_0_1[row_i][column_w]<<endl;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement