Write an algorithm that uses dynamic program to find a solution to the knapsack problem.
The input is the capacity of the knapsack then each row of the table of values and weights.
The output is the resulting maximum value of the items that can be put in the knapsack.
Code submitted in C++
Sample Input 1:
11 1 1 6 2 18 5 22 6 28 7
Sample Output 1:
40
Sample Input 2:
8 15 1 10 5 9 3 5 4
Sample Output 2:
29
Sample Input 3:
5 1 1 2 1 3 1 4 1 5 1
Sample Output 3:
15
Sample Input 4:
5 1 1 2 1 3 1 4 5
Sample Output 4:
6
Sample Input 5:
5 1 1 2 1 3 1 10 5
Sample Output 5:
10
Sample Input 6:
5 10 5 1 1 2 1 3 1
Sample Output 6:
10
CODE
#include <iostream>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int n;
cin >> n;
int W;
cin >> W;
int val[n];
int wt[n];
for (int i=0; i<n; i++) {
cin >> val[i];
cin >> wt[i];
}
cout << knapSack(W, wt, val, n);
return 0;
}
Write an algorithm that uses dynamic program to find a solution to the knapsack problem. The...
1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5
Haloo , i have java program , Java Program , dynamic program Given a knapsack with capacity B∈N and -n- objects with profits p0, ..., p n-1 and weights w0, ..., wn-1. It is also necessary to find a subset I ⊆ {0, ..., n-1} such that the profit of the selected objects is maximized without exceeding the capacity. However, we have another limitation: the number of objects must not exceed a given k ∈ N Example: For the items...
a) Implement the bottom-up dynamic programming algorithm for the
knapsack problem in python. The
program should read inputs from a file called “data.txt”, and the
output will be written to screen,
indicating the optimal subset(s).
b) For the bottom-up dynamic programming algorithm, prove that its
time efficiency is in
Θ(nW), its space efficiency is in Θ(nW) and the time needed to find
the composition of an
optimal subset from a filled dynamic programming table is in
O(n).
Consider the following...
3. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (20 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? Item 1 weighs 2 pounds and is worth $9.00 Item 2 weighs 3 pounds and is worth $12.00 Item 3 weighs 5 pounds and is worth $14.00 Item 4...
Apply the top-down (i.e., memory function) dynamic programming
algorithm to the following instance of the knapsack problem. Input
your results in the table shown below. For empty cells, input a
single minus sign (-) into the cell.
Warning: When filling in the table below with your
answers, be sure to type the number in each cell, with no decimal
points or leading zeros or spaces. For example, if a cell should
contain a value of 0, just type "0" and...
JAVA question. Pages 305 and 306 of your text discusses the Knapsack Problem. Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs. Knapsack code traditionally ends when you find a total weight of items that equals the capacity of the knapsack. In this case, it is possible...
Algorithm and computing system(Python) What is dynamic programming? Why does it usually work faster? Using the dynamic programming solution for the knapsack problem, compute a solution to this knapsack problem: Weight value 2 16 3 19 4 23 5 28 total number of items = 4 capacity of the knapsack = 7 Suppose that the similarity between an object O and 6 other objects in the database A,B,C,D,E and F are as follows: sim(A,O) = 0.1 sim(B,O) = 0.3 sim(C,O)...
Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume that the coins with which you make change are quarters, dimes, nickels and pennies. Thus you are going to set n = 4 in your program. The amount k for which you have to make change will be provided by the user and your program will return the minimum number of coins needed and also the break-up of the change in terms of the...
ALGORITHM
Given the following Knapsack problem instance and its DP solution: 1 2 3 4 5 weight value To 10 10 10 10 item1 1 10 item2 | 2 17 item3 11 11 21 21 28 28 item4 15 0 11 121 121 128 36 According to the solution table, the maximum item value that we can achieve 36. By reconstructing the solution, we know that the following items {1,3,4} are included in the solution. Carefully, fill in the following...
solution is required in pseudo code please.
2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity In this question, we will consider two different ways to represent a solution to the...