
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![Algorithm 5.7
The Backtracking Algorithm for the 0-1 Knapsack Problem
Problem: Let n items be given, where each item has a weight and a profit.
The weights and profits are positive integers. Furthermore, let a positive
integer W be given. Determine a set of items with maximum total profit,
under the constraint that the sum of their weights cannot exceed W.
Inputs: Positive integers n and W; arrays w and p, each indexed from 1
to n, and each containing positive integers sorted in nonincreasing order
according to the values of p[i]/w[i].
Outputs: an array bestset indexed from 1 to n, where the values of bestset[i]
is "yes" if the ith item is included in the optimal set and is "no" otherwise;
an integer maxprofit that is the maximum profit.
BACKTRACKING
void knapsack (index i,
1
int profit, int weight)
if (weight W && profit > maxprofit){\
}
maxprofit
numbest = i;
profit;
bestset include;
if (promising (i)){
include [i+1] = "yes";
This set is best
// so far.
// Set numbest to
// number of items
considered. Set
//bestset to this
// solution.
// Include w[i+1].
knapsack (i+1, profit + p[i+1], weight + w[i + 1]);
include [i+1]
=
"no";
knapsack (i+1, profit, weight);
// Do not include
// w[i + 1].
}
}
bool promising (index i)
{
index j, k;
int totweight;
float bound;
if (weight > W)
return false;
else{
j = i + 1;
bound profit;
totweight weight;
// Node is promising only
if we should expand to
its children. There must
be some capacity left for
// the children.
while (j <=n&& totweight + w[j] < = W){\
totweight totweight + w[j];
bound bound + p[j];
Grab as many items as
possible.
j++;
}
k = j;
if (k <=n)
bound = bound (Wtotweight) p[k]/w[k];
Use k for consistency
// with formula in text.
*
return bound > maxprofit;
// Grab fraction of kth
// item.
}
}](https://content.bartleby.com/qna-images/question/2d578ff0-205d-4ea0-ad2c-0dd7aa11c763/b79e2dee-f041-4ceb-a8c9-7cae88b117f7/0338mo_thumbnail.jpeg)
Transcribed Image Text:Algorithm 5.7
The Backtracking Algorithm for the 0-1 Knapsack Problem
Problem: Let n items be given, where each item has a weight and a profit.
The weights and profits are positive integers. Furthermore, let a positive
integer W be given. Determine a set of items with maximum total profit,
under the constraint that the sum of their weights cannot exceed W.
Inputs: Positive integers n and W; arrays w and p, each indexed from 1
to n, and each containing positive integers sorted in nonincreasing order
according to the values of p[i]/w[i].
Outputs: an array bestset indexed from 1 to n, where the values of bestset[i]
is "yes" if the ith item is included in the optimal set and is "no" otherwise;
an integer maxprofit that is the maximum profit.
BACKTRACKING
void knapsack (index i,
1
int profit, int weight)
if (weight W && profit > maxprofit){\
}
maxprofit
numbest = i;
profit;
bestset include;
if (promising (i)){
include [i+1] = "yes";
This set is best
// so far.
// Set numbest to
// number of items
considered. Set
//bestset to this
// solution.
// Include w[i+1].
knapsack (i+1, profit + p[i+1], weight + w[i + 1]);
include [i+1]
=
"no";
knapsack (i+1, profit, weight);
// Do not include
// w[i + 1].
}
}
bool promising (index i)
{
index j, k;
int totweight;
float bound;
if (weight > W)
return false;
else{
j = i + 1;
bound profit;
totweight weight;
// Node is promising only
if we should expand to
its children. There must
be some capacity left for
// the children.
while (j <=n&& totweight + w[j] < = W){\
totweight totweight + w[j];
bound bound + p[j];
Grab as many items as
possible.
j++;
}
k = j;
if (k <=n)
bound = bound (Wtotweight) p[k]/w[k];
Use k for consistency
// with formula in text.
*
return bound > maxprofit;
// Grab fraction of kth
// item.
}
}

Transcribed Image Text:33. Use the Backtracking algorithm for the 0-1 Knapsack problem (Algorithm
5.7) to maximize the profit for the following problem instance. Show the
actions step by step.
Pi
i
Pi
Wi
Wi
1
$20
2
10
2
$30
3
$35
7
5
W = 9
4
$12
3
4
5
$3
1
3
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 5 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Find the best solution for the 0-1 knapsack problem using backtracking algorithm. We have 5 items with price and weight: $50 $30 $40 $100 $20 10 1. 2. 3. 2 4. 10 5. 5 Limitation on the weight is: W = 21arrow_forwardDesign a dynamic programming algorithm for the following problem. Find the maximum total sale price that can be obtained by cutting a rod of $n$ units long into integer-length pieces if the sale price of a piece $i$ units long is $p_i$ for $i = 1, 2, . . . , n$. What are the time and space efficiencies of your algorithm?arrow_forwardThe following loop has multiple types of dependences. Find all the true dependences, output depende- nces and anti dependences and eliminate the output dependences by renaming. For( i = 1; i<= 100; i = i+1) { Y[i] = X[i]/C; %3D X[i] = X[i] + C; %3D Z[i] = Y[i] + C; %3D Y[i] = C-Y[i]; %3Darrow_forward
- X-Kingdom has trapped n number of soldiers of their opponent. They want to execute them. They created a strategy so that the prisoners will kill each other and at the end one prisoner will be alive and eventually released. As part of the process, they assigned each trapped soldier a sequence number starting from 1 and ending at n. If n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So, the person at position 3 survives. If n = 7 and k = 3, then the safe position is 4. The people at positions 3, 6, 2, 7, 5, 1 are killed in order, and the person at position 4 survives. Input:n and k Output:Print the list of prisoners in reverse order from n to 1Then reverse the list to print it in correct order from 1 to nDisplay the position number who will survive. You must have to use circular doubly linked list for your solution. You…arrow_forwardIn a hypothetical study of population dynamics, scientists have been tracking the number of rabbits and foxes on an island. The number of rabbits and foxes are determined once a year using high resolution infrared cameras and advanced computer vision methods.Each year, the number of rabbits and foxes are found to change by the following equations: $$ {\Delta}R = round( kr*R - krf*R*F ) $$ $$ {\Delta}F = round( -kf*F + kfr*R*F ) $$ where $ {\Delta}r $ and $ {\Delta} f $ are the changes in number of rabbits and foxes by the end of that year; and R and F are the population sizes at the end of the previous year. kr, krf, kf, kfr are coefficients that depend on the species of rabbits and foxes.With these dynamics, the scientists realize that one or both species can become extinct on the island. At the end of each year, if there are fewer than 2 animals of a kind, the scientists transfer rabbits and/or foxes to make sure there are at least 2 of each kind.Write a function…arrow_forwardAn object leaves point P1 with coordinates (−1,0) and travels with velocity v1=1 in a straight line to point P2 with coordinates (0,h) . It then travels with velocity v2=2 in a straight line from P2 to point P3 with coordinates (x , y ). Given x and y values there will be some optimal h value that minimizes the total travel time. Write a program in Scilab containing a function in the form function u = h(x,y) [your code goes here] endfunction u = h(1,2); disp(u); that calculates this optimal h value (to an accuracy of about 10^−6 ) given x and y values as arguments. Your function must work for any positive values of x and y.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education