The Knapsack function (thief) returns an array of weights and an array of items to take. Previous check was to determine N == 0. Since N == 0, it cannot be > 0 at the same time.
The Knapsack function (thief) returns an array of weights and an array of items to take. Previous check was to determine N == 0. Since N == 0, it cannot be > 0 at the same time.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
The Knapsack function (thief) returns an array of weights and an array of items to take. Previous check was to determine N == 0. Since N == 0, it cannot be > 0 at the same time.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
: * v_arr: The array of values : * index_array: The array of selected items to take : * W: Total weight that the thief can carry : * N: Total number of items : */ : int knapsack(int* w_arr, int* v_arr, int* index_array,int W,int N) : { : int temp1=0,temp2=0; : : if ( N == 0 || W == 0) : { : //first case : if(N > 0) : index_array[N-1] = 0; // item not taken Above check impossible. Previous check was to determine N == 0. Since N == 0, it cannot be > 0 at the same time. : return 0; : } : : else if (w_arr[N-1] > W) : { : //second case : index_array[N-1] = 0; // item not taken : return knapsack(w_arr,v_arr,index_array,W, N-1); : } : else : { : //third and final case : temp1 = knapsack(w_arr,v_arr,index_array,W, N-1); : temp2 = knapsack(w_arr,v_arr,index_array,W-w_arr[N-1],N-1) + v_a rr[N-1]; : : index_array[N-1] = N; // item taken : : if(temp1>temp2) : { : index_array[N-1] = 0; // item release : return temp1; : } : : else : { : index_array[N-1] = N; // item taken : return temp2; : } : } : }// end knapsack