1

I am trying to solve a problem using dynamic programming and the problem goes as follows:

Given an unlimited supply of coins (penny, nickel, dime, double-dime, quarter) with values (1, 5, 10, 20, 25), please find the smallest number of coins to make change for 65 cents. What coins (and how many of each) are used? Illustrate the table(s) required by using the dynamic programming algorithm and how you get what coins are used.

Note I do not expect anyone to illustrate the entire table for me, but I am kind of stuck on how I fill in the table for this problem.

I know my table would look a little like this:

    5   10  15  20  25  30  35  40  45  50  55  60  65
1

5

10

20

25

( I am omitting the 1's because I know that is not the best solution) My initial thought is that the table would be filled out a little like this:

    5   10   15  20  25  30  35  40  45  50  55  60  65
1

5   1    2   4   5    5   6  7   8    9  10  11  12  13

10  0    1   

20

25

I get stuck here when I have to go further. I do not think I am understanding how dynamic programming works for this problem completely. I have been reading my book, and reading online but I am still a bit confused.

EDIT:

Thanks to one of the answers this is how I worked out the solution:

    5     10    15    20    25    30    35    40    45    50    55    60    65
1   

5   1           1                  1                             1           

10         1    1                       1                              1     

20                    1                       2     1                        2

25                          1      1    1           1      2     2     2     1
Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
mufc
  • 695
  • 3
  • 16
  • 31

2 Answers2

1

You are doing it wrong. The columns represent the total change you have to return, and the row cells represent the number of certain coins (penny, nickel, dime, double-dime, quarter) used.

The whole point of that algorithm is to return the minimal number of coins. For example if the change is 25, you should return a single quarter, and not 25 penny's. You can see that i used one quarter in the table below for the 25 cents column.

In your example in the column for 15 change you are using 4 x 5cents, which is suboptimal, because you could have used one coin of 10 cents and one coin of 5 to return a total of 15. In the 20 cents column you are using 5 x 5 cents change which is incorrect and again not optimal, because you could have used one coin of 20 cents to return 20 cents.

Here is a table filled for the first 5 columns. You can fill the rest:

    5   10   15  20  25  30  35  40  45  50  55  60  65
1

5   1        1     

10      1    1  

20               1

25                   1
--------------------------------------------------------
T   1   1    2   1   1

I have added a T row on the bottom to count the total number of coins you used as change. Your goal is to have the min. number possible in this row for each column.

Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
  • Ah thank you, I understand now. I am going to edit my answer just to make sure I have the correct answer. but for 65 I get the 2twentyfive, 1ten, and 1five – mufc Oct 29 '14 at 22:13
  • 1
    What if the target amount is 67? There need to be columns for every amount of money, not just multiples of 5. Furthermore, the answer two 25's, one 10, 1 five is **not optimal**. One 25, two 20's is optimal. – Timothy Shields Oct 29 '14 at 22:36
  • @Timothy Shields, you are correct about the optimal number of coins for 65, but i have not mentioned the solution for 65 anywhere in my answer and i don't see why you think it is wrong. I have just pointed him in the right direction to fill the table. – Faris Zacina Oct 29 '14 at 22:47
  • Regarding the columns, they are arbitrary and they were provided by the user. You can set any column amount and the logic to fill it in would still work. It would still work for 67 = 1x25 + 2x20 + 2x1 – Faris Zacina Oct 29 '14 at 23:04
0

Still using dynamic programming, I would instead model the problem as constructing a graph in which nodes are amounts of money (node N is N cents) and in which there are 5 types of directed edges, {1, 5, 10, 20, 25}, corresponding to the coin types.

Keep a running frontier of nodes for which an optimal solution has not yet been found. Each iteration, the smallest node on the frontier must be optimal, so it can be removed, adding up to 5 new nodes onto the frontier.

Here is Python for the algorithm:

def change(coins, target):
    nodes = {0: (0, None)}
    frontier = set([0])
    while True:
        n = min(frontier)
        frontier.remove(n)
        if n == target:
            break
        elif n > target:
            return None # Infeasible!
        count = nodes[n][0]
        for coin in coins:
            m = n + coin
            frontier.add(m)
            if not m in nodes or nodes[m][0] > count + 1:
                nodes[m] = (count + 1, n)
    m = target
    sol = {}
    while True:
        n = nodes[m][1]
        if n is None:
            break
        coin = m - n
        sol[coin] = sol.get(coin, 0) + 1
        m = n
    return sol

print change([1, 5, 10, 20, 25], 65)

The output is {25: 1, 20: 2}.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • While your answer is probably useful, it doesn't answer his question. He wasn't asking how to resolve the problem most efficiently or an alternative way to resolving his problem, but he specifically asked to explain the dynamic programming problem and he asked for help to fill the table :) – Faris Zacina Oct 29 '14 at 22:58