简体   繁体   中英

Minimum coin change or 0-1 knapsack

I have data set like this:

Length: 233, 333, 450, 560, 650, 780 Limit: 5400

Now my problem is to choose item from length set highest to lowest to make up the limit or come as close as possible.

I know both knapsack and minimum coin change can solve my problem. I would like to know which one would be preferable.

Note that coin change is uses greedy algorithm and knapsack uses dynamic programming

From my understanding of your question, it seems that you're debating whether your problem can be solved with a greedy algorithm or if it is more like the 0-1 knapsack problem, and a greedy algorithm would not give you the optimal solution every time.

Here is some background information about greedy algorithms and the 0-1 knapsack problem:

To expand on greedy algorithms, they work on the basis that there is some universally optimal property to choose the next item from, and work locally. In other words, for an algorithm to be greedy, it must have the greedy choice property and optimal substructure , meaning that the first choice made by the greedy algorithm can be chosen in an optimal solution, and that an optimal solution contains the optimal solution to smaller subproblems within it.

The 0-1 knapsack problem, on the other hand, is not a problem that can be solved greedily. This is most obviously observed in the fact that the greedy choice property is not displayed. There is not a single attribute to pick your first and next items based of off; if you choose the largest item, it may be that it happens to take up too much space and there are no smaller items that can fill that space, whereas choosing smaller items would have completely filled the knapsack. If you choose smaller items first, it may result that your knapsack now has a space that is too small for larger items to fill, and choosing one of those larger items earlier would have completed the knapsack.

0-1 knapsack is a problem that requires a dynamic programming solution, which operates non-locally and in layman's terms, is a form of optimized "brute-force". In other words, it can be thought of as an optimization over recursion.


Your problem is to choose discrete items to fulfill or come as close to the maximum length limit.

This sounds like to me, almost exactly like the 0-1 knapsack problem, which cannot be solved with a greedy algorithm if you want the optimal solution with any dataset.

Instead, we want to use dynamic programming, and you can tell that a problem can be solved this way if it has both optimal substructure and overlapping subproblems .

The 0-1 knapsack problem displays optimal substructure due to: The maximum value that can be obtained from a data set of n items and W weight is either: the maximum value from n - 1 items and W weight, OR, the maximum value from n - 1 items and W - weight of item n, plus the value of n. Each of these subproblem's optimal solutions can be combined to create the optimal solution for the larger problem.

The 0-1 knapsack problem also has overlapping subproblems due to: The recursive solution to the problem involves trying all combinations of n items and checking whether the weight of those items are less than W, then finding the maximum of those values. This inherently involves a lot of repetitive work, such each combination of items tried can be calculating the same total as previously calculated on fewer items, and therefore, using simple combinatorics, you can see that the time complexity becomes factorial.

That is why the 0-1 knapsack problem and your problem would be best solved using dynamic programming, so you can save answers to previous subproblems and refer back on your already-calculated sums to solve later problems that depend on the overlapping subproblems' answers.

-

Thank you, and good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM