I have to write a brute-force implementation of the knapsack problem. Here's the pseudocode:
computeMaxProfit(weight_capacity)
max_profit = 0
S = {} // Each element of S is a weight-profit pair.
while true
if the sum of the weights in S <= weight_capacity
if the sum of the profits in S > max_profit
update max_profit
if S contains all items // Then there is no next subset to generate
return max
generate the next subset S
While the algorithm is fairly easy to implement, I haven't the slightest idea how to generate the power set of S, and to feed the subsets of the power set into each iteration of the while loop.
My current implementation uses a list of pairs to hold an item's weight and profit:
list< pair<int, int> > weight_profit_pair;
And I want to generate the power set of this list for my computeMaxProfit function. Is there an algorithm out there to generate subsets of a list? Is a list the right container to use?