6.001 SICP – September 22. 6001-Introduction Trevor Darrell [email protected] 32-D512 6.001 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/trevor/6001/ Office Hours W11, 32-D512 Orders of Growth Linear Exponential Logarithmic Let. 6.001 SICP. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• At 1 billion operations per second (current state of the art), if you were to run an exponential time algorithm in the lab on a data set of size n=100, you would be waiting for approximately 4x1011 centuries for the code to finish running!
6.001 SICP 5
Order of growth examples
For each, find simplest and slowest growing f for which R(n)=(f(n))
• R(n)=6
• R(n)=n2 + 3
• R(n)=6*n3 + 3*n2 + 7n + 100
• R(n)=23n+7
6.001 SICP 6
Order of growth examples
For each, find simplest and slowest growing f for which R(n)=(f(n))
• R(n)=6(1) 1 * 1 <= 6 <= 6 * 1 for all n• R(n)=n2 + 3(n2) 1*n2 <= n2 + 3 <= 2*n2 for all n > 2• R(n)=6*n3 + 3*n2 + 7n + 100(n3) 1*n3 <= 6*n3 + 3*n2 + 7n + 100 <= 7*n3 for all n>100• R(n)=23n+7
Towers of Hanoi• Three posts, and a set of different size disks• any stack must be sorted in decreasing order from bottom
to top• the goal is to move the disks one at a time, while
preserving these conditions, until the entire stack has moved from one post to another
6.001 SICP 10
Towers of Hanoi(define move-tower (lambda (size from to extra) (cond ((= size 0) true) (else (move-tower (- size 1) from extra to) (print-move from to) (move-tower (- size 1) extra to from)))))
(define print-move (lambda (from to) (write-line ``Move top disk from ``) (write-line from) (write-line `` to ``) (write-line to)))
6.001 SICP 11
A tree recursion
Move 4
Move 3 Move 3
Move 2Move 2Move 2Move 2
Move 1Move 1
Move 1Move 1
Move 1Move 1
Move 1Move 1
6.001 SICP 12
Orders of growth for towers of Hanoi
• Let tn be the number of steps that we need to take to solve the case for n disks. Then
• tn = 2tn-1 + 1 = 2(2tn-2 +1) + 1 ….= 2n -1• So in time we have (2n) -- exponential• In space, we have one deferred operation for each
increment of the stack of disks -- (n) -- linear
6.001 SICP 13
QuizWhat is simplest expression for the order of growth of running time of
procedure mul1 & mul2?
(define (mul1 n m) (if (= n 0) 0 (+ m (mul1 (- n 1) m))))
(define (mul2 n m) (define (help count ans) (if (= count 0) ans (help (- count 1) (+ m ans)))) (help n 0))