0% found this document useful (0 votes)
205 views15 pages

Coin Change Problem

Uploaded by

unique me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views15 pages

Coin Change Problem

Uploaded by

unique me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

10/26/2019 Coin Change Problem Using Dynamic Programming

(/)

Ad closed by

Stop seeing this ad

Why this ad?


Introduction (/course/algorithms-introduction/)

Analyze Your Algorithm (/course/algorithms-analyze-your-algorithm/)

Growth of a Function (/course/algorithms-growth-of-a-function/)

Analyze Iterative Algorithms (/course/algorithms-analyze-iterative-algorithms/)

Recurrences (/course/algorithms-recurrences/)

Let's Iterate (/course/algorithms-lets-iterate/)

Now the Recursion (/course/algorithms-now-the-recursion/)

Master's Theorem (/course/algorithms-masters-theorem/)

Sort It Out (/course/algorithms-sort-it-out/)

Bubble Sort (/course/algorithms-bubble-sort/)

Count and Sort (/course/algorithms-count-and-sort/)

Heapsort (/course/algorithms-heapsort/)

Divide and Conquer (/course/algorithms-divide-and-conquer/)

Merge Sort (/course/algorithms-merge-sort/)

QuickSort (/course/algorithms-quicksort/)

Binary Search (/course/algorithms-binary-search/)

Dynamic Programming (/course/algorithms-dynamic-programming/)

Knapsack Problem (/course/algorithms-knapsack-problem/)

Rod Cutting (/course/algorithms-rod-cutting/)

Coin Change (/course/algorithms-coin-change/)

Backtracking (/course/algorithms-backtracking/)

Knight's Tour Problem (/course/algorithms-knights-tour-problem/)

Greedy Algorithm (/course/algorithms-greedy-algorithm/)

Activity Selection (/course/algorithms-activity-selection/)

Egyptian Fraction (/course/algorithms-egyptian-fraction/)

Huffman Codes (/course/algorithms-huffman-codes/)



Graphs (/course/algorithms-graphs/)

https://www.codesdope.com/course/algorithms-coin-change/ 1/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
BFS (/course/algorithms-bfs/)

DFS (/course/algorithms-dfs/)

Minimum Spanning Tree (/course/algorithms-minimum-spanning-tree/)

Prim's Algorithm (/course/algorithms-prims-algorithm/)


https://www.codesdope.com/course/algorithms-coin-change/ 2/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest

Coin Change Problem | Dynamic Programming


Coin change problem is the last algorithm we are going to discuss in this section of dynamic programming
(/course/algorithms-dynamic-programming/). In the coin change problem, we are basically provided with coins with
different denominations like 1¢, 5¢ and 10¢. Now, we have to make an amount by using these coins such that a minimum
number of coins are used.

Let's take a case of making 10¢ using these coins, we can do it in the following ways:

1. Using 1 coin of 10¢


2. Using two coins of 5¢
3. Using one coin of 5¢ and 5 coins of 1¢
4. Using 10 coins of 1¢

Out of these 4 ways of making 10¢, we can see that the first way of using only one coin of 10¢ requires the least number
of coins and thus it is our solution.

So in a coin change problem, we are provided with different denominations of coins:

1 = d1 < d2 < d3 <. . . < dk

d1 = 1 ensures that we can make any amount using these coins.

Now, we have to make change for the value n 


using these coins and we need to find out the minimum number of coins
required to make this change.

https://www.codesdope.com/course/algorithms-coin-change/ 3/15
10/26/2019 Coin Change Problem Using Dynamic Programming

Approach to Solve the Coin Change Problem


(/add_quest

Like the rod cutting problem (/course/algorithms-rod-cutting/), coin change problem also has the property of the optimal
substructure i.e., the optimal solution of a problem incorporates the optimal solution to the subproblems. For example, we
are making an optimal solution for an amount of 8 by using two values - 5 and 3. So, the optimal solution will be the
solution in which 5 and 3 are also optimally made, otherwise, we can reduce the total number of coins of optimizing the
values of 5 and 8.

The reason we are checking if the problem has optimal substructure or not because if there is optimal substructure, then
the chances are quite high that using dynamic programming will optimize the problem.

Let's say Mn is the minimum number of coins needed to make the change for the value n.

Let's start by picking up the first coin i.e., the coin with the value d1 . So, we now need to make the value of n − d1 and
Mn−d
1
is the minimum number of coins needed for this purpose. So, the total number of coins needed are 1 + Mn−d
1
(1
coin because we already picked the coin with value d1 and Mn−d
1
is the minimum number of coins needed to make the
rest of the value).

Similarly, we can pick the second coin first and then attempt to get the optimal solution for the value of n − d2 which will
require Mn−d
2
coins and thus a total of 1 + Mn−d
2
.

We can repeat the process with all the k coins and then the minimum value of all these will be our answer. i.e.,
mini:d
i ≤n
{Mn−d + 1}
i
.

The above process can also be understood in a different way. Suppose, we have picked a coin with value x and we know
that this coin is going to be in the solution. So, our next task is to find the minimum number of coins needed to make the
change of value n-x i.e., Mn−x . Also, by choosing the coin with value x, we have already increased the total number of
coins needed by 1. So, we can write:

Mn = 1 + Mn−x


https://www.codesdope.com/course/algorithms-coin-change/ 4/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest

But the real problem is that we don't know the value of x. So, we will try every coin for the value of x and then we will
select the minimum among those.

mini:d {Mn−d + 1}, if n > 0


i ≤n i

Mn = {

0, if n = 0

You can see that there are overlapping subproblems in our solution and thus, we can use dynamic programming to
optimize this. So, let's look at the coding implementation of the formula we derived above.

Code for Coin Change Problem

We are going to use the bottom-up implementation of the dynamic programming to the code.

Our function is going to need the denomination vectors of coin (d), the value for which change has to be made (n) and
number of denominations we have (k or number of elements in the array d) i.e., COIN-CHANGE(d, n, k)

Let's start by making an array to store the minimum number of coins needed for each value i.e., M[n+1] .

We know that for a value of 0, no coins are needed at all.

M[0] = 0

Now, we need to iterate for each value in array the M and fill it up. Also to store the minimum number of coins for each
value, we will make a variable and initialize it with ∞ so that all other values are less than it in the starting. It is similar to
the variable 'maximum_revenue' we made in the previous chapter.

for j in 1 to n
minimum = INF 
https://www.codesdope.com/course/algorithms-coin-change/ 5/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
Now for each j, we need to choose the minimum of Mn−d + 1
i
, where di will change from d1 to dk and thus, i will
change from 1 to k. Also, if the value of di is greater than j (value to be made), then of course, we can't use that coin.

for j in 1 to n
minimum = INF
for i in 1 to k
if j >= d[i]
minimum = min(minimum, 1 + M[j-d[i]])

for i in 1 to n → We are iterating to change the value of di from d1 to dk .

if j >= d[i] → If the value to be made (j) is greater than the value of the current coin (d[i]), only then we can use this
coin.

minimum = min(minimum, 1 + M[j-d[i]]) → If the current value of M[j-d[i]] (or Mj−d


i
) is less than the current minimum,
then we are changing the value of the 'minimum'.

Now we just need to change the value of the array M to the calculated minimum and return it.

for j in 1 to n
minimum = INF
for i in 1 to k
...
M[j] = minimum
return M[n]

COIN-CHANGE(d, n, k)
M[n+1]
M[0] = 0

for j in 1 to n
minimum = INF

for i in 1 to k
if j >= d[i]
minimum = min(minimum, 1+M[j-d[i]])

M[j] = minimum
return M[n]

C Python Java


https://www.codesdope.com/course/algorithms-coin-change/ 6/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
#include <stdio.h>
#define MIN(x, y) (((x) < (y)) ? (x) : (y))

const int INF = 100000;

//k is number of denominations of the coin or length of d


int coin_change(int d[], int n, int k) {
int M[n+1];
M[0] = 0;

int i, j;
for(j=1; j<=n; j++) {
int minimum = INF;

for(i=1; i<=k; i++) {


if(j >= d[i]) {
minimum = MIN(minimum, 1+M[j-d[i]]);
}
}
M[j] = minimum;
}
return M[n];
}

int main() {
// array starting from 1, element at index 0 is fake
int d[] = {0, 1, 2, 3};
printf("%d\n", coin_change(d, 5, 3)); //to make 5. Number of denominations = 3
return 0;
}

Coins in Optimal Solution

Let's suppose we know the first coin needed to get the optimal solution of each value. Now, we can subtract the value of
the first coin from n and then we will have a different value to be made.

As discussed above, the problem exhibits optimal substructure and we know the first coin needed to make this new value,
so we know the second coin needed to make the value n .

We can repeat this process to get all the coins needed.

So, the point is we only need to store the first coin needed for each value and then we can get the optimal coins for any
value.

Thus, we will modify the above code and store the first coins of each value in an array S.


https://www.codesdope.com/course/algorithms-coin-change/ 7/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
COIN-CHANGE-MODIFIED(d, n, k)
M[n+1]
M[0] = 0

S[n+1]
S[0] = 0

for j in 1 to n
minimum = INF

for i in 1 to k
if j >= d[i]
if 1+M[j-d[i]] < minimum
minimum = 1+M[j-d[i]]
coin = i

M[j] = minimum
S[j] = coin

//Code to print the coins, given below


...
return M[n]

S[n+1] → We are declaring the array S.

We have just broken the 'min' function from the above code minimum = min(minimum, 1+M[j-d[i]]) to if 1+M[j-d[i]] <
minimum → minimum = 1+M[j-d[i]] . In this case, we are also assigning the value of i to the variable 'coin' because this coin
'i' is giving us the minimum value and thus, it will be the coin to be stored in the array S.

At last, we are storing this value in the array S S[j] = coin .


https://www.codesdope.com/course/algorithms-coin-change/ 8/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
COIN-CHANGE-MODIFIED(d, n, k)
M[n+1]
M[0] = 0

S[n+1]
S[0] = 0

for j in 1 to n
minimum = INF

for i in 1 to k
if j >= d[i]
if 1+M[j-d[i]] < minimum
minimum = 1+M[j-d[i]]
coin = i

M[j] = minimum
S[j] = coin

//Code to print the coins


l = n
while l>0
print d[S[l]]
l = l-d[S[l]]

return M[n]

C Python Java


https://www.codesdope.com/course/algorithms-coin-change/ 9/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest
#include <stdio.h>

const int INF = 100000;

//k is number of denominations of the coin or length of d


int coin_change_modified(int d[], int n, int k) {
int M[n+1];
M[0] = 0;

int S[n+1];
S[0] = 0;

int i, j;
for(j=1; j<=n; j++) {
int minimum = INF;
int coin=0;

for(i=1; i<=k; i++) {


if(j >= d[i]) {
if((1+M[j-d[i]]) < minimum) {
minimum = 1+M[j-d[i]];
coin = i;
}
}
}
M[j] = minimum;
S[j] = coin;
}

int l = n;
while(l>0) {
printf("%d\n",d[S[l]]);
l = l-d[S[l]];
}
return M[n];
}

int main() {
// array starting from 1, element at index 0 is fake
int d[] = {0, 1, 2, 3};
coin_change_modified(d, 5, 3);
return 0;
}

As said above, we are first printing the value of the coin (S[l] will give us the coin and d[S[l]] is the value of that coin).
After this, our initial value has decreased by the value of the coin i.e., l = l-d[S[l]] and we are doing this until we the
value is greater than 0 while l>0 .

Analysis of the Algorithm

The first loop is iterating from 1 to n and thus has a running time of Θ(n) . The last loop will also run a total of n times in
worst case (it can run faster depending upon the value of l-d[S[l]] ). Thus it has a running time of O(n) .

Now, there is also a nested loop. The first loop if iterating from 1 to n and the second is iterating from 1 to k and thus, a
total running time of Θ(nk) . All the other statements are constant time taking statements. Thus the algorithm has a
running time of Θ(nk) .

There are many other interesting algorithms which are optimized by dynamic programming. You can write about them on
BlogsDope (https://www.codesdope.com/blog/) or can download the BlogsDope app
(https://play.google.com/store/apps/details?id=com.blogsdope) to never miss any new articles.


https://www.codesdope.com/course/algorithms-coin-change/ 10/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest

 Leaders live by choice, not by accident. 


- Mark Gorman

PREV (/course/algorithms-rod-cutting/) (/course/algorithms-backtracking/) NEXT


https://www.codesdope.com/course/algorithms-coin-change/ 11/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest

Download Our App.

(https://play.google.com/store/apps/details?
id=com.blogsdope&pcampaignid=MKT-
Other-global-all-co-prtnr-py-
PartBadge-Mar2515-1)

New Questions

for loop topic questions and


answers - Python

(/discussion/for-loop-topic-
questions-and-answers)

Medlearnity - Other

(/discussion/medlearnity)

Welcome to
irononpatchesinc.com - Other

(/discussion/welcome-to-
irononpatchesinccom)

Can anyone explain how to


analyze an algorithm??
- Algorithms
(/discussion/can-
anyone-explain-how-to-analyze-an-
algorithm)

can i return something inside a


constractor? - Java

(/discussion/can-i-return-something-
inside-a-constractor)

Write a program to find i^th


largest term in a given array. - Cpp

(/discussion/write-a-program-to-find-
ith-largest-term-in-a-give)

Compiling in mac mojave - Cpp

(/discussion/compiling-in-mac-
mojave)
A s k Yo u r s

(/add_question/)


https://www.codesdope.com/course/algorithms-coin-change/ 12/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/add_quest

  
(/discussion/)
 (/media/pdf/ASCII.pdf)

(/practice/)

  
(https://www.facebook.com/codesdope/)
(https://twitter.com/CodesDope)
(https://www.linkedin.com/company/codesdope)

Select Language ▼

Recent Posts

Beginning with ML 2.0:


Multivariate Linear
Regression
(/blog/article/beginning-with-ml-
20-multivariate-linear-regressio/)

Beginning with Machine


Learning: Linear Regression 
https://www.codesdope.com/course/algorithms-coin-change/ 13/15
10/26/2019 Coin Change Problem Using Dynamic Programming
(/blog/article/beginning-with- (/add_quest
machine-learning-linear-
regression/)

Scrolling Progress Indicator


(/blog/article/scrolling-progress-
indicator/)

Getting Notebook Paper


Effect with CSS
(/blog/article/getting-notebook-
paper-effect-with-css/)
P o s t Yo u r s

(/blog/submit-article/)

FOLLOW US

 (https://www.facebook.com/codesdope)  (https://twitter.com/codesdope) 
(https://www.pinterest.com/codesdope/)  (https://www.linkedin.com/company/codesdope)
 (https://plus.google.com/b/103889423774256807287/103889423774256807287/posts)

CATEGORIES

ABOUT (/about/)
BLOG (/blog/)
COURSES (/)
PRACTICE (/practice/)
DISCUSSION (/discussion/) 
CONTACT US (/contact-us/)

https://www.codesdope.com/course/algorithms-coin-change/ 14/15
10/26/2019 Coin Change Problem Using Dynamic Programming
TERMS OF USE (/terms-of-use/) (/add_quest
PRIVACY POLICY (/privacy-policy/)

KEEP IN TOUCH

[email protected] (mailto:[email protected])

© 2016-19 | www. codesdope.com (/) | All rights reserved.


https://www.codesdope.com/course/algorithms-coin-change/ 15/15

You might also like