Tuesday, May 5, 2009

Think Have Cysts Throughout My Body

problem with dynamic programming coins

For the problem of dynamic programming currencies

need to create an algorithm that allows a vending machine to give change using the fewest coins possible. By dynamic programming will solve the case in which the number of coins of each type is unlimited. In the currency problem < matriz_cambio[i][j - monedas[i-1]] + 1){

by the greedy algorithm when the number of coins is unlimited.





Description Suppose you have coins worth 1, 4 and 6 and to be repaid an amount corresponding to the value 8. Following the method of dynamic programming

is populate a table with rows for each value for coins and columns with values \u200b\u200bfrom 1 to 8. Each position (i, j) of the table shows the minimum number of coins required to repay the amount with coins j with value less than or equal to i:

____ 1 2 3 4 5 6 7 8
m1 = 1 1 2 3 4 5 6 7 8 m2 = 4 1 2 3 1 2 3 4 2 Example for position i = 2 j = 7, it takes a coin with value 4 type 2 and three coins of a value type 1, therefore the table the number of coins in position (2.7) will be 1 + 3 = 4.

Algorithm 1. For each cell of the table do: 2. If the current coin value is greater than the amount, is paid in other currencies, ie take the result of the box superior. 3. If the current coin value is less than or equal to the amount, takes the lower of:
1. Pay with other currencies, whichever results in the upper box.
2. Pay with a coin of the current type and the other with the result that was obtained by paying the actual amount which has been subtracted from the value of existing currency. 4. Take as a result the value of the last cell. Pseudocode





solve this dynamic programming problem in a pseudo
.

As input parameters the function takes C, which corresponds to the amount to be refunded and a vector M of coins, which stores the value of each type. Returns num, the number of coins needed to make the return.
fun change (C: nat, M [1 .. n] of nat) dev num: nat var

T [0 .. n] [0 .. C] of nat

begin T [0] [ 1 .. C]: = \\ infty;
T [1 .. n] [0]: = 0;
for i: = 1 to n do
for j: = 1 to C do
if M [i]> j then T [i, j]: = T [i-1, j] if T [i, j]: = min {T [i-1, j], T [i, jM [i]] + 1}
fsi
fto fto num: = T [n, C]
Ffun

 
Code Java



solve this dynamic programming problem
in Java code: public



Minima_devolucion int (cantidad_devuelta int, int [] coins)


{/ / Create the array of returns
int [] [] matriz_cambio = new int [monedas.length +1] [cantidad_devuelta +1];

/ / Fill the 1 st column of zeros
for (int i = 0; i
matriz_cambio [i] [0] = 0;
/ / The 1st row 1st column unless a high number for (int j = 1, j
matriz_cambio [0] [j] = 999999;
for (int i = 1; i for (int j = 1, j
if (coins [i-1]> j) {
 / / If the currency is higher than the amount to be refunded 
matriz_cambio [i] [j] = matriz_cambio [i-1] [ j];

} else {/ / If the currency does not exceed the amount to return

/ / Calculate what is the minimum of these two positions
int minimum = 0; / / Save the minimum

here if ( matriz_cambio [i-1] [j]
matriz_cambio minimum = [i-1] [j];} else {< monedas.length; i++)

minimum = matriz_cambio [i] [j - coins [i-1]] + 1;}

/ / Save <= cantidad_devuelta; j++)
minimum matriz_cambio [i] [j] = least;

}} return <= monedas.length ; i++)
<= cantidad_devuelta; j++){

matriz_cambio [monedas.length] [cantidad_devuelta];}





0 comments:

Post a Comment