Knapsack with mutually exclusive items - algorithm

While standard knapsack problem can be solved by dynamic programming, I am trying to twist the problem a bit to clear my concept, however I found it maybe harder than I thought.
Original knapsack problem is that given a knapsack with size W, and a list of items which weight w[i] and has a value v[i], find the subset of items which can fit in the knapsack with highest total value.
To my understanding, this can be done by O(Wn) with dynamic programming, where n is the number of items.
Now if I try to add m constrains, each of them is a pair of items which can only be picked mutual exclusively (i.e. if there exist a constrain of item A and item B, then I can only take either one of them but not both)
Under such constrains, can this problem still be solved by dynamic programming in O(Wn)?

Assumption: Each element is included in atmost one constraint.
For the usual Knapsack problem, the optimal substructure that the problem exhibits is as follows:
For each item there can be two cases:
1. The item is included in the solution
2. The item not included in the solution.
Hence, the optimal solution for n items is given by max of following two values.
1. Maximum value obtained by n-1 items and W weight.
2. v_n + maximum value obtained by n-1 items and W-w_n weight.
Now if we add the constraint that either of nth or (n-1)th item can exist in the solution, then the optimal solution for n items is given by max of following three values.
1. Maximum value obtained by n-2 items and W weight.
2. v_n + maximum value obtained by n-2 items and W-w_n weight.
3. v_(n-1) + maximum value obtained by n-2 items and W-w_(n-1) weight.
So we treat each pair of elements in the constraint as a single element and execute the dynamic programming algorithm in O(Wn) time.

Related

Maximize sum of integer entries under a weight constraint

I have an array of integers. Lets denote it by A. There is another array W containing weights associated with each entry of A.
In other words, associated with each entry A[i] is a certain weight entry W[i]. (Note that the weights are out of a limited set S_w = {w1,w2,w3,w4} so only few possible values)
The problem statement is as follows: Pick a random number of entries out of A such that when summed together, give you the highest value (SUM_A) under the constraint that the sum of their respective weights (SUM_W) doesn't exceed a threshold, W_threshold.
One possibility is brute force: Compute all permutations of A and for each permutation, select first n entries such that their sum weight SUM_W doesn't exceed W_threshold. Finally, the permutation that gives the maximum SUM_A shall be selected. But the complexity of this scheme is very high due to permutation computation step since the length of A is not constrained.
One other (sub-optimal) possibility is to sort A in descending order and then select first n entries such that their SUM_W doesn't exceed W_threshold. This has lower complexity but the result would be suboptimal.
Could someone give me tips if their already exists an algorithm to resolve the above stated problem? Or if anyone has ideas better than the ones I described above. Many thanks

Knapsack algorithm with special objects

I need to create algorithm for extended Knapsack problem, where we have special items with normal items and I can pack only several special items.
So, we have N items and we know weight/value/isSpecial of each one. Also we have defined S, which is maximum number of special items.
Any idea?
The problem can be modeled as a non-geometric knapsack problem where each item has two weights (namely the actual weight and a second weight which is 1 if and only if it is special); the first weight capacity is the knapsack capacity and the second weight capacity is the desired maximum number of special items. According to this Wikipedia article, the problem is known to be NP-complete and does not admit an FPTAS unless P=NP, but admits an FPTAS. Apparently the two-dimensional version also admits a dynamic programming algorithm similar to the one-dimensional version. The semantics of the state space would be as follows, where each item has two weights w1[i] and w2[i] and profit p[i] and where C1 and C2 are the respective capacities.
P[i,j,k] := maximum profit attainable for items in {1,...,i} with weight
in the first component at most j and weight in the second
component at most k
for any i in {1,...,N}
j in {1,...,C1}
k in {1,...,C2}
The recurrence relation would be as follows.
P[i,j,k] = max{ P[i-1,j-w1[i],k-w2[k]] + p[i], // item i occurs
P[i-1,j,k] // item i does not occur
}
In an actual implementation, some index checking would be necessary to determine whether the first case can actuallty occur.

Algorithm: Coin Change - Counting the number of ways one can give change

I am trying to understand the DP behind the coin change problem, where one is supposed to count the number of ways you can give change for a denomination given a set of coins. Each coin is present infinite number of times.
The algorithm is taken from this geeks4geeks page. The algorithms is the following (where N stands for denomination and dp is an array of size N+1):
dp[0] = 1
for each coin c:
for i from c to N:
if i >= c:
dp[i] += dp[i-c]
I am not able to understand how is the DP working here and what are the subproblems.
Edit: I checked other related questions but none mentions the algorithm stated above. A 2-D DP solution is discussed in previous questions.
You can consider this approach as solving C subproblems where C is the number of coins.
Subproblem c consists of "Which values can be made from coins up to and including coin c, but not including any coins of greater value."
The base subproblem then becomes "Which values can be made from no coins", to which the answer is just the value 0.
Then to work out each additional subproblem we can iterate through the array marking values as possible if they consist of some value made from previous coin values plus some number of coins of the current value.
As we are updating the DP array in-place, it turns out that we only have to consider adding one coin of the current value for each location in the array.

Why swap the item order of the knapsack lead to the same solution?

As far as I know, knapsack problem uses dynamic programming to find the best solution of each item depending on its previous items. This hypothesis assumes that the solution is depending on the order of items. Why the final solution is not depend on the order?
No. The dynamic programming solution for knapsack problem does not depend on the previous items.
When considering whether to put an item into the knapsack or not, we just need to consider the remaining capacity of the knapsack before and after selecting the item. So we loop through all the possible remaining capacity and choose the best one.
dp[i][c] = max(dp[i-1][c+w[i]] + v[i], dp[i-1][c])
where dp[i][c] indicates the maximum possible value after considering the i-th item with the remaining capacity of the knapsack equals to c. w[i] indicates the weight(or volume) of the i-th item, and v[i] indicates the value of the i-th item.
It's not necessary to consider the items in order. Considering the items in order is just for convenience. You can also consider selecting the items in a reversed order, or in a random order.

Knapsack with unbounded items

I am familiar with the 0-1 knapsack problem and when you are given a certain number of copies from each item but I can figure out how to solve it when you are given infinite copies of each item using dynamic programming. I am trying to solve it by hand right now so I am not interested in any particular code. For example here is how I solve the 0-1 problem. How do I need to modify this if I am given an infinity amount of copies of each item?
Edit: I am aware there is a second solution to the problem containing items 1,2, and 3 with the same total value.
One possibility would be to provide a suitable number of multiplicities of the items. For item i, there can be at most
m_i := K / w_i
choices of that item, where K denotes the knapsack capacity and w_i denotes the weight of the i-th item. Furthermore, for each weight value which occurs in the instance, there is at most one item type necessary, namely the one with maximum profit with respect to the weight.
Equivalently, one could modify the evaluation of the dynamic program to reflect the different number of items to be taken, instead of just distinguishing between a choice of 0 and 1.

Resources