Download presentation
Presentation is loading. Please wait.
Published byHilda Newman Modified over 8 years ago
1
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007
2
Recursive Sequences A recurrence relation for a sequence {a n } is an equation that defines each term of the sequence as a function of previous terms, from some point on. The initial conditions are equations that specify the values of the first several terms a 0, …, a n – 1.
3
Recursive Sequence Define a sequence {a k } by a 0 = 2, a 1 = 3, a k = a k – 1 + 2a k – 2, for all k 2. The next few terms are a 2 = 7, a 3 = 13, a 4 = 27.
4
The Towers of Hanoi The Towers of Hanoi puzzle has A game board, Three pegs (Peg 1, Peg 2, Peg 3), 10 disks, of 10 different sizes. Initially, the 10 disks are stacked on Peg 1, each disk smaller than the disk below it.
5
The Towers of Hanoi By moving one disk at a time from peg to peg, the object of the puzzle is to reassemble the disks on Peg 3 in the original order. At no point may a larger disk be placed on a smaller disk.
6
The Towers of Hanoi 123 Start
7
The Towers of Hanoi 123 Finish
8
The Towers of Hanoi There is a very simple recursive solution. Reassemble the top 9 disks on Peg 2. Move Disk 10 from Peg 1 to Peg 3. Reassemble the top 9 disks on Peg 3.
9
The Towers of Hanoi 123
10
123
11
123
12
123
13
But how does one reassemble the top 9 disks on Peg 2? It is very simple to reassemble the top 9 disks on Peg 2. Reassemble the top 8 disks on Peg 3. Move Disk 9 from Peg 1 to Peg 2. Reassemble the top 8 disks on Peg 2.
14
The Towers of Hanoi 123
15
123
16
123
17
123
18
But how does one reassemble the top 8 disks on Peg 3? It is very simple to reassemble the top 8 disks on Peg 3. Reassemble the top 7 disks on Peg 2. Move Disk 8 from Peg 1 to Peg 3. Reassemble the top 7 disks on Peg 3.
19
The Towers of Hanoi 123
20
123
21
123
22
123
23
But how does one reassemble the top 7 disks on Peg 2? And so on…
24
The Towers of Hanoi Ultimately, the question becomes, how does one reassemble the top 1 disk on Peg 2? Hmmm….
25
The Towers of Hanoi 123
26
123
27
How many moves will it take? Let m n be the number of moves to reassemble n disks. Then m 1 = 1. m n = 2m n – 1 + 1, for all n 2.
28
Counting Strings Let b k be the number of binary strings of length k that do not contain 11. b 0 = 1, { } b 1 = 2, {0, 1} b 2 = 3, {00, 01, 10} b 3 = 5, {000, 001, 010, 100, 101} What is the pattern?
29
Counting Strings Consider strings of length k, for some k 2, that do not contain 11. If the first character is 0, then the remainder of the string is a string of length k – 1 which does not contain 11. If the first character is 1, then the next character must be 0 and the remainder is a string that does not contain 11.
30
Counting Strings k = 1: {0, 1}
31
Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10}
32
Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010} {100, 101}
33
Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101}
34
Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101} k = 4: {0000, 0001, 0010, 0100, 0101} {1000, 1001, 1010}
35
Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101} k = 4: {0000, 0001, 0010, 0100, 0101, 1000, 1001, 1010}
36
Counting Strings Therefore, b k = b k – 1 + b k – 2, for all k 2. The next few terms are b 3 = b 2 + b 1 = 5, b 4 = b 3 + b 2 = 8, b 5 = b 4 + b 3 = 13.
37
Counting Binary Trees How many binary trees are there with n nodes? Let t n be the number binary trees with n nodes. Clearly, t 0 = 1, t 1 = 1, and t 2 = 2. n = 1 n = 2
38
Counting Binary Trees When n = 2, we have 5 trees:
39
Counting Binary Trees What is the recursive relation?
40
Counting Binary Trees What is the recursive relation? t 2 = 2 1 + 1 1 + 1 2 = 5
41
Counting Binary Trees When n = 2, we have 5 trees: 2 on left, 0 on right 0 on left, 2 on right 1 on left, 1 on right
42
Counting Binary Trees Following this pattern, we compute t 4 = 5 1 + 2 1 + 1 2 + 1 5 = 14. t 5 = 14 1 + 5 1 + 2 2 + 1 5 + 1 14 = 42. t 6 = 42 1 + 14 1 + 5 2 + 2 5 + 1 14 + 1 42 = 132. and so on.
43
Counting Paths In the following figure, how many paths are there from the lower left corner to the upper right corner?
44
Counting Paths Let p n be the number of paths when the bottom has length n. Then clearly, p 0 = 1, p 1 = 1, and p 2 = 2.
45
Counting Paths When n = 3, …
46
Counting Paths When n = 3, …
47
Counting Paths When n = 3, …
48
Counting Paths When n = 3, …
49
Counting Paths When n = 3, …
50
Counting Paths When n = 3, …
51
Counting Paths When n = 3, p 3 = 5.
52
Counting Paths Are there 14 paths when n = 4? What is the connection with binary trees?
53
Counting Parenthesized Expressions How many possible legitimate arrangements are there of n left parentheses and n right parentheses? Let a n be the number of legitimate arrangements. When n = 0, a 0 = 1. When n = 1, a 1 = 1: (). When n = 2, a 2 = 2: ()() and (()).
54
Counting Parenthesized Expressions When n = 3, a 3 = 5: ()()(), ()(()), (())(), (()()), and ((())). What is the connection with binary trees and the paths?
55
Counting r-Partitions An r-partition of a set is a partition of the set into exactly r nonempty subsets. Let A be a set of size n. Let a n, r be the number of distinct r- partitions of A. Special cases a n, n = 1 for all n 1. a n, 1 = 1 for all n 1.
56
Counting r-Partitions Let A = {a, b, c, d}. a 4, 3 = 6 since the 3-partitions are {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}}
57
Counting r-Partitions Consider an r-partition of a set A. Let x A. Either x appears as a set {x} by itself or it doesn’t. If it does, then the remaining sets form an (r – 1)-partition of A – {x}. If it doesn’t, then if we remove x, we have an r-partition of A – {x}.
58
Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}}
59
Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}} Distinct 2-partitions of {b, c, d}
60
Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}} Three copies of same 3-partition of {b, c, d}
61
Counting r-Partitions Therefore, a n, r = a n – 1, r – 1 + r a n – 1, r, for all n 1 and for all r, 1 < r < n.
62
Counting r-Partitions Compute a 4, 3. a 4, 3 = a 3, 2 + 3a 3, 3 = (a 2, 1 + 2a 2, 2 ) + 3(1) = (1+2(1)) + 3 = 6.
63
Counting r-Partitions Compute a 5, 3. a 5, 3 = a 4, 2 + 3a 4, 3 = (a 3, 1 + 2a 3, 2 ) + 3(6) = (1 + 2(a 2, 1 + 2a 2, 2 )) + 18 = (1 + 2(1 + 2)) + 18 = 25.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.