-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Understand orders of growth for count-change
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
digraph { | ||
s [label="(cc 11 5)"]; | ||
s -> sl; | ||
s -> sr; | ||
sl [label="(cc 11 4)"]; | ||
sl -> sll; | ||
sl -> slr; | ||
sll [label="(cc 11 3)"]; | ||
sll -> slll; | ||
sll -> sllr; | ||
slll [label="(cc 11 2)"]; | ||
slll -> sllll; | ||
slll -> slllr; | ||
sllll [label="(cc 11 1)"]; | ||
sllll -> slllll; | ||
sllll -> sllllr; | ||
slllll [label="(cc 11 0)"]; | ||
sllllr [label="(cc 10 1)"]; | ||
sllllr -> sllllrl; | ||
sllllr -> sllllrr; | ||
sllllrl [label="(cc 10 0)"]; | ||
sllllrr [label="(cc 9 1)"]; | ||
sllllrr -> sllllrrl; | ||
sllllrr -> sllllrrr; | ||
sllllrrl [label="(cc 9 0)"]; | ||
sllllrrr [label="(cc 8 1)"]; | ||
sllllrrr -> sllllrrrl; | ||
sllllrrr -> sllllrrrr; | ||
sllllrrrl [label="(cc 8 0)"]; | ||
sllllrrrr [label="(cc 7 1)"]; | ||
sllllrrrr -> sllllrrrrl; | ||
sllllrrrr -> sllllrrrrr; | ||
sllllrrrrl [label="(cc 7 0)"]; | ||
sllllrrrrr [label="(cc 6 1)"]; | ||
sllllrrrrr -> sllllrrrrrl; | ||
sllllrrrrr -> sllllrrrrrr; | ||
sllllrrrrrl [label="(cc 6 0)"]; | ||
sllllrrrrrr [label="(cc 5 1)"]; | ||
sllllrrrrrr -> sllllrrrrrrl; | ||
sllllrrrrrr -> sllllrrrrrrr; | ||
sllllrrrrrrl [label="(cc 5 0)"]; | ||
sllllrrrrrrr [label="(cc 4 1)"]; | ||
sllllrrrrrrr -> sllllrrrrrrrl; | ||
sllllrrrrrrr -> sllllrrrrrrrr; | ||
sllllrrrrrrrl [label="(cc 4 0)"]; | ||
sllllrrrrrrrr [label="(cc 3 1)"]; | ||
sllllrrrrrrrr -> sllllrrrrrrrrl; | ||
sllllrrrrrrrr -> sllllrrrrrrrrr; | ||
sllllrrrrrrrrl [label="(cc 3 0)"]; | ||
sllllrrrrrrrrr [label="(cc 2 1)"]; | ||
sllllrrrrrrrrr -> sllllrrrrrrrrrl; | ||
sllllrrrrrrrrr -> sllllrrrrrrrrrr; | ||
sllllrrrrrrrrrl [label="(cc 2 0)"]; | ||
sllllrrrrrrrrrr [label="(cc 1 1)"]; | ||
sllllrrrrrrrrrr -> sllllrrrrrrrrrrl; | ||
sllllrrrrrrrrrr -> sllllrrrrrrrrrrr; | ||
sllllrrrrrrrrrrl [label="(cc 1 0)"]; | ||
sllllrrrrrrrrrrr [label="(cc 0 1)"]; | ||
slllr [label="(cc 6 2)"]; | ||
slllr -> slllrl; | ||
slllr -> slllrr; | ||
slllrl [label="(cc 6 1)"]; | ||
slllrl -> slllrll; | ||
slllrl -> slllrlr; | ||
slllrll [label="(cc 6 0)"]; | ||
slllrlr [label="(cc 5 1)"]; | ||
slllrlr -> slllrlrl; | ||
slllrlr -> slllrlrr; | ||
slllrlrl [label="(cc 5 0)"]; | ||
slllrlrr [label="(cc 4 1)"]; | ||
slllrlrr -> slllrlrrl; | ||
slllrlrr -> slllrlrrr; | ||
slllrlrrl [label="(cc 4 0)"]; | ||
slllrlrrr [label="(cc 3 1)"]; | ||
slllrlrrr -> slllrlrrrl; | ||
slllrlrrr -> slllrlrrrr; | ||
slllrlrrrl [label="(cc 3 0)"]; | ||
slllrlrrrr [label="(cc 2 1)"]; | ||
slllrlrrrr -> slllrlrrrrl; | ||
slllrlrrrr -> slllrlrrrrr; | ||
slllrlrrrrl [label="(cc 2 0)"]; | ||
slllrlrrrrr [label="(cc 1 1)"]; | ||
slllrlrrrrr -> slllrlrrrrrl; | ||
slllrlrrrrr -> slllrlrrrrrr; | ||
slllrlrrrrrl [label="(cc 1 0)"]; | ||
slllrlrrrrrr [label="(cc 0 1)"]; | ||
slllrr [label="(cc 1 2)"]; | ||
slllrr -> slllrrl; | ||
slllrr -> slllrrr; | ||
slllrrl [label="(cc 1 1)"]; | ||
slllrrl -> slllrrll; | ||
slllrrl -> slllrrlr; | ||
slllrrll [label="(cc 1 0)"]; | ||
slllrrlr [label="(cc 0 1)"]; | ||
slllrrr [label="(cc -4 2)"]; | ||
sllr [label="(cc 1 3)"]; | ||
sllr -> sllrl; | ||
sllr -> sllrr; | ||
sllrl [label="(cc 1 2)"]; | ||
sllrl -> sllrll; | ||
sllrl -> sllrlr; | ||
sllrll [label="(cc 1 1)"]; | ||
sllrll -> sllrlll; | ||
sllrll -> sllrllr; | ||
sllrlll [label="(cc 1 0)"]; | ||
sllrllr [label="(cc 0 1)"]; | ||
sllrlr [label="(cc -4 2)"]; | ||
sllrr [label="(cc -9 3)"]; | ||
slr [label="(cc -14 4)"]; | ||
sr [label="(cc -39 5)"]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
(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 tree-recursive process of count-change is similar in form to the | ||
;; tree-recursive Fibonacci computation, which requires a number of steps that | ||
;; grows exponentially with n and space that grows linearly with n. It seems | ||
;; that count-change requires O(2^amount) steps and O(amount) space. | ||
|
||
;; To visualize the tree, I adapted code from | ||
;; http://tobilehman.com/blog/2013/04/07/visualization-of-sicp-exercise-1-dot-14/. See below. | ||
;; | ||
;; I saved output in "ex1.14.dot" and used GraphViz to visualize as | ||
;; "ex1.14.tree.pdf". | ||
;; | ||
;; GraphViz command: "dot -Tpdf ex1.14.dot -o ex1.14.tree.pdf" | ||
|
||
(define (cc-graph amount kinds-of-coins) | ||
|
||
(define (display-node label amount kinds-of-coins) | ||
(display " ") | ||
(display label) | ||
(display " [label=\"") | ||
(display `(cc ,amount ,kinds-of-coins)) | ||
(display "\"];") | ||
(newline)) | ||
|
||
(define (display-edge a b) | ||
(display " ") | ||
(display a) | ||
(display " -> ") | ||
(display b) | ||
(display ";") | ||
(newline)) | ||
|
||
(define (base-case amount kinds-of-coins) | ||
(or (< amount 0) | ||
(= kinds-of-coins 0) | ||
(= amount 0))) | ||
|
||
(define (left label) | ||
(string-append label "l")) | ||
|
||
(define (right label) | ||
(string-append label "r")) | ||
|
||
; label is the unique label of the function invocation, e.g. "sllrl" is | ||
; reached by traveling (from the root) left then left then right then left. | ||
(define (recurse label amount kinds-of-coins) | ||
(cond ((base-case amount kinds-of-coins) | ||
(display-node label amount kinds-of-coins)) | ||
(else | ||
(display-node label amount kinds-of-coins) | ||
(display-edge label (left label)) | ||
(display-edge label (right label)) | ||
(recurse (left label) amount (- kinds-of-coins 1)) | ||
(recurse (right label) | ||
(- amount (first-denomination kinds-of-coins)) | ||
kinds-of-coins)))) | ||
|
||
(display "digraph {") | ||
(newline) | ||
(recurse "s" amount kinds-of-coins) | ||
(newline) | ||
(display "}")) |
Binary file not shown.