|
1 | 1 | package com.thealgorithms.dynamicprogramming;
|
2 | 2 |
|
3 |
| -/* A Naive recursive implementation |
4 |
| -of 0-1 Knapsack problem */ |
| 3 | +/** |
| 4 | + * A naive recursive implementation of the 0-1 Knapsack problem. |
| 5 | + * |
| 6 | + * <p>The 0-1 Knapsack problem is a classic optimization problem where you are |
| 7 | + * given a set of items, each with a weight and a value, and a knapsack with a |
| 8 | + * fixed capacity. The goal is to determine the maximum value that can be |
| 9 | + * obtained by selecting a subset of the items such that the total weight does |
| 10 | + * not exceed the knapsack's capacity. Each item can either be included (1) or |
| 11 | + * excluded (0), hence the name "0-1" Knapsack.</p> |
| 12 | + * |
| 13 | + * <p>This class provides a brute-force recursive approach to solving the |
| 14 | + * problem. It evaluates all possible combinations of items to find the optimal |
| 15 | + * solution, but this approach has exponential time complexity and is not |
| 16 | + * suitable for large input sizes.</p> |
| 17 | + * |
| 18 | + * <p><b>Time Complexity:</b> O(2^n), where n is the number of items.</p> |
| 19 | + * |
| 20 | + * <p><b>Space Complexity:</b> O(n), due to the recursive function call stack.</p> |
| 21 | + */ |
5 | 22 | public final class BruteForceKnapsack {
|
6 | 23 | private BruteForceKnapsack() {
|
7 | 24 | }
|
8 |
| - // Returns the maximum value that |
9 |
| - // can be put in a knapsack of |
10 |
| - // capacity W |
| 25 | + |
| 26 | + /** |
| 27 | + * Solves the 0-1 Knapsack problem using a recursive brute-force approach. |
| 28 | + * |
| 29 | + * @param w the total capacity of the knapsack |
| 30 | + * @param wt an array where wt[i] represents the weight of the i-th item |
| 31 | + * @param val an array where val[i] represents the value of the i-th item |
| 32 | + * @param n the number of items available for selection |
| 33 | + * @return the maximum value that can be obtained with the given capacity |
| 34 | + * |
| 35 | + * <p>The function uses recursion to explore all possible subsets of items. |
| 36 | + * For each item, it has two choices: either include it in the knapsack |
| 37 | + * (if it fits) or exclude it. It returns the maximum value obtainable |
| 38 | + * through these two choices.</p> |
| 39 | + * |
| 40 | + * <p><b>Base Cases:</b> |
| 41 | + * <ul> |
| 42 | + * <li>If no items are left (n == 0), the maximum value is 0.</li> |
| 43 | + * <li>If the knapsack's remaining capacity is 0 (w == 0), no more items can |
| 44 | + * be included, and the value is 0.</li> |
| 45 | + * </ul></p> |
| 46 | + * |
| 47 | + * <p><b>Recursive Steps:</b> |
| 48 | + * <ul> |
| 49 | + * <li>If the weight of the n-th item exceeds the current capacity, it is |
| 50 | + * excluded from the solution, and the function proceeds with the remaining |
| 51 | + * items.</li> |
| 52 | + * <li>Otherwise, the function considers two possibilities: include the n-th |
| 53 | + * item or exclude it, and returns the maximum value of these two scenarios.</li> |
| 54 | + * </ul></p> |
| 55 | + */ |
11 | 56 | static int knapSack(int w, int[] wt, int[] val, int n) {
|
12 |
| - // Base Case |
13 | 57 | if (n == 0 || w == 0) {
|
14 | 58 | return 0;
|
15 | 59 | }
|
16 | 60 |
|
17 |
| - // If weight of the nth item is |
18 |
| - // more than Knapsack capacity W, |
19 |
| - // then this item cannot be included |
20 |
| - // in the optimal solution |
21 | 61 | if (wt[n - 1] > w) {
|
22 | 62 | return knapSack(w, wt, val, n - 1);
|
23 |
| - } // Return the maximum of two cases: |
24 |
| - // (1) nth item included |
25 |
| - // (2) not included |
26 |
| - else { |
27 |
| - return Math.max(val[n - 1] + knapSack(w - wt[n - 1], wt, val, n - 1), knapSack(w, wt, val, n - 1)); |
| 63 | + } else { |
| 64 | + return Math.max(knapSack(w, wt, val, n - 1), val[n - 1] + knapSack(w - wt[n - 1], wt, val, n - 1)); |
28 | 65 | }
|
29 | 66 | }
|
30 |
| - |
31 |
| - // Driver code |
32 |
| - public static void main(String[] args) { |
33 |
| - int[] val = new int[] {60, 100, 120}; |
34 |
| - int[] wt = new int[] {10, 20, 30}; |
35 |
| - int w = 50; |
36 |
| - int n = val.length; |
37 |
| - System.out.println(knapSack(w, wt, val, n)); |
38 |
| - } |
39 | 67 | }
|
0 commit comments