Question

Dynamic programming Make the table for making $12 by using the denominations $1, $4, and $10....

Dynamic programming

Make the table for making $12 by using the denominations $1, $4, and $10. Show the traceback for the optimal set of the money used.

Homework Answers

Answer #1

This problem is part of a problem called Minimum Coins Needed, in this problem we are given a target number and a set of valid coins to use, and we need to find the minimum number of coins from these available coins such that their sum will give the target number, provided we can use these coins in any number.

So, before understanding traceback, we'll understand how to fill the table using dynamic programming.

So, the idea here is to find minimum coins for smaller targets for given coins and then solve for the given target.

eg, first we'll find the minimum number of coins needed to get the target of $0 to $12, given that coin available is of $1

then find the minimum coins needed to get a target of $0 to $12, given that coins available is $1 and $4

then minimum coins needed to get the target of $0 to $12, when coins available are all three i.e $1, $4, $10

$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12
$1
$4
$10

The column contains the target to achieve, and Row contains coins available, therefore, there is an array in which coins[0] = 1, coins[1] = 4, coins[2] = 10

Since we need 0 of any denomination to get a target of 0, therefore, the first column is all 0,

And since we need 1*target to get the minimum number of coins needed to achieve the target if only 1 rupee coin is available, therefore,

$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12
$1 0 1 2 3 4 5 6 7 8 9 10 11 12
$4 0
$10 0

Now, we know that we cannot fulfill the target if the target < coins available, for eg, we cannot achieve $3 using $4 coin, therefore, we have to use $1 coins instead for that, and also to note that a coin will only be used if the target is >= coin

So, to proceed with this, we need to fill the table with the minimum possible coins used from all available coins, and to achieve this the recurrence relation is:

if j >= coins[i]:   // if target at column j is greater than coin available
     
    // minimum of if only till previous coins is considered and till the current coin is considered

    dpTable[i][j] = minimum(dpTable[i-1][j], 1 + dpTable[i][j-coins[i]]

else:
   
    // if target at column j is less than coin available, then we cannot use the current coin
    // and we have to use minimum coins needed till previous coins considered.

    dpTable[i][j] = dpTable[i-1][j]

Therefore, the table after filling using the above relation is:

$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12
$1 0 1 2 3 4 5 6 7 8 9 10 11 12
$4 0 1 2 3 1 2 3 4 2 3 4 5 3
$10 0 1 2 3 1 2 3 4 2 3 1 2 3

And the answer is dpTable[ 2 ][ 12 ] = 3, i.e we need minimum three coins to get $12 from $1, $4, $10

Now, we'll want to know, what are those three coins i.e traceback:

We start the traceback from LastRow = 2, LastCol= 12 (i.e last filled index)

While LastCol != 0:

    if dpTable[ LastRow ][ LastCol]  == dpTable[ LastRow - 1][LastCol] :
          LastRow = LastRow - 1  
   
    else:
          Money_used.insert(coins[LastRow])
          LastCol = LastCol - coins[LastRow]

So, with above algorithm, we start from lastRow = 2 and LastCol = 12

And we can observe that

dpTable[2][12] == dpTable[1][12] == 3, therefore, decrement LastRow by 1 therfore LastRow = 1

Now LastRow = 1, LastCol = 12

dpTable[1][12] is not equal to dpTable[0][12], therefore, insert the coin at LastRow, i.e 4 into money used and decrement the LastCol by 4(coins[LastRow]) = 8

Now LastRow = 1, LastCol = 8

dpTable[1][8] is not equal to dpTable[0][8], therefore, insert the coin at LastRow, i.e 4 into money used and decrement the LastCol by 4(coins[LastRow]) = 4

Now, LastRow = 1, LastCol = 4

dpTable[1][4] is not equal to dpTable[0][4], therefore, insert the coin at LastRow, i.e 4 into money used and decrement the LastCol by 4(coins[LastRow]) = 0

Now, LastRow = 1, LastCol = 0, loop break, program over.

Money_used contains = 4, 4, 4

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a) Find the longest common subsequent of these two strings using dynamic programming. Draw the table...
a) Find the longest common subsequent of these two strings using dynamic programming. Draw the table and the formula. dbdcba dbbca b) Explain master's theorem and all its cases. Then, find the order of these recurrence relationships. 1. T(n) = 2nT(n/2) + nn 2. T(n) = 7T(n/3) + n2
Calculate the cost of inserting 12 elements in the dynamic array using tight strategy, where the...
Calculate the cost of inserting 12 elements in the dynamic array using tight strategy, where the initial array size is 3 and the cost of each operation is 1 unit. Consider the constant "c" used to increase the array size is 3?
Xi 1 2 3 4 Yi 12 10 2 1 (a) Make a scatter plot of...
Xi 1 2 3 4 Yi 12 10 2 1 (a) Make a scatter plot of the four data points (b) Compute the least-squares linear regression (c) Plot the regression line over your scatter plot Show the values of b0 and b1
Table 5-4 Price Total Revenue $10 $100 $12 $108 $14 $112 $16 $112 Table 5-4 Price...
Table 5-4 Price Total Revenue $10 $100 $12 $108 $14 $112 $16 $112 Table 5-4 Price Total Revenue $10 $100 $12 $108 $14 $112 $16 $112 Refer to Table 5-4. As price rises from $10 to $12, the price elasticity of demand using the midpoint method is approximately
1. Consider the following linear programming problem formulated by a team of business analysts at the...
1. Consider the following linear programming problem formulated by a team of business analysts at the JORDANA Company Inc. Max 3A+4B s.t. -1A + 2B ≤ 8 Constraint 1 1A +2B ≤ 12 Constraint 2 2A + 1B ≤ 16 Constraint 3 (a) Show the feasible region using the geometric or graphical approach. (b) What are the optimal values of the decision variables? (c) Find the optimal solution to this optimization problem.
Maria and Jack spend their free time making chairs and tables. Maria can make 1 table...
Maria and Jack spend their free time making chairs and tables. Maria can make 1 table in 3 hours, and 1 chair in 2 hours, whereas Jack can make 1 table in 7 hours, and 1 chair in 5 hours. Which of the following exchanges would be beneficial for both of them? a. Jack gives Maria 5 tables in exchange for 3 chairs b. Jack gives Maria 1 chair in exchange for 2 tables c. Jack gives Maria 7 chairs...
R Programming: a.Choose 10 random values of X having the Normal(4, 1) distribution. Use t.test to...
R Programming: a.Choose 10 random values of X having the Normal(4, 1) distribution. Use t.test to compute the 95% confidence interval for the mean. Is 4 in your confidence interval? b.Replicate the experiment in part a 10,000 times and compute the percentage of times the population mean 4 was included in the confidence interval. c.Repeat part b, except sample your 10 values when X has the Exponential(1/4) distribution. What is the mean of X? What percentage of times did the...
Problem #2 1) Conduct an independent-measures t-test using the following data set: Group 1: 12 10...
Problem #2 1) Conduct an independent-measures t-test using the following data set: Group 1: 12 10 15 12 4 16 6 10 8 Group 2: 8 5 9 0 6 5 0 1 - 2) Determine the critical values (for an alpha of .05) that you should use to evaluate this t-score.
Make a table of values for the following problem using multiples of π/4 for x. (Round...
Make a table of values for the following problem using multiples of π/4 for x. (Round the answers to two decimal places.) Then use the entries in the table to choose the graph of the following function for x between 0 and 2π. y=sin(x)
1.For the following linear programming problem Max 5X + 7Y s.t. 1X+ 1Y≤ 6 3X +1Y...
1.For the following linear programming problem Max 5X + 7Y s.t. 1X+ 1Y≤ 6 3X +1Y ≤ 12 X+ 2Y ≤ 10 X, Y ≥ 0 a)Write the problem in standard form. b)Solve the problem neatly using the graphical solution procedure(on paper). c)What are the values of the three slack variables at the optimal solution? d)Solve the problem with Microsoft Excel and attach your “own” printout.