2
$\begingroup$

Giving a the following:

A list of a store items $T=\{t_1, t_2,...,t_n\}$.

A list of prices of each item $P=\{p_1, p_2,...,p_n\}$.

A list of quantities of each item $Q=\{q_1, q_2,...,q_n\}$respectively.

And total bill $M$.

Our goal is to find any possible list of items that its total value is equal to $M$ using dynamic programming.

My question does 0/1 weighted Knapsack problem help, where $M$ can be the capacity of the knapsack, and the weight of each item equal to the quantity of the item. If there is any other better approach I would appreciate any references.

asked Oct 21, 2019 at 21:34
$\endgroup$

2 Answers 2

1
$\begingroup$

It turned out we can achieve it in $O(nM)$ time where $n$ is the number of distinct items in the store, and $M$ is the final bill.

We can build a 2-dimensional array with size $C[T, M]$ as follows:

$C[i, j] = 1$, if there exists a way to add items from $\{t_1,t_2,...,t_i\}$ that adds up to $M$.

$C[i, j] = 0$, if we cannot find items that adds up to $M$.

Finally, it's useful to use one extra row and column to make the calculation easier in the recursive solution.

answered Nov 30, 2019 at 15:00
$\endgroup$
0
$\begingroup$

You can model this problem as a 0/1 Knapsack problem by splitting the $i$-th item $q_i$ times. Let the maximum value in $Q$ be $K$, then the typical 0/1 Knapsack solves the problem in $O(MNK)$ time.

For simplicity, suppose $q_i$ can be written as 1ドル+2+\dots + j$. Then we can split the $i$-th item into new items of price 1ドル\cdot p_i, 2\cdot p_i, \dots , j\cdot p_i$. The powerful thing about this way of splitting is that there is a combination of items to form 1ドル$ to $q_i$. For example, if $q_i=10$ and we split into item {1,ドル 2, 3, 4\}$, we can form 6ドル$ using 4ドル+2$, 9ドル$ using 4ドル+3+2$ and so on. Therefore, 0/1 Knapsack will still gives a correct answer. If $q_i$ cannot be represented as sum of consecutive natural numbers, find the largest $j$ such that $q_i\geq \sum_{x=1}^j x$ and $r$ be remainder, then split $q_i$ into $\{1,2,\dots ,j, r\}$. Now each item gives $O(\sqrt M)$ more items in the 0/1 Knapsack, hence our solution now becomes $O(MN\sqrt{K})$.

We can further improve this by using the splitting strategy using power of two's $\{1, 2, 4, 8,\dots\}$. Then each item know gives only $O(\lg K)$ new items and our overall solution becomes $O(MN\log K)$.

answered Oct 22, 2019 at 5:21
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.