Docsity

Coin Change Problem: Set-builder Notation & Backtracking Algorithm Analysis, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1. The textbook solves the coin-change problem with the following code (note the “set-builder-like” notation):
{c | c ∈ coinValueList and c ≤ change}
Results of running this code:
Change Amount: 63 Coin types: [1,
5,
10,
25]
Run-time: 70.689 seconds
Fewest number of coins 6
Number of Backtracking Nodes: 67,716,925
I removed the fancy set-builder notation and replaced it with a simple if-statement check:
Results of running this code:
Change Amount: 63 Coin types: [1,
5,
10,
25]
Run-time: 45.815 seconds
Fewest number of coins 6
Number of Backtracking Nodes: 67,716,925
a) Why is the second version so much “faster”?
b) Why does it still take a long time?
2. To speed the recursive backtracking algorithm, we can prune unpromising branches. The general recursive
backtracking algorithm for optimization problems (e.g., fewest number of coins) looks something like:
Backtrack( recursionTreeNode p ) {
for each child c of p do # each c represents a possible choice
if promising(c) then # c is "promising" if it could lead to a better solution
if c is a solution that's better than best then # check if this is the best solution found so far
best = c # remember the best solution
else
Backtrack(c) # follow a branch down the tree
end if
end if
end for
} // end Backtrack
General Notes about Backtracking:
The depth-first nature of backtracking only stores information about the current branch being explored on the
run-time stack, so the memory usage is “low” eventhough the # of recursion tree nodes might be exponential (2
n
).
Each node of the search-space (recursive-call) tree maintains the state of a partial solution. In general the partial
solution state consists of potentially large arrays that change little between parent and child. To avoid having
multiple copies of these arrays, a reference to a single “global” array can be maintained which is updated before
we go down to the child (via a recursive call) and undone when we backtrack to the parent.
a) For the coin-change problem, what defines the current state of a search-space tree node?
Data Structures Lecture 13 Name:__________________
Lecture 13 Page 1
def recMC(change, coinValueList):
global backtrackingNodes
backtrackingNodes += 1
minCoins = change
if change in coinValueList:
return 1
else:
for i in [c for c in coinValueList if c <= change]:
numCoins = 1 + recMC(change - i, coinValueList)
if numCoins < minCoins:
minCoins = numCoins
return
minCoins
def
recMC(change,
coinValueList):
global backtrackingNodes
backtrackingNodes += 1
minCoins = change
if change in coinValueList:
return 1
else:
for i in coinValueList:
if i <= change:
numCoins = 1 + recMC(change - i, coinValueList)
if numCoins < minCoins:
minCoins = numCoins
return
minCoins
Docsity.com
pf4

Partial preview of the text

Download Coin Change Problem: Set-builder Notation & Backtracking Algorithm Analysis and more Study notes Data Structures and Algorithms in PDF only on Docsity!

1. The textbook solves the coin-change problem with the following code (note the “set-builder-like” notation):

{c | c ∈ coinValueList and c ≤ change}

Results of running this code:

Change Amount: 63 Coin types: [1, 5, 10, 25] Run-time: 70.689 seconds Fewest number of coins 6 Number of Backtracking Nodes: 67,716,

I removed the fancy set-builder notation and replaced it with a simple if-statement check:

Results of running this code:

Change Amount: 63 Coin types: [1, 5, 10, 25] Run-time: 45.815 seconds Fewest number of coins 6 Number of Backtracking Nodes: 67,716,

a) Why is the second version so much “faster”?

b) Why does it still take a long time?

2. To speed the recursive backtracking algorithm, we can prune unpromising branches. The general recursive

backtracking algorithm for optimization problems (e.g., fewest number of coins) looks something like:

Backtrack( recursionTreeNode p ) {

for each child c of p do # each c represents a possible choice

if promising(c) then # c is "promising" if it could lead to a better solution

if c is a solution that's better than best then # check if this is the best solution found so far

best = c # remember the best solution

else

Backtrack(c) # follow a branch down the tree

end if

end if

end for

} // end Backtrack

General Notes about Backtracking:

 The depth-first nature of backtracking only stores information about the current branch being explored on the

run-time stack, so the memory usage is “low” eventhough the # of recursion tree nodes might be exponential (2n).

 Each node of the search-space (recursive-call) tree maintains the state of a partial solution. In general the partial

solution state consists of potentially large arrays that change little between parent and child. To avoid having

multiple copies of these arrays, a reference to a single “global” array can be maintained which is updated before

we go down to the child (via a recursive call) and undone when we backtrack to the parent.

a) For the coin-change problem, what defines the current state of a search-space tree node?

Lecture 13 Page 1

def recMC(change, coinValueList): global backtrackingNodes backtrackingNodes += 1 minCoins = change if change in coinValueList: return 1 else: for i in [c for c in coinValueList if c <= change]: numCoins = 1 + recMC(change - i, coinValueList) if numCoins < minCoins: minCoins = numCoins return minCoins

def recMC(change, coinValueList): global backtrackingNodes backtrackingNodes += 1 minCoins = change if change in coinValueList: return 1 else: for i in coinValueList: if i <= change: numCoins = 1 + recMC(change - i, coinValueList) if numCoins < minCoins: minCoins = numCoins return minCoins

b) When would a “child” tree node NOT be promising?

3. Consider the output of running the backtracking code with pruning (next page) twice with a change amount of 63

cents.

a) Explain why ordering the coins from largest to smallest produced faster results.

b) For coins of [50, 25, 12, 10, 5, 1] typical timings:

Change Amount Run-Time (seconds) Number of Tree Nodes

Why the exponential growth in run-time?

4. As with Fibonacci, the coin-change problem can benefit from dynamic program since it was slow due to solving

the same problems over-and-over again. Recall the general idea of dynamic programming:

  • Solve smaller problems before larger ones
  • store their answers
  • look-up answers to smaller problems when solving larger subproblems, so each problem solved only once

a) To solve the coin-change problem using dynamic programming, we need to answer the questions:

 What is the smallest problem?

 Where do we store the answers to the smaller problems?

Lecture 13 Page 2

Change Amount: 63 Coin types: [1, 5, 10, 25] Run-time: 0.036 seconds Fewest number of coins 6 The number of each type of coins is: number of 1-cent coins is 3 number of 5-cent coins is 0 number of 10-cent coins is 1 number of 25-cent coins is 2 Number of Backtracking Nodes: 4831

Change Amount: 63 Coin types: [25, 10, 5, 1] Run-time: 0.003 seconds Fewest number of coins 6 The number of each type of coins is: number of 25-cent coins is 2 number of 10-cent coins is 1 number of 5-cent coins is 0 number of 1-cent coins is 3 Number of Backtracking Nodes: 310

Dynamic Programming Coin-change Algorithm:

I. Fills an array fewestCoins from 0 to the amount of change. An element of fewestCoins stores the fewest number

of coins necessary for the amount of change corresponding to its index value.

For 29-cents using the set of coin types {1, 5, 10, 12, 25, 50}, the dynamic programming algorithm would have

previously calculated the fewestCoins for the change amounts of 0, 1, 2, ..., up to 28 cents.

II. If we record the best, first coin to return for each change amount (found in the “minimum” calculation) in an

array bestFirstCoin, then we can easily recover the actual coin types to return.

fewestCoins:

bestFirstCoin:

a minimum for 29

given by 5-cent coin

Extract the coins in the solution for 29-cents from bestFirstCoin[29], bestFirstCoin[24], and bestFirstCoin[12]

fewestCoins[29] = minimum(fewestCoins[28], fewestCoins[24], fewestCoins[19],

fewestCoins[17], fewestCoins[4] ) + 1 = 2 + 1 = 3

b) Extend the lists through 32-cents.

fewestCoins:

bestFirstCoin:

c) What coins are in the solution for 32-cents?

Lecture 13 Page 4