Top Banner
CS322 Week 6 - Wednesday
42

CS322

Feb 22, 2016

Download

Documents

Kyrie

Week 6 - Wednesday. CS322. Last time. What did we talk about last time ? Examples of induction Strong induction Recurrence relations. Questions?. Logical warmup. On the planet Og , there are green people and red people Likewise, there are northerners and southerners - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS322

CS322Week 6 - Wednesday

Page 2: CS322

Last time

What did we talk about last time? Examples of induction Strong induction Recurrence relations

Page 3: CS322

Questions?

Page 4: CS322

Logical warmup On the planet Og, there are green people and red people Likewise, there are northerners and southerners Green northerners tell the truth Red northerners lie Green southerners lie Red southerners tell the truth Consider the following statements by two natives named

Ork and Bork: Ork: Bork is from the north Bork: Ork is from the south Ork: Bork is red Bork: Ork is green

What are the colors and origins of Ork and Bork?

Page 5: CS322

The proof is on fire

Perhaps you believe that you have the correct recurrence relation for perfect squares

Can you prove it? Hint: Use mathematical induction Recursion and induction and two

sides of the same coin The right cross and the left jab, if

you will, of the computer scientist's arsenal

Page 6: CS322

Tower of Hanoi

Imagine that you have a tower of disks such that each is smaller than the one it rests on

Rules:1. There are 3 pegs, and all the disks are

on peg 12. No two disks are the same size3. A larger disk may not be placed on a

smaller disk4. Disks can only be moved one at a time5. Move all the disks to peg 3

Page 7: CS322

Power of Hanoi What'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:1. (Recursively) transfer the top k – 1 disks from

the starting pole to the temporary pole2. Move the bottom disk from the starting pole to

the ending pole3. (Recursively) move the top k – 1 disks from

the temporary pole to the ending pole

Page 8: CS322

Tower of annoy How 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 disks

mk = mk-1 + 1 + mk-1 or mk = 2mk-1 + 1 Clearly, it takes 1 move to move a single

disk, so m1 = 1 Find m2, m3, m4, and m5

Page 9: CS322

Fibonacci We all know and love Fibonacci by now, but where does

it come from? It is actually supposed to model rabbit populations For the first month of their lives, they cannot reproduce After that, they reproduce every single month

Page 10: CS322

Fibonacci rules From this information about rabbit physiology (which is

a simplification, of course) we can think about pairs of rabbits

At time k, rabbits born at time k – 1 will not reproduce Any rabbits born at k – 2 or earlier will, however So, we assume that all the rabbits from time k – 2 have

doubled between time k – 1 and k Thus, our recurrence relation is:

Fk = Fk – 1 + Fk – 2, k 2 Assuming one starting pair of rabbits, our initial

conditions are: F0 = 1 F1 = 1

Page 11: CS322

Compound interest It's boring, but useful Interest is compounded based on some period of

time We can define the value recursively Let i is the annual percentage rate (APR) of

interest Let m be the number of times per year the interest

is compounded Thus, the total value of the investment at the kth

period is Pk = Pk-1 + Pk-1(i/m), k 1 P0 = initial principle

Page 12: CS322

Solving Recurrence Relations

Page 13: CS322

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 – 1

Likewise, 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

Page 14: CS322

Finding explicit formulas by iteration Consequently, we want to be able to

turn recurrence relations into explicit formulas whenever possible

Often, the simplest way is to find these formulas by iteration

The technique of iteration relies on writing out many expansions of the recursive sequence and looking for patterns

That's it

Page 15: CS322

Iteration example

Find a pattern for the following recurrence relation: ak = ak-1 + 2 a0 = 1

Start at the first term Write the next below Do not combine like terms! Leave everything in expanded form

until patterns emerge

Page 16: CS322

Arithmetic sequence In principle, we should use mathematical

induction to prove that the explicit formula we guess actually holds

The previous example (odd integers) shows a simple example of an arithmetic sequence

These are recurrences of the form: ak = ak-1 + d, for integers k ≥ 1

Note that these recurrences are always equivalent to an = a0 + dn, for all integers n ≥ 0

Let's prove it

Page 17: CS322

Geometric sequence

Find a pattern for the following recurrence relation: ak = rak-1, k ≥ 1 a0 = a

Again, start at the first term Write the next below Do not combine like terms! Leave everything in expanded form

until patterns emerge

Page 18: CS322

Geometric sequence

It appears that any geometric sequence with the following form ak = rak-1, k ≥ 1

is equivalent to an = a0rn, for all integers n ≥ 0

This result applies directly to compound interest calculation

Let's prove it

Page 19: CS322

Employing outside formulas Sure, intelligent pattern matching gets you a

long way However, it is sometimes necessary to

substitute in some known formula to simplify a series of terms

Recall Geometric series: 1 + r + r2 + … + rn = (rn+1 –

1)/(r – 1) Arithmetic series: 1 + 2 + 3 + … + n = n(n + 1)/2

Page 20: CS322

Tower of Hanoi solution Find a pattern for the following recurrence

relation: mk = 2mk-1 + 1, k ≥ 2 m1 = 1

Again, start at the first term Write the next below Do not combine like terms! Leave everything in expanded form until

patterns emerge Use the arithmetic series or geometric series

equations as needed

Page 21: CS322

How many edges are in a complete graph? In a complete graph, every node is connected to

every other node If 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 node

Recursively, this means that the number of edges in a complete graph is sk = sk-1 + (k – 1), k ≥ 2 s1 = 0 (no edges in a graph with a single node)

Use iteration to solve this recurrence relation

Page 22: CS322

Mistakes were made!

You might make a mistake when you are solving by iteration

Consider the following recursive definition ck = 2ck-1 + k, k ≥ 1 c0 = 1

Careless analysis might lead you to the explicit formula cn = 2n + n, n ≥ 0

How would this be caught in a proof by induction verification?

Page 23: CS322

Solving Second-Order Linear Homogeneous Relations with Constant CoefficientsStudent Lecture

Page 24: CS322

Solving Second-Order Linear Homogeneous Relations with Constant Coefficients

Page 25: CS322

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 0

These relations are: Second order because they depend on ak-1 and ak-2 Linear because ak-1 and ak-2 are to the first power

and not multiplied by each other Homogeneous because there is no constant term Constant coefficients because A and B are fixed

values

Page 26: CS322

Why do we care? I'm sure you're thinking that this is an

awfully narrow class of recurrence relations to have special rules for

It's true: There are many (infinitely many) ways to formulate a recurrence relation Some have explicit formulas Some do not have closed explicit formulas

We care about this one partly for two reasons1. We can solve it2. It lets us get an explicit formula for Fibonacci!

Page 27: CS322

Pick 'em out Which of the following are second-order linear

homogeneous recurrence relations with constant coefficients?

a) ak = 3ak-1 + 2ak-2b) bk = bk-1 + bk-2+ bk-3c) ck = (1/2)ck-1 – (3/7)ck-2d) dk = d2

k-1 + dk-1dk-2e) ek = 2ek-2f) fk = 2fk-1 + 1g) gk = gk-1 + gk-2h) hk = (-1)hk-1 + (k-1)hk-2

Page 28: CS322

Characteristic equation

We will use a tool to solve a SOLHRRwCC called its characteristic equation

The characteristic equation of ak = Aak-1 + Bak-2 is: t2 – At – B = 0 where t 0

Note that the sequence 1, t, t2, t3, …, tn satisfies ak = Aak-1 + Bak-2 if and only if t satisfies t2 – At – B = 0

Page 29: CS322

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-2

Since t 0, we can divide both sides through by tk-2 t2 = At + B t2 – At – B = 0

Page 30: CS322

Using the characteristic equation Consider ak = ak-1 + 2ak-2 What is its characteristic equation?

t2 – t – 2 = 0 What are its roots?

t = 2 and t = -1 What 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?

Page 31: CS322

Finding other sequences

An infinite number of sequences satisfy the recurrence relation, depending on the initial conditions

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 SOLHRRwCC an = Crn + Dsn, for integers n ≥ 0

Page 32: CS322

Finding a sequence with specific initial conditions Solve the recurrence relation ak = ak-1 + 2ak-2 where a0 = 1 and

a1 = 8 The result from the previous slide says that, for any sequence

rk and sk that satisfy ak an = Crn + Dsn, for integers n ≥ 0

We have these sequences that satisfy ak

1, 2, 22, 23, …, 2n, … 1, -1, 1, -1, …, (-1)n, …

Thus, we have a0 = 1 = C20 + D(-1)0 = C + D a1 = 8 = C21 + D(-1)1 = 2C – D

Solving for C and D, we get: C = 3 D = -2

Thus, our final result is an = 3∙2n – 2(-1)n

Page 33: CS322

Distinct Roots Theorem

We can generalize this result Let sequence a0, a1, a2, … satisfy:

ak = Aak-1 + Bak-2 for integers k ≥ 2 If the characteristic equation t2 – At –

B = 0 has two distinct roots r and s, then the sequence satisfies the explicit formula: an = Crn + Dsn

where C and D are determined by a0 and a1

Page 34: CS322

Now we can solve Fibonacci! Fibonacci is defined as follows:

Fk = Fk-1 + Fk-2, k ≥ 2 F0 = 1 F1 = 1

What is its characteristic equation? t2 – t – 1 = 0

What are its roots?

Thus, 2

51t

nn

n DCF

2

512

51

Page 35: CS322

We just need C and D So, we plug in F0 = 1 and F1 = 1

Solving for C and D, we get

Substituting in, this yields

251

251

251

251

11251

251

11

1

00

0

DCDCF

DCDCF

5251

C 52)51(

D

11

251

51

251

51

251

52)51(

251

5251

nnnn

nF

Page 36: CS322

Single-Root Case

Our previous technique only works when the characteristic equation has distinct roots r and s

Consider sequence ak = Aak-1 + Bak-2 If t2 – At – B = 0 only has a single

root r, then the following sequences both satisfy ak 1, r1, r2, r3, … rn, … 0, r, 2r2, 3r3, … nrn, …

Page 37: CS322

Single root equation

Our 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 SOLHRRwCC an = Crn + Dsn, for integers n ≥ 0

For the single root case, this means that the explicit formula is: an = Crn + Dnrn, for integers n ≥ 0 where C and D are determined by a0 and a1

Page 38: CS322

SOLHRRwCC recap

To solve sequence ak = Aak-1 + Bak-2 Find its characteristic equation t2 –

At – B = 0 If the equation has two distinct roots

r and s Substitute a0 and a1 into an = Crn + Dsn

to find C and D If the equation has a single root r

Substitute a0 and a1 into an = Crn + Dnrn to find C and D

Page 39: CS322

Quiz

Page 40: CS322

Upcoming

Page 41: CS322

Next time…

General recursive definitions Introduction to set theory

Page 42: CS322

Reminders

Homework 4 is due on Friday Read Chapter 6