forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
52 lines (47 loc) · 1.8 KB
/
Knapsack.java
File metadata and controls
52 lines (47 loc) · 1.8 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
package dynamicprogramming;
/**
* Implementation of the 0/1 Knapsack Problem using Dynamic Programming.
*
* Given weights and values of n items, put these items in a knapsack of capacity W
* to get the maximum total value in the knapsack.
*/
public class Knapsack {
/**
* Returns the maximum value that can be put in a knapsack of capacity W.
*
* @param W capacity of the knapsack
* @param wt array of weights of the items
* @param val array of values of the items
* @param n number of items
* @return maximum value achievable with given capacity
*/
public static int knapSack(int W, int[] wt, int[] val, int n) {
int[][] dp = new int[n + 1][W + 1];
// Build table dp[][] in bottom-up manner
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0; // Base case: no items or zero capacity
} else if (wt[i - 1] <= w) {
// Include the item or exclude it, choose max value
dp[i][w] = Math.max(val[i - 1] + dp[i - 1][w - wt[i - 1]],
dp[i - 1][w]);
} else {
// Item can't be included because it weighs more than capacity
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
// Example usage and simple test case
public static void main(String[] args) {
int[] values = { 60, 100, 120 };
int[] weights = { 10, 20, 30 };
int capacity = 50;
int n = values.length;
int maxValue = knapSack(capacity, weights, values, n);
System.out.println("Maximum value in Knapsack = " + maxValue);
// Expected output: 220
}
}