Outline for May 27, 2009
Reading: §13.2
- Recursion
- Express problem in terms of itself
- Recursive definitions
- Base case (not recursive)
- Recursive part (must eventually reduce to base case)
- Relationship to mathematical induction
- Example: n! (see fact.py)
- Definition
- Base case: 0! = 1
- Recursive part: n! = n(n−1)!
- Show stack for n = 3
- Example: reverse string (see reverse.py)
- Definition
- Base case: empty string
- Recursive part: reverse(s) = reverse(s[1:]) + s[0]
- Show stack for s = “yes”
- Example: binary search (see binsearch.py)
- Definition
- Base case: high < low, return failure; word is list[mid],
return mid
- Recursive part: if word < list[mid], search word[0..mid-1];
if word > list[mid], search word[mid+1..high]
- Show for list.txt from last time
- Example: list of permutations of string (see perm.py)
- Definition
- Base case: empty string gives list of empty string
- Recursive part: for each permutation of (string without first char),
put first letter of this string in each position
- Show stack for s = “012”
- Example: Fibonacci numbers (see rfib.py)
- Definition
- Base case: fib(0) = 1, fib(1) = 1
- Recursive part: fib(n) = fib(n−1) + fib(n−2)
- Show stack for n = 3