patternMinor
Time complexity for count-change procedure in SICP
Viewed 0 times
timeprocedureforsicpcountchangecomplexity
Problem
In famous Structure and Interretation of Computer Programs, there is an exercise (1.14), that asks for the time complexity of the following algorithm - in Scheme - for counting change (the problem statement suggests drawing the tree for
Now... there are sites with solutions to the SICP problems, but I couldn't find any easy to understand proof for the time complexity of the algorithm - there is a mention somewhere that it's polynomial
(cc 11 5) - which looks like this):; count change
(define (count-change amount)
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)
(cc amount
(- kinds-of-coins 1))))))
(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)))
(cc amount 5))Now... there are sites with solutions to the SICP problems, but I couldn't find any easy to understand proof for the time complexity of the algorithm - there is a mention somewhere that it's polynomial
O(n^5)Solution
Order of growth of number of steps: $\theta (n^5)$
We can prove that, in general, the order of growth of number of steps is $\theta (n^m)$, where $m$ is the number of types of coin available. Here is my (very) crude reasoning using induction:
-
When there is only one type of coin, the number of steps is obviously proportional to n.
-
Suppose it would take
(cc $n$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m$) + (cc $n-2A$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m$) + (cc $n-2A$ $m$) + (cc $n-3A$ $m+1$)
= ......
It would eventually computes to
There are approximately $n/A$ items. So the total number of steps would be proportional to $n/A*n^m$, which is proportional to $n^{m+1}$. Thus, the order of growth for number of steps of
Let $m$ be 5, and the order of growth of number of steps is $\theta (n^5)$.
We can prove that, in general, the order of growth of number of steps is $\theta (n^m)$, where $m$ is the number of types of coin available. Here is my (very) crude reasoning using induction:
-
When there is only one type of coin, the number of steps is obviously proportional to n.
-
Suppose it would take
(cc n m) steps to change an amount of $n$ with $m$ types of coin. Now let's consider (cc n m+1): ($A$ is the denomination of the $m$th kind of coin.)(cc $n$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m$) + (cc $n-2A$ $m+1$)
= (cc $n$ $m$) + (cc $n-A$ $m$) + (cc $n-2A$ $m$) + (cc $n-3A$ $m+1$)
= ......
It would eventually computes to
(cc n m) + (cc n-A m) + ... + (cc m+1)There are approximately $n/A$ items. So the total number of steps would be proportional to $n/A*n^m$, which is proportional to $n^{m+1}$. Thus, the order of growth for number of steps of
(cc n m) is $\theta (n^m)$.Let $m$ be 5, and the order of growth of number of steps is $\theta (n^5)$.
Code Snippets
(cc n m) + (cc n-A m) + ... + (cc <something-negative> m+1)Context
StackExchange Computer Science Q#7105, answer score: 6
Revisions (0)
No revisions yet.