CH 08
CH 08
Recursion
Recursion means defining something, such as a function, in terms of itself
For example, let f(x) = x! We can define f(x) as f(x) = x * f(x-1)
Recursion example
a)
Find f(1), f(2), f(3), and f(4), where f(0) = 1 Let f(n+1) = f(n) + 2
f(1) = f(0) + 2 = 1 + 2 = 3 f(2) = f(1) + 2 = 3 + 2 = 5 f(3) = f(2) + 2 = 5 + 2 = 7 f(4) = f(3) + 2 = 7 + 2 = 9
b)
Recursion example
c)
Find f(1), f(2), f(3), and f(4), where f(0) = 1 Let f(n+1) = 2f(n)
f(1) = 2f(0) = 21 = 2 f(2) = 2f(1) = 22 = 4 f(3) = 2f(2) = 24 = 16 f(4) = 2f(3) = 216 = 65536 f(n) = 2n + 1
d)
1 2 3 A B C
1 2 3 4 A B C
1 2 3 4 5 A B C
1 2 3 4 5 6
Counting the moves: Let f(n) be the number of moves for n disks. f(1) = 1; f(n) = 2f(n-1) + 1.
9
Let f(n) be the number of moves for n disks. f(1) = 1; f(n) = 2f(n-1) + 1.
Number of Disks 1 2 3 4 5 6 Number of Moves f(1) = 1 f(2) = 2*1 + 1 = 3 f(3) = 2*3 + 1 = 7 f(4) = 2*7 + 1 = 15 f(5) = 2*15 + 1 = 31 f(6) = 2*31 + 1 = 63
Let f(n) be the number of moves for n disks. f(1) = 1; f(n) = 2f(n-1) + 1. Prove: f(n) = 2n - 1. By induction.
Base step: n = 1 1. Left = f(1) = 1; Right = 21 - 1 = 1 Induction hypothesis: f(n-1) = 2n-1 - 1. Inductive step: Left = f(n) = 2f(n-1) + 1 = 2(2n-1 - 1) + 1 = 2n - 1. Right = 2n - 1.
11
Fascinating fact
So the formula for finding the number of steps it takes to transfer n disks from post A to post C is:
2 n- 1
If n = 64, the number of moves of single disks is 2 to the 64th minus 1, or 18,446,744,073,709,551,615 moves! If one worked day and night, making one move every second it would take slightly more than 580 billion years to accomplish the job! - far, far longer than some scientists estimate the solar system will last.
Fibonacci sequence
Definition of the Fibonacci sequence
Non-recursive:
F ( n) =
(1 + 5 ) (1 5 )
n
5 2
Recursive: or:
14
Recursion pros
Easy to program Easy to understand
15
Recursion cons
Consider the recursive Fibonacci generator How many recursive calls does it make?
F(1): 1 F(2): 1 F(3): 3 F(4): 5 F(5): 9 F(10): 109 F(20): 13,529 F(30): 1,664,079 F(40): 204,668,309 F(50): 25,172,538,049 F(100): 708,449,696,358,523,830,149 7 * 1020
At 1 billion recursive calls per second (generous), this would take over 22,000 years But that would also take well over 1012 Gb of memory!
16
Consider:
f(0) = 1 f(n) = 1+f(-n) What is f(1)?
17
18
b)
c)
c Z, n Z and n 0
19
20
21
Basis step: P
Second basis step: x P when x
How many binary strings of length n that do not contain the pattern 11?
n = 0: 1 string (, the empty string) n = 1: 2 strings (0 and 1) n = 2: 3 strings (00, 01, 10) n = 3: 5 strings (000 001 010 100 101) (000, 001, 010, 100, n = 4: 8 strings (0000, 0001, 0010, 0100, 1000, 0101, 1001, 1010)
How many binary strings of length n that do not contain the pattern 11?
n = 2: 3 strings (00, 01, 10) n = 3: 5 strings (000, 001, 010, 100, 101) n = 4: 8 strings (0000, 0001, 0010, 0100, 1000, 0101, 1001, 1010) The strings of n=4 can be divided into two classes: X = { 0000, 0001, 0010, 0100, 0101 } and Y = { 1000, 1001, 1010 } X can be obtained from n = 3: adding a leading 0 Y can be obtained from n = 2: adding leading 10.
25
How many binary strings of length n that do not contain the pattern 11?
Let Sn be the set of binary strings of length n that do not contain the pattern 11. For any string x in Sn-1, y = 0x is a string of Sn. For any string x in Sn-2, z = 10x is a string of Sn. A string of Sn i either y or z. Any t i f is ith Hence |Sn| = |Sn-1| + |Sn-2| From |S0| = 1, |S2| = 2, we can compute any |Sn|.
26
28
30
10
S2 (n, k ) =
k 1 k (1)k j j j n k ! j =0
33
11
34
35
12