The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
Given weights and values of n items, put these items in a knapsack of capacity to get the maximum total value in the knapsack.
For e.g.
var weight = new int[]{6,4,2};
var values = new int[]{10,6,8};
capacity = 10
so, Consider the only subsets whose total weight is smaller than weight (6+4+2)> 10. result is 18.
Solution
Using recursion – Simply consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than capacity. From all such subsets, pick the maximum value subset.
using System;
namespace KnapsackProblem{
class Program
{
static void Main()
{
Console.WriteLine("Enter array size:");
var n = Convert.ToInt32(Console.ReadLine());
var weight = new int[n];
var val = new int[n];
Console.WriteLine("Enter weight");
for (int i = 0; i < n; i++)
{
weight[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter values");
for (int i = 0; i < n; i++)
{
val[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter capacity");
var capacity = Convert.ToInt32(Console.ReadLine());
var result = Knapsack(weight, val, n, capacity);
Console.WriteLine($"Maximum values {result}");
}
static int Knapsack(int[] weight, int[] val, int len, int capacity)
{
if (capacity == 0)
return 0;
if (len == 0)
return 0;
if (weight[len - 1] > capacity)
return Knapsack(weight, val, len - 1, capacity);
return Math.Max(Knapsack(weight, val, len - 1, capacity), val[len - 1] + Knapsack(weight, val, len - 1, capacity - weight[len - 1]));
}
}
}
Execute above program to see the output
Using Dynamic programming
using System;
namespace KnapsackProblem
{
class Program
{
static void Main()
{
Console.WriteLine("Enter array size:");
var n = Convert.ToInt32(Console.ReadLine());
var weight = new int[n];
var val = new int[n];
Console.WriteLine("Enter weight");
for (int i = 0; i < n; i++)
{
weight[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter values");
for (int i = 0; i < n; i++)
{
val[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter capacity");
var capacity = Convert.ToInt32(Console.ReadLine());
var result = knapsack_dp(weight, val, n, capacity);
Console.WriteLine($"Maximum values {result}");
Console.ReadLine();
}
static int knapsack_dp(int[] weight, int[] val, int len, int capacity)
{
var result = new int[len + 1, capacity + 1];
for (var j = 0; j <= capacity; j++)
result[0, j] = 0;
for (var i = 0; i <= len; i++)
result[i, 0] = 0;
for (var i = 1; i <= len; i++)
{
for (var j = 1; j <= capacity; j++)
{
if (weight[i - 1] <= j)
result[i, j] = Math.Max(result[i - 1, j], val[i - 1] + result[i - 1, j - weight[i - 1]]);
else
result[i, j] = result[i - 1, j];
}
}
return result[len, capacity];
}
}
}