630 likes | 1.02k Views
Recursively Defined Sequences. Lecture 40 Section 8.1 Wed, Apr 11, 2007. 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.
E N D
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007
Recursive Sequences • A recurrence relation for a sequence {an} 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 a0, …, an – 1.
Recursive Sequence • Define a sequence {ak} by • a0 = 2, • a1 = 3, • ak = ak – 1 + 2ak – 2, for all k 2. • The next few terms are • a2 = 7, • a3 = 13, • a4 = 27.
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.
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.
The Towers of Hanoi Start 1 2 3
The Towers of Hanoi Finish 1 2 3
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.
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi • 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.
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi • 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.
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi • But how does one reassemble the top 7 disks on Peg 2? • And so on…
The Towers of Hanoi • Ultimately, the question becomes, how does one reassemble the top 1 disk on Peg 2? • Hmmm….
The Towers of Hanoi 1 2 3
The Towers of Hanoi 1 2 3
The Towers of Hanoi • How many moves will it take? • Let mn be the number of moves to reassemble n disks. • Then • m1 = 1. • mn = 2mn – 1 + 1, for all n 2.
Counting Strings • Let bk be the number of binary strings of length k that do not contain 11. • b0 = 1, {} • b1 = 2, {0, 1} • b2 = 3, {00, 01, 10} • b3 = 5, {000, 001, 010, 100, 101} • What is the pattern?
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.
Counting Strings • k = 1: {0, 1}
Counting Strings • k = 1: {0, 1} • k = 2: {00, 01, 10}
Counting Strings • k = 1: {0, 1} • k = 2: {00, 01, 10} • k = 3: {000, 001, 010} {100, 101}
Counting Strings • k = 1: {0, 1} • k = 2: {00, 01, 10} • k = 3: {000, 001, 010, 100, 101}
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}
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}
Counting Strings • Therefore, • bk = bk – 1 + bk – 2, for all k 2. • The next few terms are • b3 = b2 + b1 = 5, • b4 = b3 + b2 = 8, • b5 = b4 + b3 = 13.
Counting Binary Trees • How many binary trees are there with n nodes? • Let tn be the number binary trees with n nodes. • Clearly, t0 = 1, t1 = 1, and t2 = 2. n = 2 n = 1
Counting Binary Trees • When n = 2, we have 5 trees:
Counting Binary Trees • What is the recursive relation?
Counting Binary Trees • What is the recursive relation? • t2 = 21 + 11 + 12 = 5
Counting Binary Trees • When n = 2, we have 5 trees: 1 on left, 1 on right 0 on left, 2 on right 2 on left, 0 on right
Counting Binary Trees • Following this pattern, we compute • t4 = 51 + 21 + 12 + 15 = 14. • t5 = 141 + 51 + 22 + 15 + 114 = 42. • t6 = 421 + 141 + 52 + 25 + 114 + 142 = 132. • and so on.
Counting Paths • In the following figure, how many paths are there from the lower left corner to the upper right corner?
Counting Paths • Let pn be the number of paths when the bottom has length n. • Then clearly, p0 = 1, p1 = 1, and p2 = 2.
Counting Paths • When n = 3, …
Counting Paths • When n = 3, …
Counting Paths • When n = 3, …
Counting Paths • When n = 3, …
Counting Paths • When n = 3, …
Counting Paths • When n = 3, …