Docsity

Solving Recurrence Relations: Ad Hoc, Dynamic Programming, and Linear Homogeneous, Study notes of Discrete Structures and Graph Theory

Partial preview of the text

Download Solving Recurrence Relations: Ad Hoc, Dynamic Programming, and Linear Homogeneous and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

12/4/

CSci 243

Discrete Structures

Lecture 29

Recurrence Relations II

12/4/

Outline^ • Review: Ad hoc Solution Methods

-^

Back-Substitution

-^

Dynamic Programming

  • Example: Solving Fibonacci Recurrence Relation• Linear Homogeneous Recurrence Relations• Take-away Message

12/4/ Review: Number of Line Intersections^ • Remember maximum number of intersections?

-^

We derived a recurrence relation: a

n^

= a

n-

  • n – 1
  • Write a few terms:

a

4

= a

3

But a

3

= a

2

  • 2

a

4

= (a

2

a

4

= (a

1

But a

1

= 0

a

4

  • So: a

n^

= sum(1 .. n–1) =

(^

n

n

12/4/

Review: Ad Hoc Solution Methods^ • We can also solve any recurrence using

dynamic

programming•

Sequence of steps, where the state of the next stepis computed from the previous steps

-^

Compare to

linear programming where we compute

the values of variables to minimize a function

int[]

SolveRecurrence(Rec_Fun

rec_fun,

int

num_to_solve,

int[n

] 0

initial_values)

terms[1..n

] = initial_values 0

for

(i

=

n

+1 0

to

n)

terms[i]

=

rec_fun(terms[1..i-1])

return

terms

12/4/

Solving Types of Rec. Relations^ • But dynamic programming is just what we do by

hand!

  • Is there a better way? Can we solve fib

recurrence analytically?

  • Classify types of recurrence relations• Develop methods for each type• Example types: -^

Linear homogeneous (easy)

-^

Quadratic (hard)

12/4/

Next up… Linear Homogeneous RRs^ • Mandelbrot set based on

quadratic rec. relation

-^

Points C for which z

n^

= zn-

2

  • C does not go to

infinity (z

,C complex numbers, zn

0

= 0)

12/4/

Example: Fibonacci^ •

Fibonacci recurrence: fib

n^

= fib

n-

  • fib

n-

Assume solution of the form a

n^

= r

n

-^

nr = r

n-

  • r

n-

(substituting a

n^

= r

n^ )

-^

(^2) r n-2r

= r

1 r

n-

  • r

n-

(factoring)

-^

(^2) r = r + 1

(dividing by r

n-

)

•^

This is the

characteristic equation of the

recurrence relation

-^

QC: How do we solve for r?

12/4/

Example: Fibonacci (cont)^ 2.

Find all r’s that seem to work•^

(^2) r = r + 1

  • -^

We’ll call these solutions r

1

and r

2

5

1

±

r

2

5

1 1

= r^

2

5

1 2

−

r

12/4/

13

Example: Fibonacci (cont)^ 4.

Use initial conditions to find

α

(and therefore

the specific solution)•^

a^0

= 0, a

1

= 1

  • •^

So

0

2

0

1

2

5

1

2

5

1

0

  

^  

−

  

^  

=

α

α

1 5

1

= α n

n

a^ n

   

^   

−

−    

^   

=

2

5

1 1 5

2

5

1 1 5

2

−

α     

1

2

1

1

2

5

1

2

5

1

1

  

^  

−

  

^  

=

α

α

12/4/ Linear Homogenous RecurrenceRelations with Constant Coefficients^ • We can generalize the previous example•

Linear: mx+b, where m and b are constants

Homogeneous (in this context): only subscriptedterms (like a

) with coefficientsn^

Constant coefficients: coefficients are not f(n)

  • Examples: -^

an

= 3 a

n-

  • 4 a

n-

(OK)

-^

H

n^

= 2 H

n-

  • 1

(not OK—not homogeneous)

-^

an

= 3 a

n-

2

  • 4 a

n-

(not OK—not linear)

-^

an

= n a

n-

  • 4 a

n-

(not OK—non-const. coeff.)

12/4/

  • LHRR with CC or not? If so, what is the degree?

If not, why not?

  • a

n^

= 3a

n-

  • 4a

n-

  • 5a

n-

  • a

n^

= 2na

n-

  • a

n-

  • a

n^

= a

n-

  • a

n^

= a

n-

2

  • a

n^

= 4a

n-

a

n-

  • a

n-

Quick Check

12/4/

Solving LHRR with CC’s^ •

Can use the method from before

-^

Theorem: The sequence is a solution ofn

the recurrence relation a

=cn^

a 1

n-

+c

a 2

n-

iff

a

=n

α

r 1

n 1

α

r 2

n 2

. Here r

1

and r

2

are the two

distinct roots of r

2 –c

r–c 1

=0, n=0,1,2,…, and 2

α

1,

α

2

are constants.

•^

Complications:•^

Repeated roots (can handle this with some tricks)

-^

Complex roots (just use complex numbers)

12/4/

Quick Check^ • Solve: a

n^

= 5a

n-

  • 6a

n-

, a

0

= 1, a

1

12/4/

Take-Away Message^ • You can often solve recurrence relations using

back-substitution. (Works best when next valuein terms of a single previous value.)

  • Can use dynamic programming as well• Otherwise: -^

Identify linear homogeneous recurrence relations

-^

Remember they can be solved systematically

-^

Remember how to do it, or where to look it up