Question

In: Computer Science

Construct the visualization matrix for the following coin problem and find its optimal solution: Coins: Penny,...

Construct the visualization matrix for the following coin problem and find its optimal solution:

Coins: Penny, Nickle, Dime, and Quarter The amount= 27cents

USE DYNAMIC PROGRAMMING METHOD

Dynamic Programming

Let c[k,x] be the minimum number of coins for the amount x using the first k coins.

         Goal: find a recurrence relation for c[k, x].

       There are only two possible choices:

           1. amount x includes the largest coin which is dk

                 c[k, x] = 1 + c[k, x – dk]

          2. amount x does not include the largest coin

c[k, x] = c[k - 1, x ]

       Among these two choices, we always pick the smallest one.

Goal: find a recurrence relation for c[k, x]

c[k, x] = MIN (1 + c[k, x – dk], c[k - 1, x ])

c[k, x] = c[k - 1, x ], if x < dk

c[k, 0] = 0

c[1, x] = x

where 1 ≤ k ≤ n and 0 ≤ x ≤ m.

Solution to the problem is c[n, m]

Example of Money Changing Problem   d1 =1    d2=4 d3=6    and the amount = 8

0

1

2

3

4

5

6

7

8

k==1

1 cent

0

1

2

3

4

5

6

7

8

k=2

4 cents

0

1

2

3

1

2

3

4

2

k=3

6 cents

0

1

2

3

1

2

3

1

2

c[k, x] = MIN (1 + c[k, x – dk], c[k - 1, x ])

c[k, x] = c[k - 1, x ], if x < dk

c[k, 0] = 0

c[1, x] = x

where 1 ≤ k ≤ n and 0 ≤ x ≤ m                The minimum number of coins= C[3,8]=2

Solutions

Expert Solution

We have

1 penny = 1 cent

1 nickel = 5 cents

1 dime = 10 cents

1 quarter = 25 cents

We will use dynamic programming to solve this problem

  

Transitions of dp are as follows :

As we have two choices either to pick last coin or don't pick it.

Here is Visualization matrix for

coins = {  Penny, Nickle, Dime, and Quarte } = { 1, 5 ,10, 25 }

We have converted each currency to cent using below formulas

1 penny = 1 cent

1 nickel = 5 cents

1 dime = 10 cents

1 quarter = 25 cents

Amount = 27 cents

Below is visulization matrix :

0 1 2 3 4 5 6 7

8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
k=1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
k=2 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7
k=3 0 1 2 3 4 1 2 3 4 5 1 2 3 4 5 2 3 4 5 6 2 3 4 5 6 3 4 5
k=4 0 1 2 3 4 1 2 3 4 5 1 2 3 4 5 2 3 4 5 6 2 3 4 5 6 1 2 3

Min no of coins to make 27 = 3

as 27 = 25 + 1 +1

Below Is code for reference

#include<bits/stdc++.h>
using namespace std;
void coin_change( int coins[], int total_coins, int Amount )
{
    int Max = Amount + 1;
    int dp[total_coins + 1][Amount + 1];
    for (int i = 0; i <= Amount; i++) dp[0][i] = INT_MAX - 1;
    for (int i = 1; i <= total_coins; i++) dp[i][0] = 0;

    for (int i = 1; i < total_coins + 1; i++)
        for (int j = 1; j < Amount + 1; j++)
            if (coins[i - 1] > j)
                dp[i][j] = dp[i - 1][j];
            else
                dp[i][j] = min(1 + dp[i][j - coins[i - 1]], dp[i - 1][j]);

    cout << "       ";
    for (int i = 0; i <= Amount; i++)  cout << i << " "; cout << "\n";

    for (int i = 1; i <= total_coins; i++)  {
        cout << "K = " << i << "| ";
        for (int j = 0; j <= Amount; j++)
            cout << dp[i][j] << " ";
        cout << "\n";
    }
    cout << "\nMin coins = " << dp[total_coins][Amount] << "\n"; 

}
int main()
{   int total_coins, Amount;
    cin >> total_coins >> Amount;
    int coins[total_coins];
    for (int i = 0; i < total_coins; i++) cin >> coins[i];
    coin_change(coins, total_coins, Amount);
    return 0;
}

Below is screenshot of code along with output


Related Solutions

Find an optimal solution to the following transportation problem. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Copy to Clipboard + ?? To...
Find an optimal solution to the following transportation problem. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Copy to Clipboard + ?? To From A B C Supply X ?$10 ?$19 ?$12 150 Y ?$17 ?$13 ?$9 100 Z ?$20 ?$18 ?$14 50 Demand 80 120 60 Based on the given demand and? supply, the given transportation problem is ? balanced unbalanced .Before finding the initial? solution, a dummy ? supplier destination should be introduced.The total cost of the optimal solution? = ?$
you have a purse which contains six coins, each coin is either a penny , a...
you have a purse which contains six coins, each coin is either a penny , a two pence coin, a ten pence coin or a twenty pence coin. find the expected total value of all the coins in the purse.
Use the graphical method for linear programming to find the optimal solution for the following problem....
Use the graphical method for linear programming to find the optimal solution for the following problem. Maximize P = 4x + 5 y subject to 2x + 4y ≤ 12                 5x + 2y ≤ 10 and      x ≥ 0, y ≥ 0. graph the feasible region
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Minimize...
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Minimize C = 5x + 11y subject to 4x + 6y ≥ 13 7x + 4y ≥ 13 and x ≥ 0, y ≥ 0. What is the optimal value of x? What is the optimal value of y? What is the minimum value of the objective function?
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Minimize...
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Minimize C = 17x + 19y + 8z subject to 12x + 14y + 21z ≥ 58 11x + 6y + 9z ≥ 116 and x ≥ 0, y ≥ 0, z ≥ 0. What is the optimal value of x? What is the optimal value of y? What is the optimal value of z? What is the minimum value of the objective function?
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Maximize...
Find the optimal solution for the following problem. (Round your answers to 3 decimal places.) Maximize C = 16x + 21y subject to 9x + 15y ≤ 22 10x + 3y ≤ 29 and x ≥ 0, y ≥ 0. What is the optimal value of x? What is the optimal value of y? What is the maximum value of the objective function?
To find the optimal solution to a linear optimization problem, do you have to examine all...
To find the optimal solution to a linear optimization problem, do you have to examine all the points in the feasible region? Explain. Can a linear programming problem have no solution? More than one solution? Explain. ---------------------------------------------------------------------------------------------------------------- A beverage can manufacturer makes three sizes of soft drink cans—Small, Medium and Large. Production is limited by machine availability, with a combined maximum of 90 production hours per day, and the daily supply of metal, no more than 120 kg per day....
For the following LP problem, determine the optimal solution by the graphical solution method. Min Z=...
For the following LP problem, determine the optimal solution by the graphical solution method. Min Z= 3x1+2x2 Subject to 2x1+x2 >10                    -3x1+2x2 < 6                      X1+x2 > 6                      X1,x1 > 0 Graph and shade the feasible region
For the following linear programming problem, determine the optimal solution by the graphical solution method Max...
For the following linear programming problem, determine the optimal solution by the graphical solution method Max -x + 2y s.t. 6x - 2y <= 3 -2x + 3y <= 6     x +   y <= 3         x, y >= 0
Find the complete optimal solution to this linear programming problem. Min 5x + 6y s.t. 3x...
Find the complete optimal solution to this linear programming problem. Min 5x + 6y s.t. 3x +   y >= 15 x + 2y >= 12 3x + 2y >= 24      x , y >= 0 PLEASE NOTE I AM USING EXCEL FOR THIS QUESTION PLEASE SHOW ALL WORK AND FORMULAS
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT