Professional Documents
Culture Documents
ASH_CC_Algo_Coin_Change_Algorithm_Optimi
ASH_CC_Algo_Coin_Change_Algorithm_Optimi
Ashar Mehmood
School of Electrical Engineering and Computer Science (SEECS)
National University of Science and Technology (NUST)
Islamabad, 44000, Pakistan
A dynamic programming algorithm is most appropriately The presented algorithm is designed to minimize the space
applied to those problems for which table, containing the complexity. In this regard, greedy approach has done its work
computed solutions of encountered subproblems; helps by minimizing space complexity to O(1) but in most of the
eliminate a great number of redundant computations. DP is cases greedy approach fails to produce optimal result.
primarily applicable to those problems that can be expressed Moreover, it produces very bad solution in several cases. If
as a sequence of decisions to be made at each of several we talk about dynamic solution, although it produce optimal
stages. There are many dynamic programming algorithms [3] solution but its space complexity is O(V). This means that if
used to solve real life problems such as knapsack algorithm V is large it’ll take large space to find the optimal result.
(for different knapsack problem [4]), Needleman–Wunsch Thus, we come to solution which has less space complexity
algorithm [5], Bellman–Ford algorithm [6] etc. and give optimal result (presented algorithm).
1
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
Formally, the Coin-Change problem solves the following sizes can now be several megabytes of memory, which allows
integer programming problem with respect to given amount of recursion to go fairly deep without causing a core dump.
V. Sometimes recursion is used because it employs a simpler
process as compared to the iterative version. For example,
Min nearly all code written for tree-like structures is recursive.
s.t =V, ci>=0 Many sorting algorithms are more naturally written
recursively as well.
Generally, representation of V under C is called the feasible
solution (n1, n2, n3, . . . , nm) of the above integer programming However, recursive solutions can be very inefficient, if one is
problem. If this representation satisfies not diligent. For example, the obvious recursive solution to
compute the Nth Fibonacci number has exponential running
<ci for 2≤i≤m, time, even though the loop version runs in O(n) and same case
then, it is the greedy representation of V, denoted by is for coin change problem. Recursive formula for solving
GRDC(V), and |GRDC(V)|= is its size. Similarly, the coin change problem is given below:
optimal solution (x1, x2, x3, . . . , xm) is called the optimal
representation of V, denoted by OPTC(V), and 3.1 Algorithm
|OPTC(V)|= is its size, all mentioned in [10]. min_Coin(coins[], m, V)
Following the aforementioned approach, the presented If V == 0, return 0;
algorithm’s solution (a1, a2, a3, . . . ,am) is called the
suboptimal representation of V, denoted by ASHC(V), and rsltINT_MAX;
|ASHC (V)|= is its size, where ai is the frequency (the
from coinsi to coinsm
maximum number of times a coin ci is used to make amount
V) of coin at ith denomination. If (coinsi<=V)
Using the presented approach, we will be able to achieve the sbrsltmin_Coin(coins[],m,V-coinsi);
result |ASHC(V)|, which will be equal to |OPTC(V)| in most
of the time and have accuracy comparable to |OPTC(V)| in if (sbrslt!=INT_MAX and sbrslt+1 < rslt)
rest of the cases. But the positive point is that we use rsltsbrslt+1;
minimum space using this approach. Using minimum space
we are achieving optimal result in most of the time. return result;
We are dividing the algorithm into three parts: Using recursive formula, the time complexity of coin change
problem becomes exponential. If we consider the complete
Check all the coins. recursion tree given below then we can see that many sub
problems are solved repeatedly and redundantly. It results in
Check maximum frequency (the maximum number
unnecessary overhead due to which the complexity of
of times a coin ci is used to make amount V) of each
algorithm ends up in exponential value.
coin in making amount V and use only few of them
which is enough to reach the optimal result.
On the basis of minimum to maximum frequency
(that has been used) of a specific coin, check all the
other coins’ frequency in making amount V.
All the aforementioned steps are done in nested form.
for each Ci in C s.t 1≤i≤m, where m=total number of
coins in C.
for each Ci in C evaluate fmax.
Fig 1: Recursive tree for understanding the working of
fmin to fminx of Ci evaluate fCj s.t 1≤j≤m and j≠i, recursion in coin change problem solution and its
where fminx≤fmax: frequencies of coin which redundant calculations [13]
participate in finding optimal result. Dynamic Programming is an algorithmic technique which is
All the aforementioned steps cover most of the cases to find usually based on a recurrent formula and one (or multiple)
optimal solution. For each frequency of each coin, the result initial state(s). A sub-solution of the problem is constructed
will be updated. At the end, we will have optimal solution from previously solved ones. Dynamic programming
(|ASHC(V)|= |OPTC(V)|) or suboptimal solution having solutions have a polynomial complexity which assures a much
accuracy comparable to |OPTC(V)|. faster running time than other techniques like backtracking[3],
brute-force[14,15], recursion etc. Algorithm of dynamic
3. EXISTING TECHNIQUES programming based solution is given below:
We ask that authors follow some simple guidelines. In
essence, we ask you to make your paper look exactly like this 3.2 Algorithm
document. The easiest way to do this is simply to download min_Coin(coin[],m,V)
the template, and replace the content with your own material. for i = 1 to V
Recursion [11][12] is used in the majority of programming
problems as it is believed to be an efficient approach. tableiINT_MAX
Generally, recursion should only be used when the number of
recursive calls is not excessive. The number of recursive calls for j = 1 to V
somewhat depends on the amount of memory available. Stack for k = 0 to m-1
2
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
In algorithm, abbreviations of different variables are used “Instant_val_of_amount”: A temporary variable used to
which includes Of : outputflag, Mc : min_no_of_coins, Tc : represent instant value of amount.
total_coins, IA : instant_val_of_amount, Am : amount and Nc “Amount”: Amount (V) to be made.
: no_of_coins.
“Ci”: coin selected at that instant
4.1 Algorithm “Cm”: coin which has lowest denomination (assumed that
Ash_change(Nc,coins<vector>, Am)
coins are placed in descending order) where m is total number
Offalse; of coins provided.
Mc0; “Cj”: coin selected at that instant other than Ci.
Tcinfinite; “f”: frequency of selected coin used in making amount V.
IA0; “Cfmin”: minimum frequency of selected coin used, in making
amount V i.e 0.
from ci to cm
“Cfmax”: maximum frequency of selected coin used, in making
from cfmin to cfminx amount V.
Mcf; “Cfminx”: frequencies which participate in obtaining optimal
IAAm-( ci * Mc ); result.
3
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
optimal result, can be considered. It does not check some of amount V(which is 11) is 1, but if in some other case
the possible frequencies of selected coin because using those maximum frequency of first coin is greater than 1, then it will
frequencies may result in bad solutions e.g it’s better to be used maximum frequency times rather than using
neglect those frequencies of selected coin. sufficiency principle which results in a frequency lesser than
maximum frequency.
On the basis of different frequencies of selected coin, it
evaluates other coins’ frequencies using greedy approach. We can use 9 only one time in making 11. So, inner loop will
Whenever all the frequencies of other coins are calculated run 2 times in this branch (once for coins of denomination 9
with respected to specific frequency of selected coin, our with frequency 0 and once for frequency 1, same process will
result is updated on the condition that the current result is be used for all coins) and remaining amount will be made
smaller than or equal to previous one. Otherwise, result will using rest of the coins in most inner loop.
retain its previous value. With combination of some
frequencies of every coin and greedy approach, we are fi0,1
achieving optimal solution in most of the time and suboptimal V’V-(ci*fi)11,2
solution of accuracy comparable to dynamic solution in rest of
the cases. for each cj in C
min_no_of_coin=V’/cj s.t V’>cj and j!=i (eq. 1)
4.4 Algorithmic point of view
We have three loops e.g outer loop, inner loop and most inner V’=V’%cj (eq. 2)
loop. Outer and most inner loop will run m (total no. of coins)
times and on the basis of selected coin in outer loop, inner So, we will have 2 solutions in this branch i.e 1(6)+1(5)2
loop will run cfminx times (the number of times a coin and 1(9)+2(1)3, where V is original amount and V’ is
instantaneous amount. Thus, we will go for 2 coins..
participates in obtaining optimal result).
As, min_no_of_coin<total_coins
In outer loop, algorithm checks each coin one by one and
passes it to inner loop. Inner loop finds the c fmax and iterates total_coins2
from cfmin to cfminx (check frequencies of selected coin which
is sufficient for obtaining optimal result.Criteria for On the basis of coin of denomination 6:
sufficiency is described below in the example). At the end, Cfmax V/ci1
most inner loop, on the basis of selected coin’s specific
frequency, evaluates other coins’ frequencies using greedy We can use 6 only one time in making 11. So, inner loop will
approach but excluding the coin which is already selected in run 2 times in this branch and remaining amount will be made
outer loop. At any moment, in most inner loop, when the using rest of the coins in most inner loop.
amount V is reached, algorithm doesn’t check rest of the
fi0,1
coins. It just breaks out of the loop, updates the total_coins
and iterates for other frequencies of selected coin, if they V’V-(ci*fi)11,5
exist.
Same steps will be evaluated, aforementioned for coin of
With every inner loop’s iteration, total_coins is updated on denomination 9, represented as eq.1 and eq.2. We will have 2
condition that min_no_of_coins calculated is smaller than solutions in this branch e.g 1(9)+2(1)3 and 1(6)+1(5)2.
total_coins (instantaneous best result). This means that we As min_no_of_coin is not lesser than total_coins the result
update our result whenever we come up with better solution. will remain the same.
For example, consider the case where there are coins in
descending order of denominations 9, 6, 5 and 1 and we have total_coins2
to make amount V=11. The optimal result from dynamic On the basis of coin of denomination 5:
approach gives 2 coins but it is done after memorization.
Presented approach will find the same result without making Cfmax V/ci2
use of memoization. Algorithm will find the best decision as We can use 5 two times in making 11. So, inner loop should
given the manner that follows: run 3 times in this branch to check all the cases but inner loop
Criteria for sufficiency: Value obtained by multiplying will run two times due to sufficiency principle because for
frequency of selected coin and selected coin denomination frequency 2 of selected coin (denomination 5) value will be
should be less than the denomination of coin before it e.g greater than the denomination of coin right before it (which is
ci*f<ci-1. Sufficiency principle will not be used for first coin 6). However, its advantage is not observable in such cases
(no coin exists before it). Sufficiency principle will be where it discards just one iteration: its advantage is more
followed for the coins which are factors of amount V but not evident in next branch (coin of denomination 1). Thus, after
strictly as mentioned above; it just also checks the selected using frequency 0 and 1 of selected coin, remaining amount
coin maximum frequency along with frequencies which will be made using rest of the coins in most inner loop.
resulted from sufficiency principle (there are high chances fi0,1
that the maximum frequency of this coin is our optimal
result). V’V-(ci*fi)11,6
total_coinsinfinity Same steps will be evaluated, aforementioned for coin of
On the basis of coin of denomination 9: denomination 9, represented as eq.1 and eq.2. We will have
two solutions in this branch e.g 1(9)+2(1)2 and
Cfmax V/ci1 1(5)+1(6)2. However, neither of the two is less than
As there is no coin before 9 so it will be used maximum total_coins current value which is 2. So, total_coins value will
number of times (sufficiency principle will not be applicable remain unchanged.
in case of first coin). Its maximum frequency in making On the basis of coin of denomination 1:
4
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
5
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
division etc are considered an operation. Operational analysis Followings are the analysis of different algorithms with no. of
of above test is given below: coins C nearly equal to amount to be made. Amount ranges
from 2000 to 2500 (501 test cases) and coins are 1 to
Abbreviations of different variables are used in the test which V(amount)-1 e.g for V=10 coins are {1, 2, 3, 4, 5, 6, 7, 8, 9}.
includes, TO: Total number of operations used for whole
Table 4.0. Number of coinsAmount to be made
testing, AVGOP: Average number of operation used in whole
testing, MAXOP: Maximum number of operation used in AVG
Algo. NOS NSOS TC TI
whole testing. VR
Table 2.1. Real money system (Operational analysis) Dyn. 501 0 1002 0 3.2*109
Algo. TO AVGOP MAXOP Pre. 501 0 1002 0 7.5*106
Dyn. 1.8*109 183790 1.4*107 Gre. 501 0 1002 0 2.5*106
Pre. 1.0*108 10212 1.4*104
Considering above table we can say that greedy approach can
Gre. 8.4*106 842 8.8*102
also give optimal result in this case. In some tests, due to large
number of coins, presented algorithm takes a bit more
It is evident through operational analysis that Dynamic iterations than dynamic approach to find the optimal result,
algorithm takes more operations than presented algorithm to which can be neglected because it is a very rare case e.g
compute optimal result. Thus, we can say that in this case number of coins m are very large (approaches to amount V to
presented algorithm is more efficient as compared to dynamic be made). Operational analysis of this case is given below:
algorithm in terms of time complexity.
Table 4.1. Number of coinsAmount to be made
In real money system, greedy algorithm gives optimal (Operational analysis)
solution with fewer number of iterations. But when we move
Algo. TO AVGOP MAXOP
towards non-real money systems and other cases then it gives
8
very bad result.Following are some more different types of Dyn. 6.9*10 694193 3.2*107
test cases which depicts the efficiency of presented algorithm
over other algorithms. Pre. 1.0*109 1003470 1.4*107
6
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
Following are the analysis of different algorithms with very performance of presented approach by analyzing both graphs,
small no. of coins where the difference in their denominations presented approach gives optimal solution in almost every set
is very large. Amount ranges from 1000 to 21000 (20001 test of test cases except non-real money system. In non-real
cases) and coins C = {449, 93, 1}. money system, its solution varies with a small proportion to
optimal solution e.g just 2, 3 coins more needed. Now, look at
Table 6.0. Fewer coins, larger difference the average number of operations needed for each set of test
AVGV cases. Iteration has not been used as criteria because of the
Algo. NOS NSOS TC TI reason that there can be many operations in an iteration of an
R
algorithm than other algorithm’s iteration. It can be noticed
Dyn. 10001 0 4746584 0 6.1*109 that in few set of test cases, such as the set of test cases where
Pre. 10001 0 4746584 0 1.0*107 no. of coins approaches to amount to be made, presented
algorithm takes more operations than dynamic approach to
Gre. 1783 8218 5026316 34.03 3.0*104 find the result. But again, such cases are very rare. The main
advantage of presented approach is that all the optimal and
suboptimal solutions are evaluated using O(1) space. Unlike
Considering above case, it is obvious that greedy approach dynamic approach, no memoization has been done to find the
gives infeasible result in such cases too. While, presented result. Therefore, it is a big advantage to use proposed
algorithm gives optimal solution in all test cases with fewer algorithm over dynamic approach.
number of iterations than dynamic approach. Operational
analysis of this case is given below: 7. COMPLEXITY ANALYSIS AND ITS
Table 6.1. Fewer coins, larger difference (Operational COMPARISON
analysis) We ask that authors follow some simple guidelines. In
essence, we ask you to make your paper look exactly like this
Algo. TO AVGOP MAXOP document. The easiest way to do this is simply to download
Dyn. 1.8*10 9
189370 5.0*106 the template, and replace the content with your own material.
This paper hasn’t presented a thorough complexity analysis of
Pre. 1.3*108 13279 1.3*104 the presented approach but some cases have been discussed
which clearly distinguish its uses and outline its benefits over
Gre. 2.0*107 2010 2.2*103 other approaches. In worst case, algorithm checks almost
every coin in most inner loop and considers most of the
Following are the graphs of optimal solution and average possible frequencies of selected coin in inner loop (discarding
number of operations in each set of test cases. few frequencies). The problem will be in worst case when
every coin of larger denomination is very far from its
consecutive smaller coin (larger difference in their
denominations). Thus, outer loop runs m (number of coins)
times and for every coin in outer loop, inner loop runs nearly
fmax (every possible frequency to make amount V) times.
However, it does not iterate fmax number of times because of
sufficiency principle. Most inner loop runs m-1 times as it
does not use the selected coin, which is already selected in
outer loop, in most inner loop again. Thus, complexity
becomes,
(m)(m-1)(fmax)=O(m2fmax)
In average case, algorithm checks every coin in outer loop but
in inner loop, it considers only those frequencies of selected
Fig 2: No. of optimal solutions in each set of test cases coin which have the potential to give optimal result
(sufficiency principle). Thus, outer loop runs m times, inner
loop runs fminx times and most inner loop runs less than m-1
times. Some reasons for the average case are that Sometimes,
at maximum frequency of selected coin, most inner loop does
not run because amount (V) to be made happens to be
multiple of selected coin (in inner loop), which can be exactly
divided hence amount is made using selected coin (in inner
loop) only in that branch. There is another reason for average
case, for example we have to make amount V with m number
of coins. Let’s say that amount is not made with a specific
frequency of selected coin (in inner loop) then it will fall into
most inner loop and check all other coins. Suddenly, it finds
that amount V is made. In spite of the fact that some coins are
not checked yet, it will go out of inner loop as amount is
Fig 3: Average no. of operations needed in each set of test made. So, in this case inner loop will not run exactly m-1
cases times. It’ll run some less time. Normally, inner loop does not
Thus, It can be clearly noticed after all the analysis that run for every possible frequency of selected coin. It follows
greedy approach works fine for the real money system but if the sufficiency principle and run fminx times..Last condition
we move towards non-real money system and other cases, it of above three has most effect on the iteration to evaluate
gives infeasible solutions. On the other hand, if we consider optimal result. It lessens most of the iterations and gives the
7
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
same optimal or suboptimal result. Thus average complexity If the number of coins is nearly equal to amount V,
evaluates to, to be made, then its time complexity will no longer
be better than dynamic solution.
(m)(m-1)(fminx)=O(m2fminx)
In some cases, the solution is repeated in presented approach
In the ideal scenario, the difference between maximum and
in a manner similar to recursive technique but they do not
minimum coin’s denomination is minimum and amount V is
have a significant adverse impact on the algorithm’s
nearly equal to maximum coin’s denomination e.g c i=1, 2, 3,
efficiency since the presented approach discards a large
4 and V=5. In best case, in every iteration of outer loop, inner
number of cases which are not useful in obtaining optimal
loop runs once or twice. Thus, inner loop’s iterations are
results.
minimized in best case (≈fmin), outer loop runs m times and
most inner loop, in average, runs less than (m-1) times. Thus, 9. CONCLUSION
in best case complexity becomes, The algorithm introduced in this paper has no memory
2
(m)(m-1)(fmin)=O(m fmin) overhead and significantly reduced the number of iterations in
most of the cases; consequentially the execution time also
As, fmin≈1 so best case complexity becomes O(m2). decreases as now it is dependent on coins and their
The time complexity of presented algorithm is O(m2f) which frequencies, not on the amount to be made. The presented
is more efficient than dynamic solution’s time complexity in algorithm has a complexity O (m2f) which in most cases is
most of the cases. The main advantage of presented algorithm better than dynamic based solution’s complexity O (mV) and
over dynamic solution is that it has space complexity of O(1) recursive solution (exponential complexity). In most cases,
e.g. no memoization is required. The algorithm will be no presented approach gives optimal solution but even if it gives
longer much efficient than dynamic solution (in time suboptimal solution, the accuracy is comparable to optimal
complextiy aspects) in two mutually exclusive situations. result. The case, in which presented algorithm improves
First, when the number of coins given is nearly equal to the performance only marginally as compared to dynamic based
amount to be made. In this case, due to large value of m2, m2f solution arises very rarely. Therefore, in almost all of the
will be large enough to surpass the dynamic complexity mV. cases presented algorithm is far better than existing
But it’s a very rare case. Normally, the number of coins m is algorithms.
very less as compared to amount V to be made.
10. ACKNOWLEDGMENT
Second, number of coins given are very small as compared to The author thanks Dr. Muhammad Ali Tahir (Assistant
amount to be made and difference in their denominations is professor, Department of Computing-SEECS) for his
very large e.g V=5000 and coins are {4949 , 1}. In this case, guidance and inspiration.
m2 will be small but the inner loop will run several times
because sufficiency criteria will not fulfill until value obtained 11. REFERENCES
by multiplying frequency of coin with coin’s denomination [1] Richard E. Bellman, “Dynamic Programming”, Princeton
(1), reaches 4948. University Press Princeton, Dover Publications, NY,
USA, 1957.
Both of the aforementioned cases are very rare and even if
they do occur, their cost is still comparable to dynamic [2] Kevin Q. Brown, “Dynamic Programming in Computer
approach i.e it can be neglected. Thus, the presented algorithm Science”, Technical Report CMU-CS-79-106, pp. 1-3,
is very efficient and feasible solution. On the other hand, 1979.
normally, algorithm takes very less iterations as compared to
[3] Donald E. Knuth, “The Art of Computer Programming”,
dynamic to find optimal or suboptimal solution (of accuracy
Addison-Wesley, US, ISBN: 0-201-03801-3, 1968.
comparable to optimal solution) and takes no memory for the
memoization. Thus, O(m2f) is far better than O(mV) in most [4] John J. Bartholdi, “The Knapsack Problem”, DOI
of the cases and its space complexity (O(1)) has an obvious 10.1007/978-0-387-73699-0_2, ISSN 0884-8289, 2008.
advantage over other algorithms’ space complexity.
[5] S Needleman, C Wuncsch, “A general method applicable
8. ADVANTAGES AND to the search for similarities in the amino acid sequences
of two proteins”, Journal of Molecular Biology, 1970.
DISADVANTAGES
Following are some advantages of presented algorithm over [6] Richard E. Bellman, “On a routing problem”, Quarterly
existing algorithms: Applied Mathematics, DOI: 10.1090/qam/102435, 1958.
It only depends on the number of coins (m) and [7] G. S. Lueker, “Two NP-complete Problems in Non-
frequency (fminx) which eliminates the dependence negative Integer Programming”, Comput. Sci. Lab. Univ.
of execution time or number of iterations on amount Princeton, 1975.
(V) to evaluate optimal or suboptimal solution.
[8] J. W. Wright, “The Change-Making Problem”, J. Assoc.
Fewer iterations are needed. Comput. Mach., Vol. 22, Issue 1, pp. 125-128, NY,
USA, 1975.
The execution time is reduced.
[9] Thomas H. Cormen, Charles Eric Leiserson, Ronald Linn
Memoization is no longer required. Rivest, Clifford Stein, “Introduction to Algorithms”,
Chapter 16, Greedy Algorithm, ISBN: 978-0-262-03384-
There is as such no disadvantage of presented algorithm.
However, there are some limitations of presented algorithm. 8, 2001.
[10] Xuan Chai, “Canonical Coin System for Change-Making
The primary disadvantage of using this algorithm is
problems” in Ninth International Conference on Hybrid
that coins have to be inputted in descending order.
Intelligent Systems MOE- Microsoft Laboratory for
8
International Journal of Computer Applications (0975 – 8887)
Volume 178 – No. 15, May 2019
Intelligent Computing and Intelligent Systems”, pp. 1-2, [13] Elmirap, “Coin Change”, LeetCode, Retrieved August
DOI: 10.1109/HIS.2009.103, 2009. 10, 2018 from url: https://leetcode.com/articles/coin-
change/#, May 2016.
[11] Susanna S. Epp, “Discrete Mathematics with
Applications (4 th ed.)”, p.427, ISBN-10: 0495391328, [14] Eric W. Weisstein, “Exhaustive Search” MathWorld—A
2010. wolfram Web Resource, Retrieved August 13, 2018 from
url:http://mathworld.wolfram.com/ExhaustiveSearch.htl.
[12] Edsger W. Dijkstra, “Recursive Programming”,
Numerische Mathematik, Vol. 2, Issue 1, doi: [15] Christian Charras, Thierry Lecroq, “Brute Force
10.1007/BF01386232, New Jersey, USA, pp. 312-318, algorithm”, Retrieved August 13, 2018 from url:
1960. http://www-igm.univ-mlv.fr/~lecroq/string/node3.html,
1997.
IJCATM : www.ijcaonline.org 9