1+ import java .sql .Time ;
2+ import java .util .Arrays ;
3+
4+ public class Knapsack {
5+
6+ //Description:
7+ // This function solves the 0/1 Knapsack problem to find the maximum value of items
8+ // that can fit within a limited weight capacity. It uses a bottom-up dynamic
9+ // programming approach by building a table of optimal solutions for all subproblems.
10+
11+ //Time Complexity: O(n * W)
12+ //- Where 'n' is the number of items and 'W' is the knapsack capacity.
13+ //- The complexity arises from the nested loops used to fill the DP table of size (n+1) x (W+1).
14+
15+ //Space Complexity: O(n * W)
16+ //- We use a 2D array (DP table) of size (n+1) x (W+1) to store the results of subproblems.
17+
18+ /**
19+ * Solves the 0/1 Knapsack problem using Dynamic Programming (Tabulation).
20+ *
21+ * @param weights An array of weights of the items.
22+ * @param values An array of values of the items.
23+ * @param capacity The maximum weight capacity of the knapsack.
24+ * @param n The number of items.
25+ * @return The maximum value that can be put in the knapsack.
26+ */
27+ public int solveKnapsack (int [] weights , int [] values , int capacity ) {
28+ int n = weights .length ;
29+
30+ // dp[i][w] will be the maximum value that can be obtained
31+ // using the first 'i' items with a knapsack capacity of 'w'.
32+ // We use n+1 and capacity+1 to make indexing easier (1-based).
33+ int [][] dp = new int [n + 1 ][capacity + 1 ];
34+
35+ // Build table dp[][] in a bottom-up manner.
36+ for (int i = 0 ; i <= n ; i ++) {
37+ for (int w = 0 ; w <= capacity ; w ++) {
38+ // Base case: If there are no items (i=0) or the capacity is 0 (w=0),
39+ // the value is 0.
40+ if (i == 0 || w == 0 ) {
41+ dp [i ][w ] = 0 ;
42+ }
43+ // If the weight of the i-th item is less than or equal to the current capacity 'w'
44+ else if (weights [i - 1 ] <= w ) {
45+ // We have two choices:
46+ // 1. Include the i-th item: value is values[i-1] + value from remaining capacity.
47+ // 2. Exclude the i-th item: value is the same as with i-1 items.
48+ // We take the maximum of these two choices.
49+ // Note: weights[i-1] and values[i-1] correspond to the i-th item.
50+ dp [i ][w ] = Math .max (
51+ values [i - 1 ] + dp [i - 1 ][w - weights [i - 1 ]], // Include item
52+ dp [i - 1 ][w ] // Exclude item
53+ );
54+ }
55+ // If the weight of the i-th item is more than the current capacity 'w',
56+ // we cannot include it. So the value is the same as with i-1 items.
57+ else {
58+ dp [i ][w ] = dp [i - 1 ][w ];
59+ }
60+ }
61+ }
62+
63+ // The final answer is in the bottom-right corner of the table.
64+ return dp [n ][capacity ];
65+ }
66+
67+ public static void main (String [] args ) {
68+ Knapsack ks = new Knapsack ();
69+
70+ int [] values = {60 , 100 , 120 };
71+ int [] weights = {10 , 20 , 30 };
72+ int capacity = 50 ;
73+
74+ int maxValue = ks .solveKnapsack (weights , values , capacity );
75+
76+ System .out .println ("Items available:" );
77+ System .out .println ("Values: " + Arrays .toString (values ));
78+ System .out .println ("Weights: " + Arrays .toString (weights ));
79+ System .out .println ("Knapsack Capacity: " + capacity );
80+ System .out .println ("Maximum value that can be obtained is: " + maxValue );
81+
82+ // Expected output for this example is 220.
83+ // This is achieved by taking the items with weight 20 (value 100) and 30 (value 120).
84+ // Total weight = 20 + 30 = 50.
85+ // Total value = 100 + 120 = 220.
86+ }
87+ }
0 commit comments