Change-making problem

The change-making problem addresses the following question: how can a given amount of money be made with the least number of coins of given denominations? It is a knapsack type problem, and has applications wider than just currency.

Mathematical definition

Coin values can be modeled by a set of n distinct positive integer values (whole numbers), arranged in increasing order as w1 = 1 through wn. The problem is: given an amount W, also a positive integer, to find a set of non-negative (positive or zero) integers {x1, x2, ..., xn}, with each xj representing how often the coin with value wj is used, which minimize the total number of coins

subject to

Non currency examples

An application of change-making problem can be found in computing the ways one can make a nine dart finish in a game of darts.

Methods of solving

Simple dynamic programming

A classic dynamic programming strategy works upward by finding the combinations of all smaller values that would sum to the current threshold. Thus, at each threshold, all previous thresholds are potentially considered to work upward to the goal amount W. For this reason, this dynamic programming approach may require a number of steps that is at least quadratic in the goal amount W.

Optimal Substructure

We can use dynamic programming strategy to solve this problem because it exhibits optimal substructure.

Proof.

Suppose is the optimal solution for making coins. Then , for some , is an optimal solution for the subproblem of making coins.

Now, suppose is not the optimal solution for making coins, then there must exist an optimal solution that we call . Since is the optimal solution, it must contain less number of coins than .

If we combine with , we obtain the optimal solution for making coins, but this contradicts our assumptions that is the optimal solution for the original problem.

Implementation

The following is a dynamic programming implementation (with Python 3) using a matrix for keeping track of optimal solutions to subproblems. At the end, it returns the minimum amount of coins. If you want to obtain the set of coins for the optimal solution, you can keep track of them using for example another matrix.

 1 def _get_change_making_matrix(set_of_coins, r):
 2     m = [[0 for _ in range(r + 1)] for _ in range(len(set_of_coins) + 1)]
 3 
 4     for i in range(r + 1):
 5         m[0][i] = i
 6 
 7     return m
 8 
 9 def change_making(coins, n):
10     """This function assumes that all coins are available infinitely.
11 
12     n is the number that we need to obtain with the fewest number of coins.
13 
14     coins is a list or tuple with the available denominations."""
15 
16     m = _get_change_making_matrix(coins, n)
17 
18     for c in range(1, len(coins) + 1):
19 
20         for r in range(1, n + 1):
21 
22             # Just use the coin coins[c - 1].
23             if coins[c - 1] == r:
24                 m[c][r] = 1
25 
26             # coins[c - 1] cannot be included.
27             # We use the previous solution for making r,
28             # excluding coins[c - 1].
29             elif coins[c - 1] > r:
30                 m[c][r] = m[c - 1][r]
31 
32             # We can use coins[c - 1].
33             # We need to decide which one of the following solutions is the best:
34             # 1. Using the previous solution for making r (without using coins[c - 1]).
35             # 2. Using the previous solution for making r - coins[c - 1] (without using coins[c - 1]) plus this 1 extra coin.
36             else:
37                 m[c][r] = min(m[c - 1][r], 1 + m[c][r - coins[c - 1]])
38 
39     return m[-1][-1]

Dynamic programming with the probabilistic convolution tree

The probabilistic convolution tree[1] can also be used as a more efficient dynamic programming approach. The probabilistic convolution tree merges pairs of coins to produce all amounts which can be created by that pair of coins (with neither coin present, only the first coin present, only the second coin present, and both coins present), and then subsequently merging pairs of these merged outcomes in the same manner. This process is repeated until the final two collections of outcomes are merged into one, leading to a balanced binary tree with W log(W) such merge operations. Furthermore, by discretizing the coin values, each of these merge operations can be performed via convolution, which can often be performed more efficiently with the fast Fourier transform (FFT). In this manner, the probabilistic convolution tree may be used to achieve a solution in sub-quadratic number of steps: each convolution can be performed in n log(n), and the initial (more numerous) merge operations use a smaller n, while the later (less numerous) operations require n on the order of W.

The probabilistic convolution tree-based dynamic programming method also efficiently solves the probabilistic generalization of the change-making problem, where uncertainty or fuzziness in the goal amount W makes it a discrete distribution rather than a fixed quantity, where the value of each coin is likewise permitted to be fuzzy (for instance, when an exchange rate is considered), and where different coins may be used with particular frequencies.

Linear programming

Integer Linear Programming is often a quick way to solve this kind of problem, but the time it will take to resolve the problem is not certain, and may be slow in some cases

Greedy method

For the so-called canonical coin systems, like the one used in US and many other countries, a greedy algorithm of picking the largest denomination of coin which is not greater than the remaining amount to be made will produce the optimal result.[2] This is not the case for arbitrary coin systems, though: if the coin denominations were 1, 3 and 4, then to make 6, the greedy algorithm would choose three coins (4,1,1) whereas the optimal solution is two coins (3,3).

Related problems

The "optimal denomination problem"[3] is a problem for people who design entirely new currencies: What denominations should be chosen for the coins in order to minimize the average cost of making change—i.e., the average number of coins needed to make change? The version of this problem assumed that the people making change will use the minimum number of coins (from the denominations available). One variation of this problem assumes that the people making change will use the "greedy algorithm" for making change, even when that requires more than the minimum number of coins. Most current currencies use a 1-2-5 series, but some other set of denominations would require fewer denominations of coins or a smaller average number of coins to make change or both.

See also

References

  1. Serang (2014): Serang, O. (2012). "The Probabilistic Convolution Tree: Efficient Exact Bayesian Inference for Faster LC-MS/MS Protein Inference". PLOS ONE. 7 (3): e91507. doi:10.1371/journal.pone.0091507.
  2. Xuan Cai (2009). "Canonical Coin Systems for CHANGE-MAKING Problems". Proceedings of the Ninth International Conference on Hybrid Intelligent Systems. 1: 499–504. doi:10.1109/HIS.2009.103.
  3. J. Shallit. "What this country needs is an 18c piece" (PDF). Mathematical Intelligencer. 25 (2): 20–23. doi:10.1007/BF02984830.
This article is issued from Wikipedia - version of the 6/11/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.