Title: Hanoi Towers Continued
1Lecture 5
Hanoi Towers -- Continued Higher-order
procedures High order procedures for fixed
points (if we have time)
2Last Lecture Towers of Hanoi
- Three posts, and a set of disks of different
sizes. - A disk can be placed only on a larger disk (or
on bottom). - At the beginning all the disks are on the left
post. - The goal is to move the disks one at a time,
while preserving these conditions, until the
entire stack has moved from the left post to
another
You are allowed to move only the topmost disk at
a step
3Use our paradigm
- Wishful thinking
- Smaller problem A problem with one disk less
- How do we use it ?
To move n disks from post A to post C (using B as
aux) Move top n-1 disks from post A to post
B (using C as aux) Move the largest disk
from post A to post C Move n-1 disks from
post B to post C (using A as aux)
We solve 2 smaller problems !
4Towers of Hanoi
(define (move-tower size from to aux) (cond
(( size 1) (one-move from to)) (else
(move-tower (- size 1) from aux to)
(one-move from to) (move-tower (- size
1) aux to from))))
(define (one-move from to) (display "Move top
disk from ") (display from) (display " To
") (display to) (newline))
5Tree Recursion
(mt 3 1 2 3)
6Towers of Hanoi -- trace
- (move-tower 3 1 2 3)
- Move top disk from 1 to 2
- Move top disk from 1 to 3
- Move top disk from 2 to 3
- Move top disk from 1 to 2
- Move top disk from 3 to 1
- Move top disk from 3 to 2
- Move top disk from 1 to 2
7Orders of growth for towers of Hanoi
- Denote by T(n) the number of steps that we need
to take to solve the case for n disks. -
T(n) 2T(n-1) 1 T(1) 1
This solves to T(n) 2n-1 Q (2n)
What does that mean ?
8Hanoi Towers
Say we want to solve the problem for 400 disks.
Say it takes a second to move a disk . We need
about 2400 seconds. Thats about 2373
years. Thats about 2363 millenniums. Might
take longer then the age of the universe .
Infeasible !!!!
9Lets buy a fast computerand make it feasible.
Our new computer can move giga billion (260 )
disks a second. Absolutely the last word in the
field of computing. We need about 2340
seconds. Thats about 2313 years. Thats about
2303 millenniums.
Does not help much. Infeasible !!!!
An algorithm with exponential time complexityis
not scalable
10What about Space complexity?
11Towers of Hanoi
(define (move-tower size from to aux) (cond
(( size 1) (one-move from to)) (else
(move-tower (- size 1) from aux to)
(one-move from to) (move-tower (- size
1) aux to from))))
Pending operations
(define (one-move from to) (display "Move top
disk from ") (display from) (display " To
") (display to) (newline))
12Towers of Hanoi Space complexity
The number of pending operations is the height of
the recursion tree.
So the space complexity is
S(n) Q(n)
Note that the second recursive call is treated as
tail recursion, and forms an iteration (no
pending operation for these calls).
13Tree Recursion
(mt 3 2 1 3)
14Types so far
- Numbers 1, 7, 1.2
- Boolean t , f
- Strings this is a string
- Procedures
-
-
- (lambda (x) (if (lt x 0)
- x is negative
- x is not negative))
15Procedures have types
- A procedure
- may have requirements regarding the
- number of its arguments,
- may expect each argument to be of a certain
type.
The procedure expects numbers as its
arguments. Cannot be applied to strings.
( abc xyz)
16Procedures have types
- The type of a procedure is (part of) a contract
- If the operands have the specified types,the
procedure will result in a value of the specified
type - otherwise, its behavior is undefined
- maybe an error, maybe random behavior
- A contract between the caller and the procedure.
- caller responsible for argument number and types
- procedure responsible to deliver correct result
17Example
The type of the procedure add (define (add x y)
( x y)) is
( 7 xx) - causes an error.
18Your turn
The following expressions evaluate to values of
what type?
(lambda (a b c) (if (gt a 0) ( b c) (- b c)))
(lambda (p) (if p "hi" "bye"))
( 3.14 ( 2 5))
number
(lambda (p) (if p "hi" 0))
??
19Types (summary)
- type a set of possible values and operations
- every value has a type
- procedure types (types which include ?) indicate
- number of arguments required
- type of each argument
- type of result of the procedure
- Scheme has a lax type policy
20Can procedures get and return procedures?
- In scheme a procedure can
- Return a procedure as its return value,
- Receive procedures as arguments.
Why is this useful?
21Consider the following three sums
- 1 2 100 (100 101)/2
- 1 4 9 1002 (100 101 102)/6
- 1 1/32 1/52 1/1012 p2/8
In mathematics they are all captured by the
notion of a sum
22Lets have a look at the three programs
(define (sum term a next b) (if (gt a b)
0 ( (term a) (sum term (next a)
next b))))
23Lets check this new procedure out!
- (define (sum term a next b)
- (if (gt a b)
- 0
- ( (term a)
- (sum term (next a) next b))))
- What is the type of this procedure?
((number ? number) number (number? number)
number) ? number
24Higher-order procedures
(define (sum term a next b) (if (gt a b)
0 ( (term a) (sum term (next a)
next b))))
- Examples of use
- 1. (define (sum-integers1 a b)
- (sum (lambda (x) x) a (lambda (x) ( x
1)) b)) - 2. (define (sum-squares1 a b)
- (sum square a (lambda (x) ( x 1)) b))
- 3. (define (pi-sum1 a b)
- (sum (lambda (x) (/ 1 (square x))) a
(lambda (x) ( x 2)) b))
A higher-order procedure takes one or more
procedures as arguments and/or returns one
as a value
25How does it work?
- (define (sum term a next b)
- (if (gt a b) 0
- ( (term a) (sum term (next a) next b))))
(sum square 1 (lambda (x) ( x 1)) 100)
( (square 1) (sum square ((lambda (x) ( x
1)) 1) (lambda (x) ( x 1)) 100))
( 1 (sum square 2 (lambda (x) ( x 1)) 100))
( 1 ( (square 2) (sum square 3 (lambda (x) ( x
1)) 100)))
( 1 ( 4 (sum square 3 (lambda (x) ( x 1))
100)))
( 1 ( 4 ( 9 (sum square 4 (lambda (x) ( x 1))
100)))
26Integration as a procedure
- Integration under a curve f is approximated by
- dx (f(a) f(a dx) f(a 2dx) f(b))
(define (integral f a b) (define dx 1.0e-3)
( (sum f a (lambda (x) ( x dx)) b) dx))
(define atan (lambda (a)
(integral (lambda (y) (/ 1 ( 1 (square y)))) 0
a)))
27The derivative.
- We want to write a procedure with
- Argument a function f number ? number
- Result the function f number ? number
deriv (number ? number) ? (number ? number)
(define (deriv f) (lambda (x)
(define dx 0.001) (/ (- (f ( x dx))
(f x)) dx)))
gt ((deriv square) 3) 6.000999999999479
28We will soon see more examples of higher-order
procedures
Before that, LET us have an intermezzo
29The syntactic sugar Let
Suppose we wish to implement the function
f(x,y) x(1xy)2 y(1-y) (1xy)(1-y)
30The syntactic sugar Let
(define (f x y) ((lambda (a b) ( ( x
(square a)) ( y b) ( a b)))
( 1 ( x y)) (- 1 y)))
(define (f x y) (define (f-helper a b) (
( x (square a)) ( y b) ( a b)))
(f-helper ( 1 ( x y)) (- 1 y)))
(define (f x y) (let ((a ( 1 ( x y)))
(b (- 1 y))) ( ( x (square a)) ( y
b) ( a b))))
31The syntactic sugar Let
(Let ((ltvar1gt ltexp1gt) (ltvar2gt ltexp2gt)
.. (ltvarngt ltexpngt)) ltbodygt)
Is defined to be equivalent to
((lambda (ltvar1gt .. ltvarngt) ltbodygt)
ltexp1gt ltexp2gt ltexpngt)
32More Procedural Abstraction Fixed Points
x0 is a fixed point of F(x) if F(x0) x0
x0
Example
is a fixed point of F(x) a/x
33Finding fixed points for f(x)
- Start with an arbitrary first guess x1
- Each time
- try the guess, f(x) x ??
- If its not a good guess try the next guess xi1
f(xi)
(define (fixed-point f first-guess) (define
tolerance 0.00001) (define (close-enough? v1
v2) (lt (abs (- v1 v2)) tolerance)) (define
(try guess) (let ((next (f guess))) (if
(close-enough? guess next) guess
(try next)))) (try first-guess))
34An example f(x) 11/x
(define (f x) ( 1 (/ 1 x))) (fixed-point f 1.0)
X1 1.0 X2 f(x1) 2 X3 f(x2) 1.5 X4
f(x3) 1.666666666.. X5 f(x4) 1.6 X6 f(x5)
1.625 X7 f(x6) 1.6153846 Exact
fixed-point 1.6180339
Note how odd guesses underestimate And even
guesses Overestimate.
35Another example f(x) 2/x
(define (f x) (/ 2 x)) (fixed-point f 1.0)
x1 1.0 x2 f(x1) 2 x3 f(x2) 1 x4 f(x3)
2 x5 f(x4) 1 x6 f(x5) 2 x7 f(x6)
1 Exact fixed-point 1.414213562
36How do we deal with oscillation?
Consider f(x)2/x. If guess is a number such
that guess lt sqrt(2) then 2/guess gt sqrt(2) So
the average of guess and 2/guess is always an
even Better guess.
So, we will try to find a fixed point of g(x)
(x f(x))/2
Notice that g(x) (x f(x)) /2 has the same
fixed points as f.
For f(x)2/x this gives g(x) (x 2/x)/2
37(No Transcript)
38Extracting the common pattern average-damp
- (define (average-damp f) outputs g(x)(xf(x))/2
- (lambda (x) (average x (f x))))
average-damp (number ? number) ? (number ?
number)
((average-damp square) 10) ((lambda (x)
(average x (square x))) 10) (average 10
(square 10)) 55
39 which gives us a clean version of sqrt
- (define (sqrt x)
- (fixed-point
- (average-damp
- (lambda (y) (/ x y)))
- 1))
- Compare this to our previous implementation of
sqrt same process. - For the cubic root of x, fixed point of f(y)
x/y2 - (define (cubert x)
- (fixed-point
- (average-damp (lambda (y) (/ x (square y))))
- 1))
40Further abstraction
- (define (osc-fixed-point f first-guess)
- (fixed-point (average-damp f)
- first-guess))
-
(define (sqrt x) (osc-fixed-point (lambda (y)
(/ x y)) 1.0) (define (cubert x)
(osc-fixed-point (lambda (y) (/ x (square y)))
1.0)
41Newtons method
A solution to the equation F(x) 0 is a fixed
point of G(x) x - F(x)/F(x)
(define (newton-transform f) (lambda (x) (- x
(/ (f x) ((deriv f) x))))) (define
(newton-method f guess) (fixed-point
(newton-transform f) guess))
(define (sqrt x) (newton-method (lambda (y) (-
(square y) x)) 1.0))
42Further abstraction
(define (fixed-point-of-transform f transform
guess) (fixed-point (transform f)
guess)) (define (osc-fixed-point f guess)
(fixed-point-of-transform f average-damp
guess)) (define (newton-method f
guess) (fixed-point-of-transform f
newton-transform guess))