The change-making problem addresses the question of finding the minimum number of coins (of certain denominations) that add up to a given amount of money. It is a
special case
In logic, especially as applied in mathematics, concept is a special case or specialization of concept precisely if every instance of is also an instance of but not vice versa, or equivalently, if is a generalization of . A limiting case is ...
of the integer
knapsack problem
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit an ...
, and has applications wider than just currency.
It is also the most common variation of the ''coin change problem'', a general case of
partition
Partition may refer to:
Computing Hardware
* Disk partitioning, the division of a hard disk drive
* Memory partition, a subdivision of a computer's memory, usually for use by a single job
Software
* Partition (database), the division of a ...
in which, given the available denominations of an infinite set of coins, the objective is to find out the number of possible ways of making a change for a specific amount of money, without considering the order of the coins.
It is
weakly NP-hard, but may be solved optimally in
pseudo-polynomial time In computational complexity theory, a numeric algorithm runs in pseudo-polynomial time if its running time is a polynomial in the ''numeric value'' of the input (the largest integer present in the input)—but not necessarily in the ''length'' of t ...
by
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.
I ...
.
Mathematical definition
Coin values can be modeled by a set of distinct positive
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
values (whole numbers), arranged in increasing order as through . The problem is: given an amount , also a positive integer, to find a set of non-negative (positive or zero) integers , with each representing how often the coin with value 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
A nine-dart finish, also known as a nine-darter, is a perfect leg or single game in the sport of darts. The object of the game is to score a set number of points, most commonly 501; in order to win, a player must reach the target total exactly an ...
in a game of darts.
Another application is computing the possible atomic (or isotopic) composition of a given mass/charge peak in mass spectrometry.
Methods of solving
Simple dynamic programming
A classic
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.
I ...
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 requires a number of steps that is O(''nW),'' where ''n'' is the number of types of coins.
Implementation
The following is a dynamic programming implementation (with Python 3) which uses a matrix to keep track of the optimal solutions to sub-problems, and returns the minimum number of coins, or "Infinity" if there is no way to make change with the coins given. A second matrix may be used to obtain the set of coins for the optimal solution.
def _get_change_making_matrix(set_of_coins, r: int):
m = 0 for _ in range(r + 1)for _ in range(len(set_of_coins) + 1)]
for i in range(1, r + 1):
m i] = float('inf') # By default there is no way of making change
return m
def change_making(coins, n: int):
"""This function assumes that all coins are available infinitely.
n is the number to obtain with the fewest coins.
coins is a list or tuple with the available denominations.
"""
m = _get_change_making_matrix(coins, n)
for c, coin in enumerate(coins, 1):
for r in range(1, n + 1):
# Just use the coin
if coin r:
m r] = 1
# coin cannot be included.
# Use the previous solution for making r,
# excluding coin
elif coin > r:
m r] = m - 1r]
# coin can be used.
# Decide which one of the following solutions is the best:
# 1. Using the previous solution for making r (without using coin).
# 2. Using the previous solution for making r - coin (without
# using coin) plus this 1 extra coin.
else:
m r] = min(m - 1r], 1 + m r - coin])
return m 1-1]
Dynamic programming with the probabilistic convolution tree
The probabilistic convolution tree
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
A fast Fourier transform (FFT) is an algorithm that computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in th ...
(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.
Greedy method
For many real-world coin systems, such as those used in the US and many other countries, a
greedy algorithm
A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic can yield locally ...
of picking the largest denomination of coin which is not greater than the remaining amount to be made will produce the optimal result. This is not the case for arbitrary coin systems or even some real world systems, though. For instance, if we consider the old (now withdrawn) Indian coin denominations of 5, 10, 20 and 25 paise, then to make 40 paise, the greedy algorithm would choose three coins (25, 10, 5) whereas the optimal solution is two coins (20, 20). A coin system is called "canonical" if the greedy algorithm always solves its change-making problem optimally. It is possible to test whether a coin system is canonical in
polynomial time
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
.
Related problems
The "optimal
denomination problem"
[
] is a problem for people who design entirely new currencies. It asks what denominations should be chosen for the coins in order to minimize the average cost of making change, that is, 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
*
List of knapsack problems The knapsack problem is one of the most studied problems in combinatorial optimization, with many real-life applications. For this reason, many special cases and generalizations have been examined.
Common to all versions are a set of ''n'' items, w ...
*
Coin problem
The coin problem (also referred to as the Frobenius coin problem or Frobenius problem, after the mathematician Ferdinand Frobenius) is a mathematical problem that asks for the largest monetary amount that cannot be obtained using only coins of ...
*
The coin collector's problem
References
Further reading
* {{cite journal , author=M. Adamaszek, A. Niewiarowska , title=Combinatorics of the change-making problem , journal=European Journal of Combinatorics , volume=31 , issue=1 , year=2010 , pages=47–63 , doi=10.1016/j.ejc.2009.05.002 , arxiv=0801.0120, s2cid=13527488
Number theory
Recreational mathematics
Combinatorial optimization
Articles with example Python (programming language) code