Question

You are given a knapsack that can carry a maximum weight of 60. There are 4...

You are given a knapsack that can carry a maximum weight of 60. There are 4 items with weights {20, 30, 40, and 70} and values {70, 80, 90, and 200}. Apply the brute force algorithm to find the maximum value of the items you can carry using the knapsack?

Homework Answers

Answer #1

The maximum value you can get is 160. This can be achieved by choosing the items 1 and 3 that have a total weight of 60.


#include <bits/stdc++.h> 
using namespace std; 
int knapSack(int W, int wt[], int val[], int n) 
{ 

        if (n == 0 || W == 0) 
                return 0; 
        if (wt[n] > W) 
                return knapSack(W, wt, val, n - 1); 
        else
        if ( val[n] + knapSack(W - wt[n], wt, val, n - 1) > knapSack(W, wt, val, n - 1))
            return val[n] + knapSack(W - wt[n], wt, val, n - 1)
        else
            return knapSack(W, wt, val, n - 1)
                
} 

int main() 
{ 
        int val[] = { 70, 80, 90 ,200 }; 
        int wt[] = { 20, 30 ,40 ,70 }; 
        int W = 60; 
        int n = sizeof(val) / sizeof(val[0]); 
        cout << knapSack(W, wt, val, n); 
        return 0; 
} 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity of knapsack=25. Find the maximum value output assuming...
Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity of knapsack=25. Find the maximum value output assuming items to be divisible a. 60 b. 100 c. 80 d. 70
the table is for 0-1 knapsack problem given for the following items, each labeled with weight...
the table is for 0-1 knapsack problem given for the following items, each labeled with weight and value. Assume the total weight limit W is 8 lbs. Item 1 2 3 4 Value ($) 8 40 30 54 Weight (lb) 1 2 3 6 Solve the fractional knapsack problem using the input?
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1    14 20 2...
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1    14 20 2     6 16 3   10 8 4     5 10 5   4 12 Allowed weight = 24 Kg Problem 2: Item Weight Value 1 6 30 2 8 40 3 15 45 4 22 88 5 25 80 Allowed weight = 60 Kg Problem 3: Item Weight Value 1     6 30 2     8 40 3    15 45 4    22 88...
Using the given data set to answer the following questions80 60 100 40 40 50 80...
Using the given data set to answer the following questions80 60 100 40 40 50 80 90 100 60 10 80 45 60 30 80 30 90 100 60 50 75 a. find Mean, Median, Mode, Standard Deviation, and Variance: b. find First quartile and Third Quartile c. Find the number which represents the 33th percentile. d. find the percentile of 45 e. find the range and midrange f. construct a box-plot g. Construct a frequency distribution with 5 classes....
1) You measure 25 textbook' weights, and find they have a mean weight of 60 ounces....
1) You measure 25 textbook' weights, and find they have a mean weight of 60 ounces. Assume the population standard deviation is 9.2 ounces. Based on this, what is the maximal margin of error associated with a 90% confidence interval for the true population mean backpack weight. Give your answer as a decimal, to two places a) You measure 46 textbooks' weights, and find they have a mean weight of 48 ounces. Assume the population standard deviation is 7.8 ounces....
You are given the following data for your firm, which sells the only commercially-available Wifi-enabled cheese...
You are given the following data for your firm, which sells the only commercially-available Wifi-enabled cheese grater. Q P TC 0 $100 $1,000 10 $95 $1,211 20 $90 $1,411 30 $85 $1,609 40 $80 $1,814 50 $75 $2,035 60 $70 $2,281 70 $65 $2,561 80 $60 $2,884 90 $55 $3,259 100 $50 $3,695 QUESTION: The profit-maximizing quantity occurs at _______ and the profit-maximizing price occurs at _________. (Since MC is in terms of Q2, solving with calculus and algebra can...
Write a function reshaped_by_col(nums, col) which takes a Python list nums, and an integer value for...
Write a function reshaped_by_col(nums, col) which takes a Python list nums, and an integer value for columns that reshapes the 1D nums into col number of columns. Finally, returns the reshaped numpy array. If the shape cannot be reshaped, return None (you can do this without catching exceptions). For example: Test Result nums = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] col = 2 print(reshaped_by_col(nums, col)) [[ 10 20] [ 30 40] [ 50 60] [ 70...
Quantity Total Cost 0 $62 10 $90 20 $110 30 $126 40 $144 50 $166 60...
Quantity Total Cost 0 $62 10 $90 20 $110 30 $126 40 $144 50 $166 60 $192 70 $224 80 $264 90 $324 100 $404 The table above shows a strawberry farm's short-run production and cost schedule. Suppose that the prevailing market price is $6 per pack of strawberries. (a) How much is the fixed cost? (b) Find the profit-maximizing quantity and obtain the resulting maximum profit.
Pulse Frequancy Minimum Maximum Class width PULSE 60 74 86 54 90 80 66 68 68...
Pulse Frequancy Minimum Maximum Class width PULSE 60 74 86 54 90 80 66 68 68 56 80 62 74 60 52 60 66 64 64 46 68 58 68 70 56 66 78 68 62 70 72 74 64 50 70 58 60 88 84 76 46 68 58 68 70 56 66 78 68 62 constructing a frequency table 1 decide on the number of classes(should be between 5 and 20 2 calculate round up (highest value)-(lowest value)/number...
Scuba divers have maximum dive times they cannot exceed when going to different depths. The data...
Scuba divers have maximum dive times they cannot exceed when going to different depths. The data in Table show different depth with the maximum dive times in minutes. X (depth in feet) Y (maximum dive time in minutes) 50 60 70 80 90 100 80 55 45 35 25 22 1. Find the linear correlation coefficient? 2. Is the linear correlation(positive or negative)? 3. Find the equation of the least squares regression line. 4. Using the equation in part(c) what...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT