University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 23 : Recursion Potpourri
Overview of today's lecture
Answers to questions
that have come up
- What kind of exam questions on fractals could we expect for
the final?
- Well, you certainly should expect something about fractals.
- You won't be responsible for the complex geometry that is
usually required to set the fractals up.
- You should be able to generate a fractal given a pattern
and an explanation of the geometry required.
- You should also be able to trace the code for a fractal and
sketch the first few generations, and answer analysis questions
about it (the fractal equivalent of "what's the 90th thing
you do on the 100th day"?)
Project Questions
- Any questions you have should be directed to me or your TAs.
- If we feel others might have the same question, we'll post
it on the Project FAQ
page
Review
Fractals
- We saw six fractals, beautiful recursively-defined graphical
structures.
Recursion Potpourri
Overview
- Here is a very varied potpourri of recursive examples:
Randomized Sierpinski Triangle
- You recall the Sierpinski triangle from Monday.
- There's an alternate way to generate it!
- Start with a point inside the triangle
- Choose one of the 3 corner points at random, move halfway
from your current position to that corner.
- Plot that point on the screen, and repeat the line above.
- Do this 100,000 times and guess what you get?
;; randomized-sierpinski-triangle
;; by Clint Ryan
;; (we've hardcoded the vertex locations in)
;;
(define (randomized-sierpinski-triangle n)
(clear-graphics)
(rst-helper n -200 -200))
(define (rst-helper n x y)
(draw-point x y)
(if (zero? n) 'wow
(let ((vert (random 3)))
(cond
((= 0 vert)
(rst-helper (- n 1) (/ (+ x -200) 2)(/ (+ y -200) 2)))
((= 1 vert)
(rst-helper (- n 1) (/ (+ x 200) 2) (/ (+ y -200) 2)))
((= 2 vert)
(rst-helper (- n 1) (/ (+ x 0) 2) (/ (+ y 200) 2)))))))
Fibonacci revisited
- You recall this from your reading (chapter 13, p. 213 of
SS)
(define (fib n)
(if (<= n 2)
1
(+ (fib (- n 1))
(fib (- n 2)))))
- The Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, ...
- I wonder what the ratio of two consecutive numbers is. Hmmm....
- It captures the recursive structure inherent in nature! (cue
video)
Tower of Hanoi
- Given three pegs (start, end, and extra) and N disks of different
sizes on the start peg, move the disks to the end peg given two
constraints:
- Only one disk can be moved at a time
- A larger disk can never be placed on a smaller disk
- Three disks takes 7 moves, 4 disks takes 15 moves.
- In general it takes (2 to the num-disks) - 1 moves
Background
- Order of monks given the task of moving 64 disks
- Once they solve the problem, the world will be destroyed
- It will take them (/ (expt 2 64) 60 60 24 365.)
years to do so assuming one disk moved per second.
How to solve this puzzle
- If there are no disks to move (we're done) do nothing
- otherwise
- move top N-1 disks from the start peg to the extra peg
- move remaining disk from the start peg to the end peg
- move N-1 disks from the extra peg to the end peg
;; As we recurse the pegs will be changing
;; (what we start at may be our prior end peg),
;; so we will use peg names as parameters.
(define (hanoi num start extra end)
(cond ((zero? num) 'done)
(else
(hanoi (- num 1) start end extra)
(display
`(move disk ,num from peg ,start to peg ,end))
(newline)
(hanoi (- num 1) extra start end))))
: (hanoi 3 'a 'b 'c)
(move disk 1 from peg a to peg c)
(move disk 2 from peg a to peg b)
(move disk 1 from peg c to peg b)
(move disk 3 from peg a to peg c)
(move disk 1 from peg b to peg a)
(move disk 2 from peg b to peg c)
(move disk 1 from peg a to peg c)
done
- quasiquote `
- Like quote but allows evaluation within list structure
Hand trace
(hanoi 3 'a 'b 'c)
(hanoi 2 'a 'c 'b)
(hanoi 1 'a 'b 'c)
(hanoi 0 'a 'c 'b)
done
(move disk 1 from peg a to peg c)
(hanoi 0 'b 'a 'c)
done
done
(move disk 2 from peg a to peg b)
(hanoi 1 'c 'a 'b)
(hanoi 0 'c 'b 'a)
done
(move disk 1 from peg c to peg b)
(hanoi 0 'a 'c 'b)
done
done
done
(move disk 3 from peg a to peg c)
(hanoi 2 'b 'a 'c)
(hanoi 1 'b 'c 'a)
(hanoi 0 'b 'a 'c)
done
(move disk 1 from peg b to peg a)
(hanoi 0 'c 'b 'a)
done
done
(move disk 2 from peg b to peg c)
(hanoi 1 'a 'b 'c)
(hanoi 0 'a 'c 'b)
done
(move disk 1 from peg a to peg c)
(hanoi 0 'b 'a 'c)
done
done
done
done
Animation (using the Grillmeyer Scheme...)
Summary
- We saw some more wonderful examples of recursion in different
contexts
Next Time
- We'll have a guest lecture by one of our outstanding CS3
TAs...
Puzzle : Fair Event from Unfair Coin
- You have an unfair coin (which, when thrown, doesn't land
on heads and tails evenly)
- How do you generate a fair event? (I.e., how do you create
a situation which will give equal probabilities to two events,
"new-heads" and "new-tails")
Game : Amazons
- Amazons is a fairly new board game invented by the Argentinian
Walter Zamkauskas in 1988.
- It is played on a 10x10 board.
- Each player has 4 queens.
- The white queens are placed on a4, d1, g1, j4 and the black
queens are placed on a7, d10, g10, j7.
- White moves first.
- Each move consists of 2 mandatory parts.
- First, an amazons queen moves just like a chess queen.
- After a queen is moved it has to shoot an arrow, which in
turn can move like a chess queen.
- The square where an arrow lands on is blocked for the rest
of the game.
- This means that a queen cannot move to or over that square
just as it cannot move over other queens.
- There are no captures and no draws in Amazons!
- The winner is the last player able to complete a move.