4 Logical warmup On the planet Og, there are green people and red people Likewise, there are northerners and southernersGreen northerners tell the truthRed northerners lieGreen southerners lieRed southerners tell the truthConsider the following statements by two natives named Ork and Bork:Ork: Bork is from the northBork: Ork is from the southOrk: Bork is redBork: Ork is greenWhat are the colors and origins of Ork and Bork?
5 The proof is on firePerhaps you believe that you have the correct recurrence relation for perfect squaresCan you prove it?Hint: Use mathematical inductionRecursion and induction and two sides of the same coinThe right cross and the left jab, if you will, of the computer scientist's arsenal
6 Tower of HanoiImagine that you have a tower of disks such that each is smaller than the one it rests onRules:There are 3 pegs, and all the disks are on peg 1No two disks are the same sizeA larger disk may not be placed on a smaller diskDisks can only be moved one at a timeMove all the disks to peg 3
7 Power of HanoiWhat's the smallest number of moves needed to move the disks?Consider the following algorithm for moving k disks from the starting pole to the ending pole:(Recursively) transfer the top k – 1 disks from the starting pole to the temporary poleMove the bottom disk from the starting pole to the ending pole(Recursively) move the top k – 1 disks from the temporary pole to the ending pole
8 Tower of annoyHow do we represent the running time of this algorithm recursively?We have to (recursively) move k – 1 disks, then a single disk, then (recursively) another k – 1 disksmk = mk mk-1 ormk = 2mk-1 + 1Clearly, it takes 1 move to move a single disk, so m1 = 1Find m2, m3, m4, and m5
9 FibonacciWe all know and love Fibonacci by now, but where does it come from?It is actually supposed to model rabbit populationsFor the first month of their lives, they cannot reproduceAfter that, they reproduce every single month
10 Fibonacci rulesFrom this information about rabbit physiology (which is a simplification, of course) we can think about pairs of rabbitsAt time k, rabbits born at time k – 1 will not reproduceAny rabbits born at k – 2 or earlier will, howeverSo, we assume that all the rabbits from time k – 2 have doubled between time k – 1 and kThus, our recurrence relation is:Fk = Fk – 1 + Fk – 2, k 2Assuming one starting pair of rabbits, our initial conditions are:F0 = 1F1 = 1
11 Compound interest It's boring, but useful Interest is compounded based on some period of timeWe can define the value recursivelyLet i is the annual percentage rate (APR) of interestLet m be the number of times per year the interest is compoundedThus, the total value of the investment at the kth period isPk = Pk-1 + Pk-1(i/m), k 1P0 = initial principle
13 Recursion … is confusing We don't naturally think recursively (but perhaps you can raise your children to think that way?)As it turns out, the total number of moves needed to solve the Tower of Hanoi for n disks is 2n – 1Likewise, with an interest rate of i, a principle of P0, and m periods per year, the investment will yield Po(i/m + 1)k after k periods
14 Finding explicit formulas by iteration Consequently, we want to be able to turn recurrence relations into explicit formulas whenever possibleOften, the simplest way is to find these formulas by iterationThe technique of iteration relies on writing out many expansions of the recursive sequence and looking for patternsThat's it
15 Iteration exampleFind a pattern for the following recurrence relation:ak = ak-1 + 2a0 = 1Start at the first termWrite the next belowDo not combine like terms!Leave everything in expanded form until patterns emerge
16 Arithmetic sequenceIn principle, we should use mathematical induction to prove that the explicit formula we guess actually holdsThe previous example (odd integers) shows a simple example of an arithmetic sequenceThese are recurrences of the form:ak = ak-1 + d, for integers k ≥ 1Note that these recurrences are always equivalent toan = a0 + dn, for all integers n ≥ 0Let's prove it
17 Geometric sequenceFind a pattern for the following recurrence relation:ak = rak-1, k ≥ 1a0 = aAgain, start at the first termWrite the next belowDo not combine like terms!Leave everything in expanded form until patterns emerge
18 Geometric sequenceIt appears that any geometric sequence with the following formak = rak-1, k ≥ 1is equivalent toan = a0rn, for all integers n ≥ 0This result applies directly to compound interest calculationLet's prove it
19 Employing outside formulas Sure, intelligent pattern matching gets you a long wayHowever, it is sometimes necessary to substitute in some known formula to simplify a series of termsRecallGeometric series: 1 + r + r2 + … + rn = (rn+1 – 1)/(r – 1)Arithmetic series: … + n = n(n + 1)/2
20 Tower of Hanoi solution Find a pattern for the following recurrence relation:mk = 2mk-1 + 1, k ≥ 2m1 = 1Again, start at the first termWrite the next belowDo not combine like terms!Leave everything in expanded form until patterns emergeUse the arithmetic series or geometric series equations as needed
21 How many edges are in a complete graph? In a complete graph, every node is connected to every other nodeIf we want to make a complete graph with k nodes, we can take a complete graph with k – 1 nodes, add a new node, and add k – 1 edges (so that all the old nodes are connected to the new nodeRecursively, this means that the number of edges in a complete graph issk = sk-1 + (k – 1), k ≥ 2s1 = 0 (no edges in a graph with a single node)Use iteration to solve this recurrence relation
22 Mistakes were made!You might make a mistake when you are solving by iterationConsider the following recursive definitionck = 2ck-1 + k, k ≥ 1c0 = 1Careless analysis might lead you to the explicit formulacn = 2n + n, n ≥ 0How would this be caught in a proof by induction verification?
23 Solving Second-Order Linear Homogeneous Relations with Constant Coefficients Student Lecture
24 Solving Second-Order Linear Homogeneous Relations with Constant Coefficients
25 Second-Order Linear Homogeneous Relations with Constant Coefficients Second-order linear homogeneous relations with constant coefficients are recurrence relations of the following form:ak = Aak-1 + Bak-2 where A, B R and B 0These relations are:Second order because they depend on ak-1 and ak-2Linear because ak-1 and ak-2 are to the first power and not multiplied by each otherHomogeneous because there is no constant termConstant coefficients because A and B are fixed values
26 Why do we care?I'm sure you're thinking that this is an awfully narrow class of recurrence relations to have special rules forIt's true: There are many (infinitely many) ways to formulate a recurrence relationSome have explicit formulasSome do not have closed explicit formulasWe care about this one partly for two reasonsWe can solve itIt lets us get an explicit formula for Fibonacci!
27 Pick 'em outWhich of the following are second-order linear homogeneous recurrence relations with constant coefficients?ak = 3ak-1 + 2ak-2bk = bk-1 + bk-2+ bk-3ck = (1/2)ck-1 – (3/7)ck-2dk = d2k-1 + dk-1dk-2ek = 2ek-2fk = 2fk-1 + 1gk = gk-1 + gk-2hk = (-1)hk-1 + (k-1)hk-2
28 Characteristic equation We will use a tool to solve a SOLHRRwCC called its characteristic equationThe characteristic equation of ak = Aak-1 + Bak-2 is:t2 – At – B = 0 where t 0Note that the sequence 1, t, t2, t3, …, tn satisfies ak = Aak-1 + Bak-2 if and only if t satisfies t2 – At – B = 0
29 Demonstrating the characteristic equation We can see that 1, t, t2, t3, …, tn satisfies ak = Aak-1 + Bak-2 if and only if t satisfies t2 – At – B = 0 by substituting in t terms for ak as follows:tk = Atk-1 + Btk-2Since t 0, we can divide both sides through by tk-2t2 = At + Bt2 – At – B = 0
30 Using the characteristic equation Consider ak = ak-1 + 2ak-2What is its characteristic equation?t2 – t – 2 = 0What are its roots?t = 2 and t = -1What are the sequences defined by each value of t?1, 2, 22, 23, …, 2n, …1, -1, 1, -1, …, (-1)n, …Do these sequences satisfy the recurrence relation?
31 Finding other sequences An infinite number of sequences satisfy the recurrence relation, depending on the initial conditionsIf r0, r1, r2, … and s0, s1, s2, … are sequences that satisfy the same SOLHRRwCC, then, for any C, D R, the following sequence also satisfies the SOLHRRwCCan = Crn + Dsn, for integers n ≥ 0
32 Finding a sequence with specific initial conditions Solve the recurrence relation ak = ak-1 + 2ak-2 where a0 = 1 and a1 = 8The result from the previous slide says that, for any sequence rk and sk that satisfy akan = Crn + Dsn, for integers n ≥ 0We have these sequences that satisfy ak1, 2, 22, 23, …, 2n, …1, -1, 1, -1, …, (-1)n, …Thus, we havea0 = 1 = C20 + D(-1)0 = C + Da1 = 8 = C21 + D(-1)1 = 2C – DSolving for C and D, we get:C = 3D = -2Thus, our final result is an = 3∙2n – 2(-1)n
33 Distinct Roots Theorem We can generalize this resultLet sequence a0, a1, a2, … satisfy:ak = Aak-1 + Bak-2 for integers k ≥ 2If the characteristic equation t2 – At – B = 0 has two distinct roots r and s, then the sequence satisfies the explicit formula:an = Crn + Dsnwhere C and D are determined by a0 and a1
34 Now we can solve Fibonacci! Fibonacci is defined as follows:Fk = Fk-1 + Fk-2, k ≥ 2F0 = 1F1 = 1What is its characteristic equation?t2 – t – 1 = 0What are its roots?Thus,
35 We just need C and D So, we plug in F0 = 1 and F1 = 1 Solving for C and D, we getSubstituting in, this yields
36 Single-Root CaseOur previous technique only works when the characteristic equation has distinct roots r and sConsider sequence ak = Aak-1 + Bak-2If t2 – At – B = 0 only has a single root r, then the following sequences both satisfy ak1, r1, r2, r3, … rn, …0, r, 2r2, 3r3, … nrn, …
37 Single root equationOur old rule said that if r0, r1, r2, … and s0, s1, s2, … are sequences that satisfy the same SOLHRRwCC, then, for any C, D R, the following sequence also satisfies the SOLHRRwCCan = Crn + Dsn, for integers n ≥ 0For the single root case, this means that the explicit formula is:an = Crn + Dnrn, for integers n ≥ 0where C and D are determined by a0 and a1
38 SOLHRRwCC recap To solve sequence ak = Aak-1 + Bak-2 Find its characteristic equation t2 – At – B = 0If the equation has two distinct roots r and sSubstitute a0 and a1 into an = Crn + Dsn to find C and DIf the equation has a single root rSubstitute a0 and a1 into an = Crn + Dnrn to find C and D
Your consent to our cookies if you continue to use this website.