-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path0 1 Knapsack_java_gopalthedev
142 lines (122 loc) · 4.73 KB
/
0 1 Knapsack_java_gopalthedev
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public class KnapsackProblem {
/**
* Dynamic Programming Solution for 0/1 Knapsack
* Time Complexity: O(nW)
* Space Complexity: O(nW)
*/
public static int knapsackDP(int[] weights, int[] values, int capacity) {
int n = weights.length;
int[][] dp = new int[n + 1][capacity + 1];
// Build table dp[][] in bottom-up manner
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
}
else if (weights[i - 1] <= w) {
dp[i][w] = Math.max(
values[i - 1] + dp[i - 1][w - weights[i - 1]],
dp[i - 1][w]
);
}
else {
dp[i][w] = dp[i - 1][w];
}
}
}
// Store selected items
boolean[] selectedItems = new boolean[n];
int w = capacity;
int i = n;
while (i > 0 && w > 0) {
if (dp[i][w] != dp[i - 1][w]) {
selectedItems[i - 1] = true;
w = w - weights[i - 1];
}
i--;
}
// Print selected items
System.out.println("Selected items (index, weight, value):");
for (i = 0; i < n; i++) {
if (selectedItems[i]) {
System.out.printf("(%d, %d, %d)%n", i, weights[i], values[i]);
}
}
return dp[n][capacity];
}
/**
* Space-Optimized Dynamic Programming Solution
* Time Complexity: O(nW)
* Space Complexity: O(W)
*/
public static int knapsackDPOptimized(int[] weights, int[] values, int capacity) {
int n = weights.length;
int[] dp = new int[capacity + 1];
for (int i = 0; i < n; i++) {
for (int w = capacity; w >= weights[i]; w--) {
dp[w] = Math.max(dp[w], values[i] + dp[w - weights[i]]);
}
}
return dp[capacity];
}
/**
* Recursive Solution with Memoization
* Time Complexity: O(nW)
* Space Complexity: O(nW)
*/
public static int knapsackRecursive(int[] weights, int[] values, int capacity) {
int n = weights.length;
Integer[][] memo = new Integer[n + 1][capacity + 1];
return knapsackRecursiveHelper(weights, values, capacity, n - 1, memo);
}
private static int knapsackRecursiveHelper(int[] weights, int[] values,
int capacity, int index,
Integer[][] memo) {
// Base case
if (index < 0 || capacity == 0) {
return 0;
}
// Check if already computed
if (memo[index][capacity] != null) {
return memo[index][capacity];
}
// If weight of current item is more than capacity,
// skip this item
if (weights[index] > capacity) {
return memo[index][capacity] = knapsackRecursiveHelper(
weights, values, capacity, index - 1, memo
);
}
// Return maximum of two cases:
// 1. Item included
// 2. Item not included
memo[index][capacity] = Math.max(
values[index] + knapsackRecursiveHelper(
weights, values, capacity - weights[index], index - 1, memo
),
knapsackRecursiveHelper(weights, values, capacity, index - 1, memo)
);
return memo[index][capacity];
}
public static void main(String[] args) {
int[] values = {60, 100, 120};
int[] weights = {10, 20, 30};
int capacity = 50;
System.out.println("Dynamic Programming Solution:");
int maxValueDP = knapsackDP(weights, values, capacity);
System.out.println("Maximum value: " + maxValueDP);
System.out.println("\nSpace-Optimized DP Solution:");
int maxValueDPOpt = knapsackDPOptimized(weights, values, capacity);
System.out.println("Maximum value: " + maxValueDPOpt);
System.out.println("\nRecursive Solution with Memoization:");
int maxValueRec = knapsackRecursive(weights, values, capacity);
System.out.println("Maximum value: " + maxValueRec);
// Additional test case
int[] values2 = {10, 40, 30, 50};
int[] weights2 = {5, 4, 6, 3};
int capacity2 = 10;
System.out.println("\nTest Case 2 - Dynamic Programming Solution:");
maxValueDP = knapsackDP(weights2, values2, capacity2);
System.out.println("Maximum value: " + maxValueDP);
}
}