Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

The Coin Changing problem The Coin Changing problem, Study Guides, Projects, Research of Computer Science

Optimal solution for 67 has six coins: two quarters, one dime, a nickel, and two pennies. • We can use a greedy algorithm to solve this problem: repeatedly ...

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 09/12/2022

kataelin
kataelin 🇬🇧

4.7

(9)

221 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The Coin Changing problem
The Coin Changing problem
Suppose we need to make change for 67
¢
. We want to do
this using the fewest number of coins possible. Pennies,
nickels, dimes and quarters are available.
Optimal solution for 67
¢
has six coins: two quarters, one
dime, a nickel, and two pennies.
We can use a greedy algorithm to solve this problem:
repeatedly choose the largest coin less than or equal to the
remaining sum, until the desired sum is obtained.
This is how millions of people make change every day (*).
'
&
$
%
CS404/504 Computer Science
1
Design and Analysis of Algorithms: Lecture 19
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download The Coin Changing problem The Coin Changing problem and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

The Coin Changing problem

The Coin Changing problem

Suppose we need to make change for 67

. We want to do

nickels, dimes and quarters are available.this using the fewest number of coins possible. Pennies,

Optimal solution for 67

has six coins: two quarters, one

dime, a nickel, and two pennies.

We can use a

greedy algorithm

to solve this problem:

remaining sum, until the desired sum is obtained.repeatedly choose the largest coin less than or equal to the

This is how millions of people make change every day (*).

CS404/

Computer Science

1

Design and Analysis of Algorithms: Lecture 19

The Coin-Changing problem:

formal

description

The Coin-Changing problem:

formal

description

Let

D

d

1 , d

2

, ..., d

k

}

be a finite set of distinct coin

denominations. Example:

d

1

d

2

d

3

, and

d

4

We can assume each

d

i

is an integer and

d

1

d

2

... > d

k .

Each denomination is available in unlimited quantity.

The Coin-Changing problem:

Make change for

n

cents, using a minimum total number

of coins.

Assume that

d k

= 1 so that there is always a solution.

CS404/

Computer Science

2

Design and Analysis of Algorithms: Lecture 19

A Dynamic Programming Solution:

Step (i)

A Dynamic Programming Solution:

Step (i)

Step (i):

Characterize the structure of a coin-change solution

Define

C

[

j ] to be the minimum number of coins we need to

make change for

j

cents.

making change forIf we knew that an optimal solution for the problem of

j

cents used a coin of denomination

d

i ,

we would have:

C

[

j ] = 1 +

C [ j − d i

].

CS404/

Computer Science

4

Design and Analysis of Algorithms: Lecture 19

A Dynamic Programming Solution:

Step (ii)

A Dynamic Programming Solution:

Step (ii)

Step (ii):

Recursively define the value of an optimal solution

C

[

j ] =

  

if

j <

if

j

min

1 ≤ i ≤ k { C [ j − d i ] }

if

j

CS404/

Computer Science

5

Design and Analysis of Algorithms: Lecture 19

An example

An example

C

[11] = min

  

C

[

50]

C

[

25]

C

[

10]

C

[

1]

C C[20] = 2; ..., C[29] = 5;

[30] = min

  

C

[

50]

C

[

25]

C

[5] = 6

C

[

10]

C

[20] = 3;

C

[

1]

C

[29] = 6;

% $

CS404/

Computer Science

7

Design and Analysis of Algorithms: Lecture 19

A Dynamic Programming Solution:

Step (iii)

A Dynamic Programming Solution:

Step (iii)

Step (iii):

Compute values in a bottom-up fashion.

Avoid examining

C

[

j ] for

j <

0 by ensuring that

j

d

i

before

looking up

C [ j − d i

].

C

OMPUTE-

C

HANGE(

n, d, k

C

[0] := 0

for j := 1 to n do

C

[

j ] :=

for i := 1 to k do

if

j

d

i

and 1 +

C [ j − d i ]

< C

[

j ] then

C

[

j ] := 1 +

C [ j − d i ]

return

c

Complexity: Θ(

nk

CS404/

Computer Science

8

Design and Analysis of Algorithms: Lecture 19

Step (iv):

Print optimal solution

Step (iv):

Print optimal solution

P

RINT-COINS(

denom, j

if

j >

P

RINT-COINS(

denom, j

denom

[

j ])

print

denom

[

j ]

Initial call is PRINT-COINS(

denom, n

Example: CS404/

Computer Science

10

Design and Analysis of Algorithms: Lecture 19

Time complexity of DP algorithms

Time complexity of DP algorithms

Usually the complexity of a DP algorithm is:

of sub-problems

×

choices for each sub-problem

For example:

0/1 Knapsack Problem:

C[

i,̟

] = max( C[

i

], C[

i

w

i ] +

p

i ).

n

×

M

sub-problems, each needs to check

choices.

nM

C Matrix Chain Multiplication:

[

i, j

] = min

i ≤

k<j

C

[

i, k

] +

C

[

k

, j

] +

rows

[

A

i ]

col

[

A

k

]

col

[

A

j ] }

n

×

n

sub-problems, each needs to check

O

n

) choices

— O ( n 3 ) Coin Changing Problem:

size of

C

n

,

k

possible coin types

for each

C

[

j ]. — Θ(

nk

% $

CS404/

Computer Science

11

Design and Analysis of Algorithms: Lecture 19

Another Dynamic Programming Solution

Another Dynamic Programming Solution

Step (i):

Characterize the structure of a coin-change solution

Making change for

j

cents with coins

d 1

, d

2

, ..., d

i

can be

  1. Don’t use coindone in two ways:

d i

(even if it’s possible):

C

[

i, j

] =

C

[

i

, j

]

  1. Use coin

d

i

and reduce the amount:

C

[

i, j

] = 1 +

C

[

i, j

d i ].

We will pick the solution with least number of coins:

C

[

i, j

] = min(

C

[

i

, j

]

C

[

i, j

d

i ])

CS404/

Computer Science

13

Design and Analysis of Algorithms: Lecture 19

Another Dynamic Programming Solution

Another Dynamic Programming Solution

Step (ii):

Recursively define the value of an optimal solution

C

[

i, j

] =

  

if

j <

if

j

j

if

i

= 0

min

{ C [ i − 1

, j

]

C

[

i, j

d

i ] }

if

j

CS404/

Computer Science

14

Design and Analysis of Algorithms: Lecture 19

Example:

Bottom-up computation

Example:

Bottom-up computation

Suppose we have coin set

d

1 , d

2

, d

3 }

c,

c,

c }

and

n

c .

d3 = 6 | 0 1 2 3 1 2 1 2 2d2 = 4 | 0 1 2 3 1 2 3 4 2d1 = 1 | 0 1 2 3 4 5 6 7 8----------------------------- C[i,j] | 0 1 2 3 4 5 6 7 8

C

[

8] = min(

C

[

8]

C

[

d

3

]) = min(

Evidently, the optimal solution does NOT use

d

3

.

CS404/

Computer Science

16

Design and Analysis of Algorithms: Lecture 19

Another Dynamic Programming Solution

Another Dynamic Programming Solution

Step (iv):

Construct an optimal solution.

Two strategies:

Online: use an additional matrix

S

[

..k,

..n

], where

S

[

i, j

]

indicates which of the values

C

[

i

, j

] and

C

[

i, j

d

i ] was

used to compute

C

[

i, j

] (use two symbols:

and

Compute

S

in parallel with

C

optimal solution by starting backwards fromBatch: recover the denominations of the coins used in the

C

[

k, n

], after

computing the entire matrix

C

space complexity. HW exercise: write the pseudocode for each, analyze time & CS404/

Computer Science

17

Design and Analysis of Algorithms: Lecture 19