-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
64 lines (55 loc) · 2.48 KB
/
Knapsack.java
File metadata and controls
64 lines (55 loc) · 2.48 KB
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
package DynamicProgramming;
import java.util.Arrays;
import java.util.Random;
public class Knapsack {
private final static int SIZE = 10_000;
public static void main(String[] args) {
int[] profits = new int[SIZE], weights = new int[SIZE];
int capacity = SIZE >> 2;
//var dp = new Integer[SIZE][capacity + 1];
var random = new Random(42);
Arrays.fill(profits, random.nextInt(1, SIZE));
Arrays.fill(weights, random.nextInt(1, SIZE / 10));
var start = System.currentTimeMillis();
int maxProfit = calculateProfitBottomUp(profits, weights, capacity, SIZE);
var end = System.currentTimeMillis();
System.out.printf("Max profit: %d. It took %dms to compute.", maxProfit, end - start);
}
// private static int calculateProfitBruteForce(int[] profits, int[] weights, int capacity, int n, int i) {
// if(capacity <= 0 || i >= n) return 0;
// int profit1 = 0;
// if(weights[i] <= capacity) {
// profit1 = profits[i] + calculateProfitBruteForce(profits, weights, capacity - weights[i], n, i + 1);
// }
//
// int profit2 = calculateProfitBruteForce(profits, weights, capacity, n, i + 1);
// return Math.max(profit1, profit2);
// }
// private static int calculateProfitTopDown(int[] profits, int[] weights, Integer[][] dp, int capacity, int n, int i) {
// if(capacity <= 0 || i >= n) return 0;
// if(dp[i][capacity] != null) return dp[i][capacity];
//
// int profit1 = 0;
// if(weights[i] <= capacity) {
// profit1 = profits[i] + calculateProfitTopDown(profits, weights, dp, capacity - weights[i], n, i + 1);
// }
//
// int profit2 = calculateProfitTopDown(profits, weights, dp, capacity, n, i + 1);
// dp[i][capacity] = Math.max(profit1, profit2);
// return dp[i][capacity];
// }
private static int calculateProfitBottomUp(int[] profits, int[] weights, int capacity, int n) {
//int[][] dp = new int[2][capacity + 1];
int[] dp = new int[capacity + 1];
//for(int i = 0; i < SIZE; i++) dp[i][0] = 0;
for(int c = 0; c <= capacity; c++) if(weights[0] <= c) dp[c] = profits[0];
for(int i = 1; i < n; i++) {
for(int c = capacity; c >= 0; c--) {
int profit1 = 0;
if(weights[i] <= c) profit1 = profits[i] + dp[c - weights[i]];
dp[c] = Math.max(profit1, dp[c]);
}
}
return dp[capacity];
}
}