On the Change-Making Problem
Total Page:16
File Type:pdf, Size:1020Kb
On the Change-Making Problem Timothy M. Chan∗ Qizheng He∗ Abstract (There are a number of other interesting problems Given a set of n non-negative integers representing a coin related to coin systems, such as the Frobenius problem, system, the change-making problem seeks the fewest number but these are not relevant to the present paper.) of coins that sum up to a given value t, where each Partially spurred by a renewed interest in fine- type of coin can be used an unlimited number of times. grained complexity, there have been a flurry of re- This problem is a popular homework exercise in dynamic cent works that improve upon the standard O(nt) so- programming, where the textbook solution runs in O(nt) lution for the subset sum problem,p starting with Koil- time. iaris and Xu's deterministic O( nt polylog t)-time al- It is not hard to solve this problem in O(t polylog t) time gorithm [8, 9], followed by Bringmann's randomized by using convolution. In this paper, we present a simple O(t polylog t)-time algorithm [3] and Jin and Wu's deterministic O(t log t log log t) time algorithm, and later randomized O(t log t)-time algorithm from SOSA last improve the running time to O(t log t) by randomization. year [7]. All these improved algorithms use convolu- tions (i.e., FFT) or other algebraic techniques. There 1 Introduction are also conditional lower bounds [1] to suggest that get- ting better than t1−" running time might not be possible In the change-making (or coin changing) problem, a for subset sum (ignoring nO(1) factors). Of course, as a cashier wants to give exactly t amount of money to the function of n, improving upon 2n=2 worst-case time has customer, choosing from a set of coins, where each type remained a longstanding open problem. of coin has a positive integer value and has unlimited Despite all these recent works on subset sum, the supply, and the cashier wants to minimize the number change-making problem has not received as much at- of coins used. More formally, given a set V of n positive tention, and we are not aware of any reported im- integers (coin values) and a target number t, we want provements. One paper [10] provided conditional hard- to select the fewest number of elements from V that ness results for knapsack-related problems, including a sum to exactly t, where an element may be picked stronger version of the change-making problem with a more than once. This problem is a popular exercise in general linear objective function. Goebbels et al. [5] algorithm textbooks, where the standard solution runs noted an O(mt log t)-time algorithm for the decision in O(nt) time by dynamic programming [13]. (The naive problem of testing whether there is a solution with at greedy algorithm may fail to give optimal solutions for most m coins, which is better for small m, but their arbitrary sets of coin values.) The problem is known to solution uses convolution in a very naive way. The be weakly NP-hard [11]. situation is somewhat ironic, considering that change- The change-making problem is closely related to making seems not as difficult to solve via convolution, many other well known problems with similar O(nt)- because duplicates are allowed (in previous works on time textbook solutions, the most relevant of which solving subset sum via convolution [8, 3], much of the include challenge is about how to avoid duplicates). Indeed, an • subset sum: select elements from V to sum to O(t polylog t)-time solution follows almost immediately exactly t, but an element may be picked at most by combining convolution with repeated squaring|we once, and a minimum solution is not required; point out some of these easily rediscoverable solutions in Section 3.1 • unbounded knapsack: select elements from V to Following these straightforward solutions with extra sum to at most t, and an element may be picked logarithmic factors, we present a new simple determin- more than once, but we want to maximize a general istic algorithm in Section 4 that improves the running linear objective function. 1For example, the first solution in Section 3 has appeared ∗Department of Computer Science, University of Illinois at in an algorithms homework by Sariel Har-Peled; see the second Urbana-Champaign, USA, ftmc,[email protected]. Work page of https://courses.engr.illinois.edu/cs473/fa2018/hw/ supported in part by NSF Grant CCF-1814026. hw_03.pdf. Copyright c 2020 by SIAM Unauthorized reproduction of this article is prohibited time to O(t log t log log t), and a further refinement us- Lemma 2.3. We can compute the convolution A∗t B of ing (Las Vegas) randomization in Section 5 that achieves two arrays A and B in deterministic O(t log t) time, by O(t log t) expected running time. FFT. We feel that the succession of improved solutions presented here is well suitable for teaching, considering Boolean convolution. When the elements of A and B the popularity and natural appeal of the change-making are Boolean values in f0; 1g, we can define the Boolean problem. convolution of A and B (with respective size t1 and t2) to be an array A ◦ B with t1 + t2 − 1 elements such that Wt1−1 2 Preliminaries (A◦B)[i] = j=0 (A[j]^B[i−j]) for 0 ≤ i ≤ t1 +t2 −2 The change-making problem can be formally defined as (where we assume out-of-range values are 0). Similarly follows: let A ◦t B denote the array containing the first t + 1 elements of A ◦ B. It is easy to see (A ◦ B)[i] = 1 iff Problem 2.1. (Change Making (Optimization (A ∗ B)[i] > 0, so we can compute A ◦t B by Lemma 2.3 Version)) Given a set V = fv1; : : : ; vng of n nonneg- in deterministic O(t log t) time. ative integers (coin values) and an integer t, find the The following lemma states that speedup is possible smallest multiset S (duplicates allowed) contained in V with randomization, if we can tolerate error with prob- such that S sums to exactly t, i.e., find the minimum m∗ ability p per entry. This improvement will be needed Pn Pn of i=1 mi subject to the constraint that i=1 mivi = t, only in our final randomized algorithm in Section 5. where mi 2 N. Lemma 2.4. Given two arrays A and B with size O(t) For simplicity of presentation, we focus on finding and 0 < p < 1, we can compute an array C in ∗ 1 the minimum number of coins m . It is possible to randomized O(t log p ) time such that for all 0 ≤ i ≤ t: modify our algorithms to return the optimal multiset of coins without increasing the asymptotic running time; • If (A ◦t B)[i] = 0, then C[i] = 0. see the remark at the end of Section 4. • If (A ◦ B)[i] = 1, then Pr[C[i] = 1] ≥ 1 − p. Without loss of generality, we assume that 0 is in V , t since adding a coin value 0 does not affect the optimal Proof. For p = 1=2, this was noted for example by solution. This assumption ensures monotonicity, which Indyk [6]. Roughly, we use Freivald's technique [4] to will be useful: if there is a solution using m coins, then 0 0 reduce Boolean convolution to convolution over GF(2), there is a solution using m coins for any m ≥ m. We with constant error probability. The latter problem can may assume that n ≤ t. Trivially, m∗ ≤ t (unless ∗ be solved by packing k = Θ(log t) bits and compute m = 1). t k convolution of arrays with size O( k ) over GF(2 ) by Some of our algorithms will solve the decision FFT. version of the problem as a stepping stone: We can further reduce the error probability to any p < 1 by repeating O(log 1 ) times independently and Problem 2.2. (Change Making (Decision Ver- p taking the entry-wise or of the results. sion)) Given a set V = fv1; : : : ; vng of n nonnegative integers (coin values), an integer t, and an integer m, decide whether m∗ ≤ m, i.e., whether there is a multiset 3 Basic Algorithms S with m coins (counting duplicates) such that S sums We first present two basic algorithms for the change- to exactly t, i.e., whether there exist mi 2 N such that making problem. Pn Pn i=1 mivi = t and i=1 mi = m. First solution. The first solution solves the decision We use the standard word-RAM model with word version of the problem in O(t log2 t) time, and the length w = Θ(log t) in this paper. optimization version in O(t log3 t) time. For any k, let Ck[0; : : : ; t] denote the Boolean array where Ck[j] = 1 iff Convolution. Let A[0; : : : ; t1 − 1] be an array with we can sum to exactly j using k coins. Given a value m, ∗ t1 elements, and let B[0; : : : ; t2 − 1] be an array with we can decide whether m ≥ m by computing Cm[t]. It t2 elements. The convolution of A and B is an array is easy to compute C1 in O(t) time, and we can compute A ∗ B with t1 + t2 − 1 elements such that (A ∗ B)[i] = Cm recursively (by repeated squaring with respect to t −1 P 1 the operator ◦ ): if m is even, C = C m ◦ C m , j=0 A[j]B[i − j] for 0 ≤ i ≤ t1 + t2 − 2 (where we t m 2 t 2 assume out-of-range values are 0).