Question

how to make greedy coin change algorithm optimal with backtracking ? is it possible if int...

how to make greedy coin change algorithm optimal with backtracking ?

is it possible if int requiredCoins (the minimum number of coin) is given as parameter?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

INF = 100000 #infinity

# k is number of denominations of the coin or length of d
# d is denomination vector of coin,
# n is the value for which change has to be made

def coin_change(d, n, k):
M = [0]*(n+1)
S = [0]*(n+1) # we are also assigning the value of i to the variable 'coin' because this coin 'i' is giving us the minimum value and thus, it will be the coin to be stored in the array

for j in range(1, n+1):
minimum = INF
coin = 0

for i in range(1, k+1):
if(j >= d[i]):
minimum = min(minimum, 1+M[j-d[i]])
coin = i
M[j] = minimum
S[j] = coin

l = n
while(l>0):
print(d[S[l]])
l = l-d[S[l]]
return M[n]

if __name__ == '__main__':
d = [0, 1, 2, 3]
coin_change(d, 6, 3)

Add a comment
Know the answer?
Add Answer to:
how to make greedy coin change algorithm optimal with backtracking ? is it possible if int...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show that if there were a coin worth 12 cents, the greedy algorithm using quarters, 12-cent...

    Show that if there were a coin worth 12 cents, the greedy algorithm using quarters, 12-cent coins, dimes, nickels, and pennies would not always produce change using the fewest coins possible.

  • Question 2. Use the greedy algorithm to color the graph below, ordering the vertices alphabetically. Is this coloring optimal? How do you know? Question 2. Use the greedy algorithm to color...

    Question 2. Use the greedy algorithm to color the graph below, ordering the vertices alphabetically. Is this coloring optimal? How do you know? Question 2. Use the greedy algorithm to color the graph below, ordering the vertices alphabetically. Is this coloring optimal? How do you know?

  • Suppose we want to make change for n cents, using the least number of coins of...

    Suppose we want to make change for n cents, using the least number of coins of denominations 1, 10, and 25 cents. Consider the following greedy strategy: suppose the amount left to change is m; take the largest coin that is no more than m; subtract this coin's value from m, and repeat. Either give a counter example, to prove that this algorithm can output a non-optimal solution or prove that this algorithm always outputs an optimal solution.

  • Suppose we want to make change for N cents, using the least number of coins of...

    Suppose we want to make change for N cents, using the least number of coins of denominations {1, 5, 10} cents (Different from the US currency system!). Consider the following greedy strategy: suppose the amount left to change is M, take the largest coin that is no more than M; subtract this coin's value from M, and repeat. Implement the Greedy Coin Changing Algorithm in Java. public class Coinchange {    public static int greedycoinchange(int givenvalue, int[] givencoins)    {...

  • Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume...

    Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume that the coins with which you make change are quarters, dimes, nickels and pennies. Thus you are going to set n = 4 in your program. The amount k for which you have to make change will be provided by the user and your program will return the minimum number of coins needed and also the break-up of the change in terms of the...

  • 1. Minimization and Maximization Problems Which of the following best describes the role of the greedy...

    1. Minimization and Maximization Problems Which of the following best describes the role of the greedy approach in minimization (min) and maximization (max) optimization problems? A) The greedy approach never works for min or max problems B) The greedy approach never works for min problems but may work for max problems C) The greedy approach never works for max problems but may work for min problems D) The greedy approach may work for min and max problems E) The greedy...

  • Write a java code to solve the following question using the backtracking algorithm. Given a set...

    Write a java code to solve the following question using the backtracking algorithm. Given a set of distinct integers, return all possible subsets. input: new int[] {1,2,3} output: [], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3] What to submit: 1. Your source code in a text document (15pts), 2. A screen copy showing you can run the code and the result of running the code (5 pts). 3. What is the performance of your algorithm (5 pts), give the answer,...

  • Consider the following greedy algorithm for the knapsack problem: each time we pick the item with...

    Consider the following greedy algorithm for the knapsack problem: each time we pick the item with the highest value to weight ratio to the bag. Skip items that will make the total weight exceeded the capacity of the bag. Find a counterexample to show that this approach will not work, and the result could be 100 times worse than the optimal solution. That is, construct a table of set of items with weight and values and find a bag capacity...

  • Making Change: Given coins of denominations (value) 1 = v1 < v2< … < vn, we...

    Making Change: Given coins of denominations (value) 1 = v1 < v2< … < vn, we wish to make change for an amount A using as few coins as possible. Assume that vi’s and A are integers. Since v1= 1 there will always be a solution. Formally, an algorithm for this problem should take as input:  An array V where V[i] is the value of the coin of the ith denomination.  A value A which is the amount...

  • In this question, you will test, using a backtracking algorithm, if a mouse can escape from...

    In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT