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
Solve the following instance of the 0/1 knapsack problem by using dynamic programming approach. Where W=5...
Solve the following instance of the 0/1 knapsack problem by using dynamic programming approach. Where W=5 item weight Value 1 3 SR 40 2 2 SR 6 3 1 SR 20 4 4 SR 12
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
When using its associated dynamic-programming recurrence to compute the entries of wac(i, j), how many times...
When using its associated dynamic-programming recurrence to compute the entries of wac(i, j), how many times is entry wac(25, 32) used in order to compute other entries of wac that represent larger subproblems, assuming n = 76 keys? Explain and show work. 5b. The general form of a divide-and-conquer recurrence may be expressed as T(n) = Xr i=1 T(n/bi) + f(n), for some integer r ≥ 1 and real constants bi > 1, i = 1, . . . ,...
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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT