The exercise asks us to draw the tree illustrating the process generated by count-change
given the argument 11 cents.
The count-change
procedure is given as follows:
(define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50)))
The process tree can be laid out as follows:
(count-change 11) (cc 11 5) (+ (cc 11 4) (cc -39 4)) (+ (cc 11 4) 0) (+ (+ (cc 11 3) (cc -14 4)) 0) (+ (+ (+ (cc 11 2) (cc 1 3)) 0) 0) (+ (+ (+ (+ (cc 11 1) (cc 6 2)) (+ (cc 1 2) (cc -9 3))) 0) 0) (+ (+ (+ (+ (+ (cc 11 0) (cc 10 1)) (+ (cc 6 1) (cc 1 1))) (+ (+ (cc 1 1) (cc -4 2)) 0)) 0) 0) (+ (+ (+ (+ (+ 0 (+ (cc 10 0) (cc 9 1))) (+ (+ (cc 6 0) (cc 5 1)) (+ (cc 1 0) (cc 0 1)))) (+ (+ (+ (cc 1 0) (cc 0 1)) 0) 0)) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ (cc 9 0) (cc 8 1)))) (+ (+ 0 (+ (cc 5 0) (cc 4 1))) (+ 0 1))) (+ (+ (+ 0 1) 0) 0)) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ (cc 8 0) (cc 7 1))))) (+ (+ 0 (+ 0 (+ (cc 4 0) (cc 3 1)))) 1)) (+ (+ 1 0) 0)) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 (+ (cc 7 0) (cc 6 1)))))) (+ (+ 0 (+ 0 (+ 0 (+ (cc 3 0) (cc 2 1))))) 1)) (+ 1 0)) 0) 0) (+ (+ (+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+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 1))))))))) (+ 1 1)) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 1)))))))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 1))))))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 1)))))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 (+ 0 1))))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 (+ 0 1)))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 (+ 0 1))) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 (+ 0 1)) 2) 1) 0) 0) (+ (+ (+ (+ (+ 0 1) 2) 1) 0) 0) (+ (+ (+ (+ 1 2) 1) 0) 0) (+ (+ (+ 3 1) 0) 0) (+ (+ 4 0) 0) (+ 4 0) 4
Clearly, from the above expansion, the space required grows linearly with the argument to count-change
.
To calculate the order of growth as the change increases, we know that the number of coins in the denominations are a constant (5).
Now, every call to (cc n 1)
leads to two calls (+ (cc n 0) (cc (- n 1) 0))
and this process keeps on continuing until n
becomes 1, which belongs to O(n)
.
Every call to (cc n 2)
leads to two calls (+ (cc n 1) (cc (- n 5) 2))
. Now at every step, the argument to (cc n 2)
decreases by 5 and hence a total of n/5
calls are made each of which gets attached with a (cc n 1)
call which belongs to O(n). Thus, (cc n 2)
belongs to .
Now, (cc n 3)
expands to (+ (cc n 2) (cc (- n 10) 3))
. Similar to the above situation, n/10
calls are made each attached to a (cc n 2)
call, thus belonging to .
Similarly, (cc n 4)
expands to (+ (cc n 3) (cc (- n 25) 4))
, which belongs to and (cc n 5)
expands to (+ (cc n 4) (cc (- n 50) 5))
which belongs to .
Thus, the order of growth of the procedure belongs to .