Advertisement
hasanfaraaz

Knapsack algorithm

Nov 15th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<process.h>
  4. int n, w[10],p[10],M;
  5. void knapsack();
  6. int max(int, int);
  7.  
  8. void main()
  9. {
  10.  
  11.         int i,j;
  12.         clrscr();
  13.         printf("Enter no. of items \n(How many items do you wnt to get recharged for ?)");
  14.         scanf("%d",&n);
  15.         printf("Enter the weigths \n(Supply the recharge plans here to the program)")
  16.         for(i=1;i<=n;i++)
  17.         {
  18.                 scanf("%d", &w[i]);
  19.         }
  20.  
  21.         printf("Enter profit for each item \n(enter the validity or how much ever talktime you get by the plan)");
  22.         for(i=1;i<=n;i++)
  23.         {
  24.                 scanf("%d", &p[i]);
  25.         }
  26.  
  27.         printf("Enter the knapsack's capacity(User budget)");
  28.         scanf("%d", &M);
  29.         knpasck();
  30.         getch();
  31. }
  32.  
  33. void knapsack()
  34. {
  35.         int v[10][10],i,j;
  36.         for(i=0; i<=n;i++)
  37.         {
  38.                 for(j=0;j<M;j++)
  39.                 {
  40.                         if(i==0||j==0)
  41.                         {
  42.                                 v[i][j]=0;
  43.                         }
  44.                         else if(j-w[i]<0)
  45.                         {
  46.                                 v[i][j]=v[i-1][j];
  47.                         }
  48.                         else
  49.                         {
  50.                                 v[i][j]=max(v[i-1][j],p[i]+v[i-1][j-w[i]]);
  51.                         }
  52.                 }
  53.         }
  54.  
  55.         printf("Matrix is \n ");
  56.         for(i=0;i<=n;i++)
  57.         {
  58.                 for(j=0;j<=M;j++)
  59.                 {
  60.                         printf("%d\t",v[i][j]);
  61.                 }
  62.                 printf("\n");
  63.         }
  64.         printf("Optimal solution is =%d",v[n][m]);//the last element shown in the matrix i.e. the total amount used from the given budget
  65. }
  66.  
  67. int max(int x, int y)
  68. {
  69.         if(x>y)
  70.         {
  71.                 return x;
  72.         }
  73.         else
  74.         {
  75.                 return y;
  76.         }
  77. }
  78.  
  79. /* Give this as input
  80.  
  81. Obtain optimal solution of the given table
  82. _______________________
  83. |item | weight |profit|
  84. -----------------------
  85. 1     | 2      |12    |
  86. 2     | 1      |10    |
  87. 3     | 3      |20    |
  88. 4     | 2      |15    |
  89. -----------------------
  90.  
  91. Your input to the program should be a 2 dimensional maxtrix:-
  92.  
  93.  
  94. 0 0  0  0   0  0
  95. 0 0  12 12 12 12
  96. 0 10 12 22 22 22
  97. 0 10 12 22 30 32
  98. 0 10 15 25 30 37
  99.  
  100. (Last element of this matrix here is the used up budget from the given budget)
  101. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement