Skip to main content.
-->
April 25th, 2005

Solution to SICP Exercise 1.14

Structure and Interpretation of Computer Programs

Solution to Exercise 1.14:

The process generated by the count-change procedure
Click to enlarge.

The count-change is Θ(n) in space. Like the fib procedure, the space required is equal to the maximum depth of the tree, which occurs on the combination that is all pennies.

I believe, though I could very well be wrong, that the process is Θ(n2) in time as the calls to cc tend to double with increments to n.

I couldn’t get conclusive timing numbers out of DrScheme to confirm this belief. Here’s the code I was using to test

(require (lib "19.ss" "srfi"))
(map (lambda (amount)
       (let ((start-time (current-time time-process))
              (change (count-change amount))
              (end-time (current-time time-process)))
         (let ((diff (time-difference end-time start-time)))
           (list (time-second diff) (time-nanosecond diff)))))
     (list 100 200 300 400))

The output was ((0 160000) (0 2810000) (0 1560000) (1 6560000)). I don’t understand why the third time was consistently less than the second. Anybody care to enlighten me?

Posted by Ken Dyck in Programming

This entry was posted on Monday, April 25th, 2005 at 9:07 pm and is filed under Programming. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 Responses to “Solution to SICP Exercise 1.14”

  1. Scott says:

    Hi Ken,

    My wife and I both agree with you on space being O(n), but we also think the steps (or “time”) is O(n) as well. Our reasoning is: the limiting case is where the change is all pennies (for both space and steps) and increasing the amount by ‘1′ will add one step and one “unit” (expression) of space. So steps, as well as space, is linear with amount (and amount is synonymous with n in this exercise).

    Dei gratia,
    Scott

  2. SICP по-русски » Blog Archive » Решение упражнения 1.14 из SICP says:

    […] отметить, что и у Эли Бендерски, и у Кена Дика решение этого упражнения в части оценки числа шагов […]

  3. mats says:

    According to my tests, the number of steps for count-change 11 is 55, count-change 22 is 250, count-change 44 is 1351, and count-change 88 is 11260.

    This seems to be O(n^3).

  4. Chris says:

    You did (cc 6 2) wrong.

  5. Bhrgunatha says:

    I agree with Chris - I think the 6.2 rhs should branch to (cc 1,2).
    Here is my attempt : SICP ex 1.14

  6. Tim Eichner says:

    I agree that the space required is O(n), but I suspect that the number of steps is O(n^c), where n = amount and c = number of kinds of coins. For each kind of coin at position k with value v, you get about n/v steps with k kinds of coins. For each of these steps, you repeat the cc procedure with (k - 1) kinds of coins (with amounts between 0 and n). For large n, n/v is on the order of n, and (cc n k) amounts to multiplying n by itself k times.

  7. Tim Eichner says:

    If you find anything wrong with my previous argument, let me know; I’m curious about this ;) Not every step repeats the cc procedure with amount = 0 to n, of course; and (cc n k) multiplies n by itself about (k - 1) times. I can be fuzzy with details.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>