-1
int knapsack(i, k){

    if(i=N){

        val= 0; //end of recrusive

        return val;
    }

    if (w[i]>k) // no space anymore

        val= knapsack(i+1, k); 

    else {

        a = knapsack(i+1, k) // i don't take with

        b = knapsack(i+1, k-w[i]) + v[i]; //i take with

        val= max(a,b); 

    } 

    return val;
} 

My question is, what is N in this case ? In my Program for variables I have weight, value, maxweight and a LinkedList. Anyone can help me out ? Thanks

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131

1 Answers1

0

N is the number of elements in the input array/list. The first recursive call is knapsack(0,k), and each recursive call increments i , until it reaches N and the recursion ends.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • a and b ? what is that ? – Jeryoz Dimitar Jul 04 '17 at 03:16
  • @JeryozDimitar `a` is the result of the `knapsack` algorithm that doesn't include the i'th element and `b` is the result of the `knapsack` algorithm that includes the i'th element. The max of the two gives you the total result. – Eran Jul 04 '17 at 05:16