Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
Question
Book Icon
Chapter 16, Problem 1P

(a)

Program Plan Intro

To discuss the greedy algorithm in coin changing problem with pennies, dime, nickel, quarter and prove that greedy algorithm will give an optimal solution always.

(a)

Expert Solution
Check Mark

Explanation of Solution

To prove that the changing problem of coin has an optimal solution, take ' n ' cent and suppose, for these ' n ' cent changes, there is an optimal solution. The solution takes a coin with value ' c ' and uses ' k ' coins. If the solution of ' n-c ' cent changes problem lies within the solution of ' n ' cent problem solution, that means ' k-1 ' coins are available for ' n-c ' cents. So, take the solution for the ' n-c ' cent changing problem and make sure this should be lesser then ' k- 1 ' coins then figure out the solution for ' n ' cent problem.

While dealing with ' n ' cent problem and try to get an optimal solution by taking minimum no. of coins. Always provide the biggest denomination coin until the rest amount became zero. So, if there is the biggest denomination present at first then by applying greedy methodology changes in quarter, dime, nickel and pennies are:

Q = ⌊ n /25 ⌋ quarter that gives nQ = n mod 25 cent for change.

D = ⌊ nQ /10 ⌋ dimes that gives nD = nQ mod 10 cent for change.

K = ⌊ nD /10 ⌋ dimes that gives nK = nD mod 5 cent for change.

P = nK pennies

By seeing the greedy solution, there are few possibilities like:

  1. If n = 0; then zero coins in optimal solution.
  2. If n > 0; then get the biggest denomination with value = ' n ' , suppose it’s ' c ' .
  3. Now, take a coin and apply process in recursive manner for ' n-c ' cent.

To get the optimal solution through greedy algorithm, one must hold the coin ' c ' with value greater than ' n ' .

There are four cases needs to be considered here:

  1. If (1= n < 5) then c = 1, this must hold the greedy choice and contains pennies.
  2. If (5 = n = 10) then c = 5, by supposition, in this it must hold pennies and change 5 pennies to one nickel because this solution is not holding any nickel.
  3. If (10 = n = 25) then c = 10, by supposition, it must hold a nickel and pennies but not a dime. So, add a dime in the solution.
  4. If (25 = n ) then c = 25, by supposition, it holds a dime, pennies, and nickel but not quarter. If it has three dimes change it with a quarter and nickel and if has at least two dime then few subset of nickel, dime and pennies add nearly 25 cents and change these coins with a quarter to get a solution.

Here, for showing that it must contains an optimal solution with greedy choices and these choices will further helpful to get the solution of sub problem. Therefore, this greedy approach provides an optimal outcome for the given problem.

For an algorithm that selected a coin and recurses sub problem one by one, the running complexity will be θ ( k ), where ' k ' is the coins used for optimal solution. Now ( k = n), so, running time will be O ( n ) and there is only four type of coins. So, it requires O (1) constant time.

(b)

Program Plan Intro

To show that for given denomination ( c0, c1, c2, ........., ck ), greedy algorithm always gives an optimal solution where ( c > 1) and (1 = k ).

(b)

Expert Solution
Check Mark

Explanation of Solution

Take denomination c0, c1, c2, ........., ck and changes amount ' n ' . By taking this, it is clear that

Only few coins are required in changing coin to give an optimal solution. Apply greedy algorithm, take biggest denomination coin first means take a denomination ci (not ck ), where ( k>i ).

  1. To make sure that solution should be optimal take any denomination ( ci ) no. of coin that must be lesser then ' c ' .
  2. Take coins of ' ci ' denomination having ( x0, x1, ...., xk ) optimal solution and prove that for every ( k >i ), denomination ( c >xi ).
  3. And for few j , ( c = xi ) and change denomination coins cj with cj+1coin .
  4. Therefore, xj reduced by c, xj +1 increased by 1 and total no. of coins reduced by c -1.

This is contradicting about ( x0, x1,....., xk ) being a solution. So, that optimal solution should have denomination ' c ' greater than xi for any ' ci ' denomination.

Thus the only solution of the given problem is xk = n/ck ; for ( k>i ), xi = n mod ci +1/ ci . Therefore this is proved that greedy algorithm with ( c0, c1, c2, ........., ck ) denomination always gives an optimal solution.

(c)

Program Plan Intro

To prove that greedy algorithm does not provides the optimal solution all the time.

(c)

Expert Solution
Check Mark

Explanation of Solution

Take the denomination set as {1, 3, 4} and the value of ' n =6’.

Then greedy solution will give two one cent coin {1, 1} and a four-cent coin {4} like {1, 1, 4} but two coins as {3, 3} is optimal solution.

So, for the set {1, 3, 4}, optimal solution will not present by using greedy algorithm.

In the other example where denomination set contains value {1, 10, 25}, for ' n = 30’; greedy algorithm provide a quarter and five pennies {25, 1, 1, 1, 1, 1} but the optimal solution should be three dime like {10, 10, 10}.

Thus, greedy algorithm will not provide an optimal solution always.

(d)

Program Plan Intro

To show that the time complexity of the algorithm is O ( nk ), where ' k ' is the total no. of coins.

(d)

Expert Solution
Check Mark

Explanation of Solution

While applying the dynamic programming, take the demonstration ' d ' define as d1 , d2 , d3 ,...., dk and to make changes for ' i ' cent, no. of coins required min( c [ i ]). So, the minimum coins required can be calculated as:

c [ i ] = 1 + min{ c [ i-dj ]}, if ( i > 0) and (1 = j = k ),

c [ i ] = 0, if ( i = 0)

This formula should be implemented as,

MakeChange ( d,n )

  1. For ( i ← 1) to n
  2. Do c [ i ] ← 8
  3. For ( j ← 1) to k
  4. Do if ( dj = i )
  5. Then if c [ i ] > c [ i-dj ] + 1
  6. Then c [ i ] ← c [ i - dj ] + 1
  7. denom[ i ] ← dj
  8. Return demon and ' c '

By seeing the algorithmic procedure of MakeChange( d,n ), it is showing that it requires two loops up to ' n ' times as outer loop and ' k ' times as inner loop.

Thus, the complexity of the implementation will be O ( nk ).

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
The classic example of following a greedy algorithm is making change. Let’s say you buy some items at the store and the change from your purchase is 63 cents. How does the clerk determine the change to give you? If the clerkfollows a greedy algorithm, he or she gives you two quarters, a dime, and three pennies. That is the smallest number of coins that will equal 63 cents (given that we don’t allow fifty-cent pieces). It has been proven that an optimal solution for coin changing can always be found using the current American denominations of coins. However, if we introduce some other denomination to the mix, the greedy algorithm doesn’t produce an optimal solution.Write a program that uses a greedy algorithm to make change (this code assumes change of less than one dollar).
A. One greedy strategy to solve the knapsack problem is to consider the items in order of decreasing values of where Vi and Si are the value and the size of item i. For each item, add it to the knapsack if it fits otherwise ignore it and move to the next item in the ordered list. Write a complete pseudocode for this strategy. The pseudocode should print the details of the solution (selected items, their total value and size). B. Show that this strategy does not necessarily yield optimal solutions. Take the example of exercise 1 and change the knapsack capacity to 15 (The value of the optimal solution is 54)..
Consider a currency system that has the following coins and their values: dollar (100cents), quarter (25 cents), dime (10 cents), nickel (5 cents) and 1-cent coins. (A unit-valuecoin is always required). Suppose we want to give a change of value n cents in such a waythat the total number of coins n is minimized. Design a greedy algorithm to solve thisproblem.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education