forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnapsackUnbounded.java
92 lines (71 loc) · 2.98 KB
/
KnapsackUnbounded.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* This file contains a dynamic programming solutions to the classic unbounded knapsack problem
* where are you are trying to maximize the total profit of items selected without exceeding the
* capacity of your knapsack.
*
* <p>Version 1: Time Complexity: O(nW) Version 1 Space Complexity: O(nW)
*
* <p>Version 2: Time Complexity: O(nW) Space Complexity: O(W)
*
* <p>Tested code against: https://www.hackerrank.com/challenges/unbounded-knapsack
*
* @author William Fiset, [email protected]
*/
package com.williamfiset.algorithms.dp;
public class KnapsackUnbounded {
/**
* @param maxWeight - The maximum weight of the knapsack
* @param W - The weights of the items
* @param V - The values of the items
* @return The maximum achievable profit of selecting a subset of the elements such that the
* capacity of the knapsack is not exceeded
*/
public static int unboundedKnapsack(int maxWeight, int[] W, int[] V) {
if (W == null || V == null || W.length != V.length || maxWeight < 0)
throw new IllegalArgumentException("Invalid input");
final int N = W.length;
// Initialize a table where individual rows represent items
// and columns represent the weight of the knapsack
int[][] DP = new int[N + 1][maxWeight + 1];
// Loop through items
for (int i = 1; i <= N; i++) {
// Get the value and weight of the item
int w = W[i - 1], v = V[i - 1];
// Consider all possible knapsack sizes
for (int sz = 1; sz <= maxWeight; sz++) {
// Try including the current element
if (sz >= w) DP[i][sz] = DP[i][sz - w] + v;
// Check if not selecting this item at all is more profitable
if (DP[i - 1][sz] > DP[i][sz]) DP[i][sz] = DP[i - 1][sz];
}
}
// Return the best value achievable
return DP[N][maxWeight];
}
public static int unboundedKnapsackSpaceEfficient(int maxWeight, int[] W, int[] V) {
if (W == null || V == null || W.length != V.length)
throw new IllegalArgumentException("Invalid input");
final int N = W.length;
// Initialize a table where we will only keep track of
// the best possible value for each knapsack weight
int[] DP = new int[maxWeight + 1];
// Consider all possible knapsack sizes
for (int sz = 1; sz <= maxWeight; sz++) {
// Loop through items
for (int i = 0; i < N; i++) {
// First check that we can include this item (we can't include it if
// it's too heavy for our knapsack). Assumming it fits inside the
// knapsack check if including this element would be profitable.
if (sz - W[i] >= 0 && DP[sz - W[i]] + V[i] > DP[sz]) DP[sz] = DP[sz - W[i]] + V[i];
}
}
// Return the best value achievable
return DP[maxWeight];
}
public static void main(String[] args) {
int[] W = {3, 6, 2};
int[] V = {5, 20, 3};
int knapsackValue = unboundedKnapsackSpaceEfficient(10, W, V);
System.out.println("Maximum knapsack value: " + knapsackValue);
}
}