-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack_Fractional_DP_FractionalKnapsackProblem.java
More file actions
76 lines (61 loc) · 2.62 KB
/
Knapsack_Fractional_DP_FractionalKnapsackProblem.java
File metadata and controls
76 lines (61 loc) · 2.62 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
65
66
67
68
69
70
71
72
73
74
75
76
package Algorithms.DynamicProgramming;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 27 May 2025
* @link <a href="https://www.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1">GeeksForGeeks problem link</a>
* @link <a href="https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/">GeeksForGeeks article link</a>
*
*/
public class Knapsack_Fractional_DP_FractionalKnapsackProblem {
public static void main(String[] args) {
int[] weights = {1, 2, 3, 2};
int[] values = {10, 15, 40, 30};
int capacity = 6;
System.out.println("Maximum value in Knapsack = " + knapsack(weights, values, capacity));
}
public static int knapsack(int[] weights, int[] values, int capacity) {
int n = weights.length;
int[][] dp = new int[n + 1][capacity + 1];
for (int i = 1; i <= n; i++) {
int itemWeight = weights[i - 1];
int itemValue = values[i - 1];
for (int w = 1; w <= capacity; w++) {
// if(i==0 || w==0) dp[i][w] = 0; // Base case, already initialized to 0
if (itemWeight <= w) {
// max(top, top_left+itemVal)
dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - itemWeight] + itemValue);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][capacity];
}
public static int knapsackSpaceOptimized(int[] weights, int[] values, int capacity) {
int n = weights.length;
int[] dp = new int[capacity + 1];
for (int i = 0; i < n; i++) {
int itemWeight = weights[i];
int itemValue = values[i];
for (int w = capacity; w >= itemWeight; w--) {
dp[w] = Math.max(dp[w], dp[w - itemWeight] + itemValue);
}
}
return dp[capacity];
}
/**
* Working but TLE
*/
public static int knapsackRecursive(int[] weights, int[] values, int capacity) {
return backtrack(weights, values, capacity, 0); // no need for currSum, just decrease capacity till 0
}
public static int backtrack(int[] weights, int[] values, int capacity, int currentIndex) {
if (capacity <= 0 || currentIndex >= weights.length) return 0;
int include = 0;
if (weights[currentIndex] <= capacity) { // currWeight <= capacity
include = values[currentIndex] + backtrack(weights, values, capacity - weights[currentIndex], currentIndex + 1);
}
int exclude = backtrack(weights, values, capacity, currentIndex + 1);
return Math.max(include, exclude);
}
}