C Program to implement 01 Knapsack problem using recursion
Program
#include<stdio.h>
int max(int a, int b)
{
return (a > b) ? a : b;
}
int knapsack(int capacity, int no_items, int weight[], int value[])
{
if (no_items == 0 || capacity == 0)
return 0;
if (weight[no_items - 1] > capacity)
return knapsack(capacity, no_items - 1, weight, value);
else
return max(value[no_items - 1] + knapsack(capacity - weight[no_items - 1], no_items - 1, weight, value),
knapsack(capacity, no_items - 1, weight, value));
}
void main()
{
int weight[20], value[20];
int i, no_items, capacity, profit;
printf("Enter the capacity of the knapsack:\n");
scanf("%d", &capacity);
printf("Enter the number of items:\n");
scanf("%d", &no_items);
printf("Enter weight and value of each product:\n");
for(i = 0; i < no_items; i++)
{
printf("Weight[%d]:\t", i);
scanf("%d", &weight[i]);
printf("Value[%d]:\t", i);
scanf("%d", &value[i]);
}
profit = knapsack(capacity, no_items, weight, value);
printf("Profit:\t%d\n", profit);
}
Output
$ gcc 0-1-knapsack-recursion.c
$ ./a.out
Enter the capacity of the knapsack:
70
Enter the number of items:
5
Enter weight and value of each product:
Weight[0]: 15
Value[0]: 70
Weight[1]: 10
Value[1]: 155
Weight[2]: 5
Value[2]: 80
Weight[3]: 20
Value[3]: 210
Weight[4]: 22
Value[4]: 125
Profit: 570