0 1 Knapsack Problem – Dynamic Programming Solution
0 1 Knapsack Problem – Dynamic Programming Solution:
Problem Description:
Given weights and values of n items, put these items in a knapsack of capacity M to get the maximum total value in the knapsack.
Note that, you can select items, the sum of whose weight is less than or equal to the capacity of knapsack, W.
Problem Solution:
The problem is to find a subset of items such that – the sum of weight of all the items in the subset should be less than or equal to knapsack capacity out of all subsets that satisfy criteria 1 above, the desired subset is the one in which the sum values of its items is maximum.
Expected Input and Output:
Case-1:
number of items, n=4
weight of items, w[]=2 3 4 5
value of items, v[]=3 4 5 6
capacity of knapsack, M=5
maximum attainable value of items=7 by collecting first and second item in the knapsack.
Case-2:
n=3
w[]=3 2 1
v[]=5 3 4
M=5
maximum attainable value of items=9
by collecting first and last item in the knapsack.
Case-3:
n=5
w[]=1 2 3 2 2
v[]=8 4 0 4 3
M=4
maximum attainable value of items=13
Solution:
Optimized Solution:
import java.util.*;
public class Knapsack0_1problem {
public int[][] solve(int w, int wt[], int val[], int n) {
System.out.println("Original Values" + w + " " + n+ " " + Arrays.toString(wt)+ " " + Arrays.toString(val));
int[][] k = new int[n+1][w+1];
System.out.println("K array");
for(int r=0;r<=n;r++)
{
for(int c = 0; c<=w; c++)
{
k[r][c] = 0;
}
}
System.out.println("K array after initializing to 0"+Arrays.toString(k));
System.out.println();
for(int r=0;r<=n;r++)
{
for(int c = 0; c<=w; c++)
{
if(r==0 || c==0)
{
k[r][c] = 0;
}
else
{
if(c<wt[r-1])
{
k[r][c] = k[r-1][c];
}
else
{
k[r][c] = Math.max(k[r-1][c], val[r-1] + k[r-1][c-wt[r-1]]);
}
}
}
}
System.out.println("printing k array after calculation");
for(int p=0; p<=n;p++)
{
for(int q=0;q<=w;q++)
{
System.out.print(k[p][q]);
}
System.out.println();
}
return k;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int w = s.nextInt();
int n = s.nextInt();
int[] wt = new int[n];
int[] val = new int[n];
for(int i=0; i<n ; i++)
{
wt[i] = s.nextInt();
val[i] = s.nextInt();
}
// System.out.println(w + " " + n+ " " + Arrays.toString(wt)+ " " + Arrays.toString(val));
Knapsack0_1problem kp = new Knapsack0_1problem();
System.out.println("printing in main method ");
int[][] k = kp.solve(w, wt, val, n);
for(int p=0; p<=n;p++)
{
for(int q=0;q<=w;q++)
{
System.out.print(k[p][q]);
}
System.out.println();
}
System.out.println("Maximum profit is: " + k[n][w]);
int i = n, j = w;
while(i>0 && j>0)
{
if(k[i][j] == k[i-1][j])
{
System.out.println(i + " is not included");
i-=1;
}
else
{
System.out.println(i + "is included");
i-=1;
j = j-wt[i];
}
}
}
}
Time Complexity: O(n*w)
Execution:
Successfully Executed
8
4
2
1
3
2
4
5
5
6
printing in main method
Original Values 8 4 [2, 3, 4, 5] [1, 2, 5, 6]
printing k array after calculation
000000000
001111111
001223333
001255677
001256678
**
000000000
001111111
001223333
001255677
001256678
Maximum profit is: 8
4 is included
3 is not included
2 is included
K(n, W)
K(3, 2)
/ \
/ \
K(2, 2) K(2, 1)
/ \ / \
/ \ / \
K(1, 2) K(1, 1) K(1, 1) K(1, 0)
/ \ / \ / \
/ \ / \ / \
K(0, 2) K(0, 1) K(0, 1) K(0, 0) K(0, 1) K(0, 0)
Comments
Post a Comment