570 likes | 578 Views
Explore recursion in sequence generation and solving Towers of Hanoi puzzle, with examples and solutions explained step by step.
E N D
Recursively Defined Sequences Lecture 35 Section 8.1 Fri, Apr 14, 2006
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 game board has three pegs, Peg 1, Peg 2, Peg 3, and 10 disks. • Initially, the 10 disks are stacked on Peg 1, each disk smaller than the disk below it. • By moving one disk at a time from peg to peg, 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. • But how does one reassemble the top 9 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 • 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. • But how does one reassemble the top 8 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 • 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. • But how does one reassemble the top 7 disks on Peg 2? • Etc.
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 • Ultimately, the question becomes, how does one reassemble the top 1 disk on Peg 2? • That really is simple: just move it there.
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 an be the number of moves to reassemble n disks. • Then • a1 = 1. • an = 2an – 1 + 1, for all n 2.
Future Value of an Annuity • Begin with an initial deposit of $d. • At the end of each month • Add interest at a monthly interest rate r. • Deposit an additional $d. • Let ak denote the value at the end of the k-th month. • a0 = d, • ak = (1 + r)ak – 1 + d, for all k 1.
Future Value of an Annuity • The first few terms are • a0 = d. • a1 = (1 + r)a0 + d = 2d + rd. • a2 = (1 + r)a1 + d = 3d + 3rd + r2d. • a3 = (1 + r)a2 + d = 4d + 6rd + 4r2d + r3d. • What is the pattern?
Future Value of an Annuity • We might guess that the nonrecursive formula is an = ((1 + r)n + 1 – 1)(d/r). • We will verify this guess later.
Counting Strings • Let = {0, 1}. • Let ak be the number of strings in * of length k that do not contain 11. • a0 = 1, {} • a1 = 2, {0, 1} • a2 = 3, {00, 01, 10} • a3 = 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, • ak = ak – 1 + ak – 2, for all k 2. • The next few terms are • a3 = a2 + a1 = 5, • a4 = a3 + a2 = 8, • a5 = a4 + a3 = 13.
Counting r-Partitions • An r-partition of a set is a partition of the set into r nonempty subsets. • Let A be a set of size n. • Let an, r be the number of distinct r-partitions of A. • Special cases • an, n = 1 for all n 1. • an, 1 = 1 for all n 1.
Counting r-Partitions • Let A = {a, b, c, d}. • a4, 2 = 7 since the 2-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}}
Counting r-Partitions • Consider an r-partition of a set A. • Let xA. • Either x is in a set {x} by itself or it isn’t. • If it is, then the remaining sets form an (r – 1)-partition of A – {x}. • If it isn’t, then if we remove x, we have an r-partition of A – {x}.
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}}
Counting r-Partitions • The 3-partitions that contain {a}. • {{a}, {b}, {c, d}} {{b}, {c, d}} • {{a}, {c}, {b, d}} {{c}, {b, d}} • {{a}, {d}, {b, c}} {{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}
Counting r-Partitions • The 3-partitions that contain {a}. • {{a}, {b}, {c, d}} {{b}, {c, d}} • {{a}, {c}, {b, d}} {{c}, {b, d}} • {{a}, {d}, {b, c}} {{d}, {b, c}} • The 3-partitions that do not contain {a}. • {{b}, {c}, {a, d}} {{b}, {c}, {d}} • {{b}, {d}, {a, c}} {{b}, {d}, {c}} • {{c}, {d}, {a, b}} {{c}, {d}, {b}} Three copies of same 3-partition of {b, c, d}
Counting r-Partitions • In fact, we get the same r-partition of A – {x} that we would get had x been a member of any other set in the partition. • Thus, each r-partition of A – {x} gives rise to rr-partitions of A. • Therefore, an, r = an – 1, r – 1 + r an – 1, r , for all n 1 and for all r, 1 < r < n.
Counting r-Partitions • Compute a4, 2. and a5, 2. • a4, 2 = a3, 1 + 2a3, 2 = 1 + 2(a2, 1 + 2a2, 2) = 1 + 2(1 + 2 1) = 7. • a5, 2 = a4, 1 + 2a4, 2 = 1 + 2 7 = 15.
Taxicab Routes • In a city, streets run either east-west or north-south. • They form a grid, like graph paper. • Let am, n denote the number of routes that a taxicab may follow to reach a point that is m blocks east and n blocks north of its current location. • Assume the cab travels only east and north.
Taxicab Routes • Special cases • a0, n = 1 for all n 0. • am, 0 = 1 for all m 0. • One block from the destination, he is either one block south or one block west.
One Block South One block south 4 6 Grid
One Block West One block north 4 6 Grid
Taxicab Routes • If he is one block south of the destination, then there are am,n – 1 routes to get to that point, followed by one route (north) to get to the destination.