Chapter 8 Recursion 8.2 Solving Recurrence Relations by Iteration Recursion • A Sequence can be defined as: – informally be providing a few terms to demonstrate.
Download ReportTranscript Chapter 8 Recursion 8.2 Solving Recurrence Relations by Iteration Recursion • A Sequence can be defined as: – informally be providing a few terms to demonstrate.
Chapter 8 Recursion 8.2 Solving Recurrence Relations by Iteration Recursion • A Sequence can be defined as: – informally be providing a few terms to demonstrate the pattern, i.e. 3, 5, 7, …. – give an explicit formula for it nth term, i.e. n an 1 n 1 – or, by recursion which requires a recurrence relation. Recursion by Iteration • Method of Iteration • most basic method for finding explicit formula • given a sequence a0, a1, … defined by a recurrence relation and initial conditions, calculate successive terms until a pattern emerges • define a formula based on the pattern Example • Finding explicit formula – Let a0, a1, a2,… be the sequence defined as (k≥1): 1. ak = ak-1+ 2 (recurrence relation) 2. a0 = 1 (initial condition) – Use iteration to guess formula. – Solution • ak = ak-1+ 2 k≥1, • a1 = a0 + 2, a2 = a1 + 2, a3 = a2 + 2, a4 = a3 + 2 … • an = an-1 + 2 Example – Solution • • • • • • • ak = ak-1+ 2 k≥1, a0 = 1 a1 = a0 + 2, (1) + 2, 1+ 1x2 a2 = a1 + 2, (1 + 2) + 2, 1 + 2x2 a3 = a2 + 2, (1 + 2 + 2) + 2, 1 + 3x2 a4 = a3 + 2, (1 + 2 + 2 + 2) + 2, 1 + 3x2 an = an-1 + 2 = 1 + nx2 – Guess • an = 2n + 1 – Check by solving for some known number of n’s. Definition • Arithmetic Sequence – A sequence is called an arithmetic sequence if, and only if, there is a constant d such that • ak = ak-1 + d, k≥1, or • an = a0 + dn , n≥0 – Arithmetic sequence is a sequence in which the current term equals the previous term plus a fixed constant. (Note: illustrated in previous example) Example • Arithmetic sequence – In a vacuum an object under gravity will fall 9.8 meters farther from one second to the next. – A skydiver falls 4.9 meters b/n 0 & 1 second, 4.9 + 9.8=14.7 meters b/n 1 & 2 sec, how far will the skydiver had falling b/n 60 & 61 secs? – Solution • • • • dk = dk-1 + 9.8 meters, k≥1 since dk is an arithmetic sequence it can be rewritten as: dn = d0 + nx(9.8 meters), n≥0 , d0 = 4.9 meters d60 = 4.9 + 60(9.8) = 592.9 meters Geometric Sequence • Geometric sequence is a sequence where each term equals the previous term times a fixed constant. • Geometric sequences are found in population growth models, compounding interest (financial), number of operations for an algorithm, etc. • Definition – A sequence is called a geometric sequence if, and only if, there is a constant r such that • ak = r ak-1 , k≥1 Or • an = a0 rn , n≥0 Example • Geometric sequence – A bank pays 4% per year of compounded interest, If the initial amount deposited is $100,000, how much will the account be worth in 21 years? In how many years will the account be worth $1,000,000? – Solution • • • • • • • an = a0 rn , n≥0, a0 = $100,000 and r = 1.04 (1 + int rate) a21 = $100,000 x (1.04)21 = $227,876.81 at = $100,000 x (1.04)t = $1,000,000, solve for t (1.04)t = 1,000,000/100,000 = 10 t ln(1.04) = ln(10) t = ln(10) / ln (1.04) = 58.7, hence it would take 58.8 years to compound $100,000 into $1,000,000 at 4% interest rate. Formula Simplification • Geometric sequence –1+r+ r2 +…+ n 1 n r 1 r = r 1 • Sum of first n integers ,n Z | n 0 – 1 + 2 + 3 + … + n = nn 1 ,n Z | n 1 2 Example • Tower of Hanoi using Iteration – mk = 2mk-1 + 1 (recurrence) – m1 = 1 – By Iteration • • • • • • • m1 = 1 m2 = 2m1 + 1 = 2(1) + 1 = 21 + 20 m3 = 2m2 + 1 = 2(2+1) + 1 = 22 + 21 + 20 m4 = 2m3 + 1 = 2(2*2 + 2*1 + 1)+1 = 23 + 22 + 21 + 20 m5 = 2m4 + 1 = 2(2*2*2 + 2*2*1 + 2*1 + 1) + 1 =24+23+22+21+20 … mn = 2n-1 + 2n-2 + 2n-3 + … + 21 + 20 – From prior slide (sum of a geometric sequence, r=2) • 2n-1 + 2n-2 + 2n-3 + … + 21 + 20 = 2 n 1 2 1 n • mn = 2 - 1 Checking with Mathematical Induction • Verifying Tower Hanoi Solution – Problem: verify that sequence (m1, … , mn) is defined by mk = 2mk-1 + 1, k≥2 and m1=1, then mn = 2n – 1. • Given: mk = 2mk-1 + 1, k≥2 (recurrence) m1=1 (initial) • Show: mn = 2n – 1 • Proof 1. Basis (n=1) m1=1 (initial) and m1 = 21 – 1 = 2 – 1 = 1 2. Inductive (k≥1) holds for n=k, then it holds for n=k+1 – – – – – mk = 2k – 1 (k≥1), show that mk+1 = 2k+1 – 1 mk+1 = 2mk+1-1 + 1 (mk = 2mk-1 + 1, apply k+1+ = 2mk + 1 (subs in mk = 2k – 1 ) = 2(2k – 1 ) + 1 = 2k+1 – 2 + 1 = 2k+1 – 1 hence, left side (mk=2mk-1 + 1) is equal to right side (mk+1 =2k+1 – 1)