So I thought
I would like to make a variation of the Knapsack problem.
Imagine the original problem with items with different weights / values.
My version, along with normal weights / values, contains the value "group".
eg. Item1 [5kg, $ 600, electronic] Item2 [1kg, $ 50, food]
Now, having a set of such elements, as if I encoded a problem with a backpack, to make sure that no more than 1 element is selected from each "group".
Notes:
- You do not need to select an item from this group.
- Each group has several elements.
- You still minimize weight, maximize value
- The number of groups is predetermined along with their values.
I am just writing a draft code at this point, and I decided to use a dynamic approach. I understand the idea of ββa dynamic solution for a common backpack problem, how do I change this solution to include these βgroupsβ?
KnapSackVariation(v,w,g,n,W) { for (w = 0 to W) V[0,w] = 0; for(i = 1 to n) for(w = 0 to W) if(w[i] <= w) V[i,w] = max{V[i-1, w], v[i] + V[i-1, ww[i]]}; else V[i,w] = V[i-1, w]; return V[n,W]; }
This is what I still have, I need to add it so that it removes all the relevant elements from the group in which it is located every time it solves this.