I am trying this code for solving 0/1 knapsack problem using Brute-force recursive solution, but it keeps running with no output at all when I make the size of the problem(profit and weight arrays) 100. if any one can tell me why? and how to solve it.
Please if any one can tell me when to find trusted pseudocodes and codes for 0/1 knapsack problem.
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
//Return the maximum value that can be put in a knapsack of capacity W
int knapsackRecursive(int profits[], int profitsLength, int weights[], int capacity, int currentIndex) {
// Base Case
if (capacity <= 0 || currentIndex >= profitsLength || currentIndex < 0)
return 0;
//If weight of the nth item is more than knapsack capacity W, then
// this item cannot be included in the optimal solgurion
int profit1 = 0;
if (weights[currentIndex] <= capacity)
profit1 = profits[currentIndex] + knapsackRecursive(profits, profitsLength, weights, capacity - weights[currentIndex], currentIndex + 1);
int profit2 = knapsackRecursive(profits, profitsLength, weights, capacity, currentIndex + 1);
//Return the maximum of two cases:
//(1) nth item included
//(2) not included
return max(profit1, profit2);
}
int knapSack(int profits[], int profitsLength, int weights[], int capacity) {
return knapsackRecursive(profits, profitsLength, weights, capacity, 0);
}
int main() {
int profits[100];
int weights[100];
int capacity = 300;
srand(time(0));
clock_t startTime;
clock_t endTime;
clock_t timeTaken = 0;
for (int i = 0; i < 20; i++) { //repeat the knapSack 20 times
for (int j = 0; j < 100; j++) {
profits[j] = 1 + (rand() % 100);
weights[j] = 1 + (rand() % 100);
}
startTime = clock();
knapSack(profits, 100, weights, capacity);
endTime = clock();
timeTaken = timeTaken + (endTime - startTime); //compute the total cpu time
}
cout << "The avearage of the time taken is" << ((float)timeTaken / CLOCKS_PER_SEC) / 20 << " seconds";
return 0;
}