-1

I've been doing 0-1 Knapsack problem using recursion+memoization.

My Code:

def knapSack(W, wt, val, n):
    '''
    :param W: capacity of knapsack 
    :param wt: list containing weights
    :param val: list containing corresponding values
    :param n: size of lists
    :return: Integer
    '''
    t = [[-1 for x in range(W + 1)] for j in range(n + 1)]
    if n == 0 or W == 0: 
        return 0
    if t[n][W] != -1: 
        return t[n][W] 
    elif wt[n-1] <= W: 
        t[n][W] = max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-1))
        return t[n][W] 
    elif wt[n-1] > W: 
        t[n][W] = knapSack(wt, val, W, n-1) 
        return t[n][W]

Why am I getting a runtime error.

Runtime ErrorTraceback (most recent call last):
  File "/home/e8c2fc67721232cbee976a6adfc2c990.py", line 41, in <module>
    print(knapSack(W,wt,val,n))
  File "/home/e8c2fc67721232cbee976a6adfc2c990.py", line 12, in knapSack
    t[n][W] = knapSack(wt, val, W, n-1) 
  File "/home/e8c2fc67721232cbee976a6adfc2c990.py", line 5, in knapSack
    t = [[-1 for x in range(W + 1)] for j in range(n + 1)]
  File "/home/e8c2fc67721232cbee976a6adfc2c990.py", line 5, in <listcomp>
    t = [[-1 for x in range(W + 1)] for j in range(n + 1)]
TypeError: can only concatenate list (not "int") to list

This is the runtime error deatils I'm getting.

  • What about the line number, traceback, and filename for the error. Is that information available? I suspect the error is occurring where this function is being called for the first time (the outermost invocation of the recursion). I don't see any concatenation in this function. – fountainhead Nov 08 '20 at 11:05
  • I've edited my question. Please check – Arnav Luhadiya Nov 08 '20 at 11:08

2 Answers2

1

I suspect that there is a mismatch between the parameter-order of your function definition, and the argument-order in which the args are actually being passed to one of the many invocations.

Note that, in the traceback:

At line 41 it shows the arg-order as print(knapSack(W,wt,val,n))

At line line 12 it shows a different arg-order knapSack(wt, val, W, n-1)

For the condition elif wt[n-1] > W:, your recursive invocation of knapSack() is definitely passing the args in the wrong order.

fountainhead
  • 3,584
  • 1
  • 8
  • 17
0

I think there is an easy fix to this, if you would just put the loops bellow the first if.

if n == 0 or W == 0: 
        return 0
t = [[-1 for x in range(W + 1)] for j in range(n + 1)]
if t[n][W] != -1: 
        return t[n][W] 
elif wt[n-1] <= W: 
    t[n][W] = max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-1))
        return t[n][W] 
elif wt[n-1] > W: 
    t[n][W] = knapSack(wt, val, W, n-1) 
    return t[n][W]
Oliver Hnat
  • 797
  • 4
  • 19
  • Alright, could you please give me an example of ```W, wt, val``` and ```n``` that you are passing in? – Oliver Hnat Nov 08 '20 at 10:54
  • I'm not passing anything(All the work is being done by GFG). I'm solving this question on GeeksForGeeks and it states that: Complete the function knapSack() which takes maximum capacity W, weight array wt[], value array val[] and number of items n as a parameter and returns the maximum possible value you can get – Arnav Luhadiya Nov 08 '20 at 10:58