Question

In: Computer Science

Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items,...

Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items, their values and weights. The maximum weight capacity of the knapsack is 113. What could be the maximum value of the knapsack and the most valuable set of items that fit in the knapsack?

Item Weight Value
1 32 727
2 40 763
3 44 60
4 20 606
5 1 45
6 29 370
7 3 414
8 13 880
9 6 133
10 39 820

Solutions

Expert Solution

The language used to solve this problem is Java. Algorithm implemented is dynamic programming.

Code:

public class KnapsackProblem {

    /**
     * Knapsack problem can be solved using dynamic programming.
     * Here, we have used bottom up approach.
     */
    public static int getMaxValueOfKnapSack(int maxWeight, int[] weightOfItems, int[] valueOfItems, int n) {

        int[][] knapsack = new int[n + 1][maxWeight + 1];

        // Fill the knapsack array
        for (int i = 0; i <= n; i++) {
            for (int w = 0; w <= maxWeight; w++) {
                if (i == 0 || w == 0) {
                    knapsack[i][w] = 0;
                } else if (weightOfItems[i - 1] <= w) {
                    knapsack[i][w] = Math.max(valueOfItems[i - 1] + knapsack[i - 1][w - weightOfItems[i - 1]], knapsack[i - 1][w]);
                } else {
                    knapsack[i][w] = knapsack[i - 1][w];
                }
            }
        }

        return knapsack[n][maxWeight];
    }

    public static void main(String[] args) {
        int[] weightOfItems = new int[]{32, 40, 44, 20, 1, 29, 3, 13, 6, 39};
        int[] valueOfItems = new int[]{727, 763, 60, 606, 45, 370, 414, 880, 133, 820};
        int maxWeight = 113;
        int n = valueOfItems.length;
        System.out.println("Maximum value of Knapsack: " + getMaxValueOfKnapSack(maxWeight, weightOfItems, valueOfItems, n));
    }
}

Code Snippet:

Sample Output for given input:


Related Solutions

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of items, let call them 1,2,3,4,...,n. There are exactly c_i copies of item i, and each such copy has value v_i and weight w_i. As before, the knapsack capacity is W, and the other constraint is that you can only take at most c_i copies of item i ( since no more are available). Show how to compute the optimal value that can be achieved...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to compute the value of the best packing of the knapsack? For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to identify the items that are included in an optimal packing of the knapsack? For the Longest Common Subsequence algorithm, is it necessary to maintain the entire...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with only fractional inclusion (i.e. not entirely included or excluded) in the knapsack? (Let n denote the number of items for possible inclusion.) A) At least 0 items and at most n items B) At least 1 items and at most n items C) Exactly n items D) At least 0 items and at most n-1 items E) At least 1 items and at most...
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Consider the following items in the Knapsack Problem: Item                weight             value &nbsp
Consider the following items in the Knapsack Problem: Item                weight             value            Knapsack capacity W = 9. 1                      3                   $6 2 4 $12 3                      2                  $10 4                      5                  $20 Determine the maximum value in the knapsack if we allow repetitions; i.e., if there are an unlimited number of each item so that more than one such item can be chosen. Find the missing value in the following linear array. P(w) is the maximum profit obtainable for a knapsack of capacity w. w-->             0       1      2       3       4       5       6       7       8       9 P(w):    0 0 10 10     20     20     30    30 40 ?   P(9)...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define what the function is computing.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT