I have a list of weights where each index represents the weight of an item.
Weights = [0.3, 0.7, 0.1, 0.3, 0.1, ...]
Each item has a list of of collision items, hence if you pick item 0 you can't pick item 1.
Item_0 = [0,1,3,7]
Item_1 = [1,5,6,8]
All the items have the same number of collisions.
The goal is to pick N items keeping in mind the collisions and maximize the weight of the items picked.
What's the easiest and most pythonic way to do this?
I initially thought a greedy approach will work (sort the weights in descending order) but it doesn't and the only other solution I can come up with is finding all possible combinations of N items (without collisions) and calculating the total weights.
Greedy Algorithm (Gives incorrect result):
def pickTop_K(weights, collision_dict):
ret = []
while len(ret) < k:
index = np.argmax(probs)
ret.append(index)
collisions = collision_dict[index]
weights[collisions] = 0
if np.max(weights) == 0:
break
return ret
Weights? So item 0 -> 0.3, item 1 -> 0.7 etc. - RoadRunner