University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 10
Advanced Recursion
Overview of today's lecture
Review
Tail Recursion
- We saw examples of tail recursion, and learned how
it implements iteration.
- Sometimes it's easier to think of a problem and solve it
this way.
- We saw:
- sum-0-to-n-tail
- my-count-tail
- count-even-and-odds
- all-increasing
- longest-win-streak
Advanced Recursion
Overview
- This is the last official recursion lecture.
- Today we'll see advanced forms of recursion
General techniques for solving recursive
problems
- Make sure you understand what the program is supposed to
do
- Take a fairly complicated problem and do the answer in your
head and write the answer down.
- Then, start exploring answers to similar problems, also in
your head
- Note the pattern
Pascal's triangle : pascal
- We'd like to be able to calculate elements of Pascal's
triangle
- The triangle is used in many places!
- Combinatorics
- How many ways are there of choosing C things from R possible
choices?
- E.g., How many ways are there of choosing C=2 cards from
a deck of R=5 cards?
- Answer: look in the table under (C,R) = (2,5) and you see
there are 10 ways!
- Polynomial multiplication
- What are the coefficients of the terns (x + y)5?
- 1 x5 + 5 x4y
+ 10 x3y2 + 10 x2y3
+ 5 xy4 + 1 y5
- See any correlation between the bold numbers above and the
5th row (R=5) ?
|
columns (C) |
r
o
w
s
(R) |
|
0 |
1 |
2 |
3 |
4 |
5 |
... |
0 |
1 |
|
|
|
|
|
... |
1 |
1 |
1 |
|
|
|
|
... |
2 |
1 |
2 |
1 |
|
|
|
... |
3 |
1 |
3 |
3 |
1 |
|
|
... |
4 |
1 |
4 |
6 |
4 |
1 |
|
... |
5 |
1 |
5 |
10 |
10 |
5 |
1 |
... |
... |
... |
... |
... |
... |
... |
... |
... |
Pascal's Triangle
- The rule for calculating the elements triangle is:
- Every element is the sum of the two elements directly above
(north) and to the immediate upper left (northeast)
- More specifically:
pascal(C,R) = pascal(C-1,R-1)
+ pascal(C,R-1)
(define (pascal C R)
==>
==>
==>
- Exercise for the ambitious: rewrite pascal
to use the formula (! = factorial, you will have to write that
also):
pascal(C,R) = R! / ((R-C)! C!)
Insertion Sort : sort
- Sometimes it's nice to have things in order. (Imagine how
useless a dictionary would be if it weren't in alphabetical order!)
- We are going to take a sentence that's out of order and sort
it, alphabetically.
- Example: (sort '(c a d e b)) ==> (a b c d e)
- Idea #1: Break the problem into two sub-problems
- insert : insert an element into an already sorted
sentence
(insert 'c '(a b d e)) ==> (a b c d e)
- There are three cases for insert:
- Is the sentence empty? Then make a sentence of the input
word.
- Is the word smaller than the first element (i.e., does it
go in front?). Then put it in front and stop recursing
- (otherwise) Is the word bigger than the first element? If
so, skip the first element (but remember it) and keep recursing.
- Crucial tool we'll need: before, which tests whether
one word is alphabetically before another
- sort : call insert recursively on every element
- Let's see if we can solve one problem at a time:
(define (insert w sorted)
==>
==>
==>
==>
==>
(define (sort s)
==>
==>
==>
==>
- Exercise for the ambitious: Rewrite the code to sort
the words by length (count of the word), not alphabetically
Pair all prefixes with suffixes : pair-all
- We want to take a sentence of prefixes and one of
suffixes and return a sentence with pairings of all
the prefixes with all the suffixes
(pair-all '(a b c d) '(1 2 3))
;; ==> (a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3)
(pair-all '(to re on) '(ad ward ly))
;; ==> (toad toward toly read reward rely onad onward only)
- Inspiration! Divide the problem into two parts:
- (append-prefix prefix suffixes)
;; append prefix word to all words in suffixes
- (pair-all prefixes suffixes) ;; send
all prefixes in prefixes to append-prefix
(define (append-prefix prefix suffixes)
==>
==>
==>
(define (pair-all prefixes suffixes)
==>
==>
==>
==>
==>
==>
==>
- Exercise for the ambitious: Rewrite the code so that
instead of looping on prefixes first and suffixes next (e.g.,
a1 a2 a3 b1 b2 ...) it loops on suffixes first and prefixes
next (e.g., a1 b1 c1 d1 a2 b2 ...)
Summary
- We looked at three examples which, when combined with the
four examples in the book, give you a good idea what you can
do with the power of recursion!
Next Time
- We'll look at really powerful time-saving recursive functions,
called higher-order functions.
Puzzle : Two Kids [courtesy Ross Levinsky]
- Man #1 says he has two children.
- One of them is a boy.
- What's the chance his other one is a girl?
- Supplementary question:
- Man #2 says he has two children.
- His first one is a boy.
- What's the chance the other one is a girl?
Game : Odd or Even ["Pentagames"
by Pentagram, Fireside Publishing, 1990]
| | | | | | | | | | | | | | |
- Place 15 matches in a row.
- Each player in turn picks up one, two or three matches.
- Once they have all been picked up, the winner is the player
with an even number of matches in her hand.