Problem 3. (15 marks) Suppose you are a teller and the crecy in your country is
ID: 3706838 • Letter: P
Question
Problem 3. (15 marks) Suppose you are a teller and the crecy in your country is arranged in coins of increasing C values (each coin has a value of a power of C for some integer C>1), .e., you have coins of le, Ce, C2e, up to some k 1. Your customer wants you to make change for Ne (N is a positive integer) using the minimum number of coins. Propose an efficient greedy algorithm in pseudocode which returns k+1 numbers no, n1,n2,,nk such that (i) N ?? on;C" and (ii) the total number of coins used ?? o algorithm is guaranteed to always return the optimal solution, that is, condition is always satisfied. (Hint: Prove the two properties, subproblem optimality and substitution property, required for a greedy algorithm to work.) ni, is minimized. Prove that yourExplanation / Answer
Answer
given by
The aim is to uncover the largest promising value and subtract it from beloved value.
After that,
we include to find the major probable denomination and take away it from the left over value.
Repeat this development gain and yet again till you find the greatest possible value which is equal to the lingering value (the values of previous answer).
The algorithm is greedy since at every step, we are sentence optimum pick at each stage.
For pattern we have 1, 10, 100,1000 cents and we have to a change for 120 cents.
By the algorithm we are discuss we will lessen 120 to 20 by deduct 100 cents.
Now, repeat this step tiil we canister find another remaining value.
now, the maximum possible value is 10.So 20 is reduced by 10 and repeat the similar process again. Now,the utmost possible denomination is 10 which is equal to residual value 10 and for this reason the algorithm is frequent.
The chain of steps for the dilemma can be in black and white as:
A )Initialize an array which be supposed to be empty - Change
B). Find the prime value X which isless than or the same to N
C). cut N by (N-X), add X into selection Change
D). If N = 0, crack
E). Else the duplicate ladder from 2 to 4
At the end of algorithm, the selection Change resolve have denominations which adds up to N.
Proof:
If X=N then we encompass found the smallest amount amount number of coins which happens to be 1.
If X< N, then we look again for least valueof X' such that X' <= N' (N' = N-X).
therefore for every iteration, we are incrementing the value by 1,the least amount positive integer.
so the algorithm will alway return the smallest amount figure of coins.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.