-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKnapsackZeroOne.java
74 lines (64 loc) · 2.43 KB
/
KnapsackZeroOne.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package algorithms.study;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* 0/1 KnapsackZeroOne
* https://www.youtube.com/watch?v=zRza99HPvkQ&list=PLDN4rrl48XKpZkf03iYFl-O29szjTrs_O&index=55
* Difficulty : Medium
* Related Topics : Dynamic Programming
*
* created by Cenk Canarslan on 2021-11-19
*/
public class KnapsackZeroOne {
@Test
public void testKnapsackZeroOne() {
// knapsack(int[] profit, int[] weight, int knapsackCapacity)
assertEquals(8, knapsack(new int[]{1, 2, 5, 6}, new int[]{2, 3, 4, 5}, 8));
assertEquals(8, knapsack(new int[]{2, 2, 5, 6}, new int[]{1, 2, 3, 4}, 5));
assertEquals(9, knapsack(new int[]{1, 4, 5, 7}, new int[]{1, 3, 4, 5}, 7));
assertEquals(7, knapsack(new int[]{2, 3, 5}, new int[]{1, 2, 3}, 4));
assertEquals(220, knapsack(new int[]{60, 100, 120}, new int[]{10, 20, 30}, 50));
}
public static void main(String[] args) {
int[] profit = {1,2,5,6};
int[] weight = {2,3,4,5};
int knapsackCapacity = 8;
KnapsackZeroOne knapsack = new KnapsackZeroOne();
int k = knapsack.knapsack(profit, weight, knapsackCapacity);
System.out.println("knapsack = " + k);
}
/**
* Time Complexity = O(n.w) (n->number of items, w->capacity)
*
* @param profit
* @param weight
* @param knapsackCapacity
* @return
*/
private int knapsack(int[] profit, int[] weight, int knapsackCapacity) {
if (profit.length != weight.length) return -1;
int[][] dp = new int[profit.length + 1][knapsackCapacity + 1];
for (int i = 0; i <= profit.length; i++) {
for (int w = 0; w <= knapsackCapacity; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else if (weight[i-1] <= w) {
dp[i][w] = Math.max(profit[i-1] + dp[i-1][w-weight[i-1]], dp[i-1][w]);
} else {
dp[i][w] = dp[i-1][w];
}
}
}
printMatrix(dp);
return dp[profit.length][knapsackCapacity];
}
private void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
// System.out.println("matrix["+i+"]["+j+"] = " + matrix[i][j]);
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}