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).
#open the file in read mode
f = open("data.txt", "r")
#read the first line which has headings
line = f.readline()
#create empty lists for weights and values
weights = []
values = []
#it is used to store the total number of items
length = 0
#read the first line
line = f.readline()
while(line):
print(line)
temp = line.split(",")
#for each line split the data and add the values of
weights and values in the corresponding lists
weights.append(int(temp[1]))
values.append(int(temp[2]))
#increment length
length += 1
#read the next line
line = f.readline()
print(weights)
print(values)
print(length)
#knapsack program using bottom up dynamic programming
approach
def knapsack_bottonup(weight:int, weights:list, values:list,
length:int):
#create a matrix with number of rows as the number of
items(length) given and the number of columns as the weight
given
dp = [[0 for i in range(weight + 1)] for j in
range(length + 1)]
for item in range(length + 1):
for w in range(weight + 1):
#for each item
if item is 0 or weight is 0 the element in dp will be 0
if(item == 0 or
w == 0):
dp[item][w] = 0
#for each item
if weight of item is less than the given weight take the maximum
with including the weight and without including the weight
elif(weights[item - 1] <= w):
dp[item][w] = max(values[item - 1] + dp[item -
1][w - weights[item - 1]], dp[item - 1][w])
#if the weight
of item is less than the given weight then take the previous
element in dp matrix
else:
dp[item][w] = dp[item - 1][w]
#take the last element from dp which contains the
optimal value
return dp[length][weight]
print(knapsack_bottonup(6, weights, values, length))
time complexity and space complexity:
Time Complexity:
consider the number of items as n
let the weight given be W
Consider the program
The outer loop runs till the item value reaches n (which runs for n
+ 1 times 0 to n)
The inner loop runs till the weight values reaches W(which runs for
W + 1 times 0 to W)
so the time complexity will be O((n + 1)(W + 1)) which gives
O(nW)
Space Complexity:
To use the property of memoization in dynamic programming
A matrix is used to store the values of problems and using it
whenever same problem is repeated
so the space allocated for the matrix will be the extra space
consider the matrix dimensions
which contains the number of rows as given Weight(W + 1) and number
of columns as number of item(n + 1)
so the space complexity will be O((W + 1)(n + 1)) gives O(nW)
Time needed to get optimal solution from the table
To find the total value of the items in the optimal subset it will
take o(1) (just access the element from table)
To find the subset which gives that value it will take O(n) since
the table has to be traversed using the values
If you have any doubts please comment and please don't dislike.
a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should...
Design a dynamic programming algorithm for the version of the knapsack problem in which there are unlimited quantities of copies for each of the n item kinds given. Indicate the time efficiency of the algorithm
The Knapsack Problem in Python Not using the exhaustive search method or the Dynamic Programming Method, find another method that accomplishes the task of the knapsack problem in python. PLEASE DON'T USE THE EXHAUSTIVE SEARCH METHOD OR THE DYNAMIC PROGRAMMING METHOD, I DON'T NEED THOSE. THANK YOU.
Steps to develop a dynamic programming algorithm: a) Establish a recursive property that gives the solution to an instance of the problem; b) Compute the value of an optimal solution in a bottom-up fashion by solving smaller instances first. Select one: True False
Consider the following more general version of the Knapsack problem. There are p groups of objects O1, O2, . . . , Op and a knapsack capacity W. Each object x has a weight wx and a value vx. Our goal is to select a subset of objects such that: • the total weights of selected objects is at most W, • at most one object is selected from any group, and • the total value of the selected objects...
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
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)...
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...
Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...
Give pseudocode that performs the traceback to construct an LCS from a filled dynamic programming table without using the “arrows”, in O(n + m) time. 2. Suppose we are given a “chain” of n nodes as shown below. Each node i is “neighbors” with the node to its left and the node to its right (if they exist). An independent set of these nodes is a subset of the nodes such that no two of the chosen nodes are neighbors....
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...