Docsity

Recurrence Relation, Lecture Notes - Computer Science, Study notes of Number Theory

Partial preview of the text

Download Recurrence Relation, Lecture Notes - Computer Science and more Study notes Number Theory in PDF only on Docsity!

Recurrence Relations;

General Inclusion-Exclusion

Zeph Grunschlag

Agenda

Recurrence relations (Section 5.1)  Counting strings  Partition function Solving recurrence solutions (Section 5.2)  Fast numerical algorithm “dynamic programming”  Closed solutions by telescoping (back-substitution)  Linear recurrences with constant coefficients  Homogeneous case (RHS = 0)  General case Inclusion-Exclusion (Sections 5.5, 5.6)  Standard (2 sets)  “Inclusion-Exclusion-Inclusion” (3 sets)  Generalized (n sets)  Counting onto functions  Derangements  Sieve of Erastothenes (blackboard)

Recursively Defined

Sequences

EG: Recall the Fibonacci sequence:

{fn } = 0,1,1,2,3,5,8,13,21,34,55,…

Recursive definition for {fn }:

INITIALIZE: f 0 = 0, f 1 = 1 RECURSE: fn = fn-1+fn-2 for n > 1.

The recurrence relation is the recursive part

fn = fn-1+fn-2.Thus a recurrence relation for a

sequence consists of an equation that expresses each term in terms of lower terms.

Q: Is there another solution to the Fibonacci recurrence relation?

Recursively Defined

Sequences

A: Yes, for example could give a different set

of initial conditions such as f 0 =1, f 1 = -

in which case would get the sequence

{fn } = 1,-1,0,-1,-2,-3,-5,-8,-13,-21,…

Q: How many solutions are there to the

Fibonacci recursion relation?

Recurrence Relations for

Counting

Often it is very hard to come up with a

closed formula for counting a particular

set, but coming up with recurrence relation

easier.

EG: Geometric example of counting the

number of points of intersection of n lines.

Q: Find a recurrence relation for the

number of bit strings of length n which

contain the string 00.

Recurrence Relations for

Counting

A: an= #(length n bit strings containing 00):

I. If the first n-1 letters contain 00 then so does

the string of length n. As last bit is free to

choose get contribution of 2an-

II. Else, string must be of the form u00 with u a

string of length n-2 not containing 00 and not

ending in 0 (why not?). But the number of

strings of length n-3 which don’t contain 00 is

the total number of strings minus the number

that do. Thus get contribution of 2^ n-3-an-

Solution: an = 2an-1 + 2^ n-3^ - an-

Q: What are the initial conditions:

Financial Recursion Relation

Most savings plans satisfy certain recursion

relations.

Q: Consider a savings plan in which $10 is

deposited per month, and a 6%/year

interest rate given with payments made

every month. If P n represents the

amount in the account after n months,

find a recurrence relation for P n.

Financial Recursion Relation

A: Pn = (1+r)·P n-1 +

where r = 1 + 6%/12 = 1.

Partition Function

Let’s find a recursion relation for the partition

function. There are n possible scenarios for

the number of members on n’s team:

0: n is all by itself  (e.g. {{1,2},{3}})

1: n has 1 friend (e.g. {{1},{2,3}})

2: n has 2 friends (e.g. {{1,2,3}})

n-1: n has n-1 friends on its team.

By the sum rule, we need to count the number of partitions of each kind, and then add together.

Partition Function

Consider the k’ th case:

k : n has k friends

There are C (n-1,k) ways of choosing

fellow members of n’s team.

Furthermore, there are pn-k-1 ways of

partitioning the rest of the n elements.

Using the product and sum rules we

get:

0 1

1

0

p C n n p C n

p p C n n i

n

n

i

n i

Numerical Solutions

Dynamic Programming

Recursion + Lookup Table = Dynamic Programming

Consider a recurrence relation of the form:

an = f (a 0 ,a 1 ,…, an-2,an-1)

Then can always solve the recurrence relation for

first n values by using following pseudocode:

integer-array a(integers n, a 0 )

table 0 = a 0 for(i = 1 to n) tablei = f(table 0 ,table 1 ,…,tablei-1) return table

Dynamic Program

for String Example

Solve an = 2an-1 + 2^ n-3^ - an-3 up to n=7.

Pseudocode becomes:

integer-array a(integer n)

table 0 = table 1 = 0 table 2 = 1 for(i = 3 to n) tablei = 2i-3-tablei-3+2tablei- return table*

L20 19

Dynamic Program

for String Example

Solve an = 2an-1 + 2^ n-3^ - an-3 up to n=7:

i 2 i-3-ai-3+2ai-1 = ai

L20 20

Dynamic Program

for String Example

Solve an = 2an-1 + 2^ n-3^ - an-3 up to n=7:

i 2 i-3-ai-3+2ai-1 = ai