# 0/1 Knapsack problem with overlapping items

Here's a doozy:

Given a knapsack with a capacity W, and n overlapping items (definition of overlapping to follow), which items should we take to maximize the value of the knapsack?

In this problem, you can think of an "item" as a bag of coins with the following properties:

• There are many, many different types of coins (1M+)
• Every coin is worth the same amount
• Each bag has at most one of any type of coin

For example, bag 1 might have two coins, one of type A and another of type B. And bag 2 might have two coins, one type B and one type C.

We can only take one of each type of coin. So picking bag 1 and bag 2 would mean that we have 3 total coins (one A, one B, and one C).

How can we figure out which bags to take to maximize the number of coins you can take?

The subproblems aren't independent, so I don't think we can use dynamic programming.

For extra credit:

How can we get within a certain threshold, say 10%, of the capacity W as fast as possible?

• just to clarify: so you can't take both bags A and B, is it? And what are the weights of the different types of coins? – Mathguy Jun 4 '16 at 0:33
• What role does the capacity play? – harold Jun 4 '16 at 1:12
• @Mathguy Great questions. I clarified the question to say that while you can take bag 1 and bag 2, if you do, you have to get rid of any duplicate coins. The weights of every type of coin is the same, so we can act as though they all have a weight of 1. – yndolok Jun 4 '16 at 4:52
• @harold Thanks for the question. The capacity of the knapsack is essentially the number of coins you can take. – yndolok Jun 4 '16 at 4:53
• Do we "get rid of any duplicate coins" before or after calculating the total weight? ​ ​ – user12859 Jun 4 '16 at 8:27

Calling this a variant of Knapsack is a bit confusing, since it's more like maximum-weight independent set. Bags are nodes, bags that share a type of coin have an edge between them, and the number of coins in each bag is the weight of the node. Going the other way is annoying, translating unweighted MIS into this problem is easy though.

Anyway here's a direct ILP formulation of the coins/bags thing:

maximize sum of take_bag[i] * size[i]
s.t.
∀c: sum[i | c ∈ bag[i]] of take_bag[i] ≤ 1
take_bag[i] ∈ { 0, 1 }


You could use the linear relaxation of that to do branch & bound, which you can stop early when you're happy enough with the gap between the best found solution and the objective value of the linear relaxation. Or check the literature for MWIS solving. There are many approximation algorithms as well.

Taking into account the capacity constraint, the ILP model becomes

maximize sum of take_bag[i] * size[i]
s.t.
∀c: sum[i | c ∈ bag[i]] of take_bag[i] ≤ 1
sum of take_bag[i] * size[i] ≤ W
take_bag[i] ∈ { 0, 1 }

• Please correct me if I'm wrong - I don't think this algorithm takes the maximum total weight (capacity) into account. That's why I thought this was a variant of the knapsack problem - we can't take more than W coins. – yndolok Jun 4 '16 at 21:55
• @yndolok so it is a constraint after all. No problem for ILP, but that makes this "not MWIS" – harold Jun 4 '16 at 21:57
• Yes, it is a constraint. Sorry for the confusion. How could we modify the ILP formulation you gave to take the capacity into account? – yndolok Jun 4 '16 at 22:01
• Is the ILP solution essentially the equivalent of trying every possible combination of bags until we find a combination of bags with a capacity W? And if we've tried every combination, and none have a capacity of W, we just return the largest combo of bags with a sum < W? – yndolok Jun 5 '16 at 2:15
• @yndolok the result is equivalent (not necessarily the same due to the order of solutions with the same objective) but IMO that does not make it equivalent as a solution - the whole point of having that model is so it doesn't have to try every combination because it can prune large parts of the search space. – harold Jun 5 '16 at 9:54