Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views79 pages

L11 Recursion

The document discusses recursion, a fundamental concept in computer science that involves breaking down problems into smaller subproblems. It covers various types of recurrences, such as Fibonacci and Catalan, and provides examples including rabbit populations and bit strings without specific patterns. Additionally, it explores applications of recursion in combinatorial problems like valid parentheses arrangements and stair filling with rectangles.

Uploaded by

J0Ÿ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views79 pages

L11 Recursion

The document discusses recursion, a fundamental concept in computer science that involves breaking down problems into smaller subproblems. It covers various types of recurrences, such as Fibonacci and Catalan, and provides examples including rabbit populations and bit strings without specific patterns. Additionally, it explores applications of recursion in combinatorial problems like valid parentheses arrangements and stair filling with rectangles.

Uploaded by

J0Ÿ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Recursion

This
Lecture
Recursion is one of the most important techniques in computer
science.

The main idea is to “reduce” a problem into smaller problems.

• Setting up recurrences
• Fibonacci recurrence
• Catalan recurrence
• Other recurrences

• Writing recursive
program

• Solving recurrences
Recursively Defined
Sequences
We can define a sequence by specifying relation
between
the current term and the previous terms.

•Arithmetic sequence: (a, a+d, a+2d,


a+3d, …, )
recursive definition: a0=a,

ai+1=ai+d

•Geometric sequence: (a, ra, r2a, r3a, …, )


recursive definition: a0=a, ai+1=rai

•Harmonic sequence: (1, 1/2, 1/3, 1/4, …, )


recursive definition: a0=1,
Rabbit
Populations

The Rabbit
Population

• A mature boy/girl rabbit pair reproduces every


month.
• Rabbits mature after one month.

wn::= # newborn pairs after n months

rn::= # reproducing pairs after n months

• Start with a newborn pair: w0 = ?, r0 = ?


w0 =1, r0 =
0
How many rabbits after n
months?
Rabbit
Populations
wn::= # newborn pairs after n months

rn::= # reproducing pairs after n


months w0 =1, r0 =
0
r1 = 1

rn = rn-1 + wn-1

wn = rn-1

∴ rn = rn-1 + rn-2
It was Fibonacci who was studying rabbit population
growth.
Warm
Up
We will solve counting problems by setting up recurrence relations.

First we use recursion to count something we already know,


to get familiar with this approach.

Let us count the number of elements in pow(Sn)

where Sn = {a1, a2, …, an} is an n-element set.


We know this already
rn = 2n
Let rn be the size of pow(Sn). But we will compute
again
Then r1 = 2, where pow(S1) = {Ф, {a1}}

r2 = 4, where pow(S2) = {Ф, {a1}, {a2}, {a1,a2}}

r3 = 8, where pow(S3) = {Ф, {a1}, {a2}, {a3} {a1,a2} {a1,a3},


{a2,a3}, {a1,a2,a3}}
Warm
Up
Let rn be the size of pow(Sn) where Sn = {a1, a2, …, an} is an n-element
set.

Then r1 = 2, where pow(S1) = {Ф, {a1}}

r2 = 4, where pow(S2) = {Ф, {a1}, {a2}, {a1,a2}}

r3 =main
The 8, where
ideapow(S 3) = {Ф,
of recursion is {a }, {a2},rn{a
to 1define } {a1,aof
in3terms 2}the
{a1,a3},
{a2,a3previous
}, {a1,a2,a
ri.3}}
How to define r3 in terms of r1 and
r2?
pow(S3) = the union of {Ф, {a1}, {a2}, pow(S2
{a1,a2}} )

and {a3, {a1,a3}, {a2,a3},


while
{a1,athe lower sets are obtained by adding a3 to the upper
2,a3}}
sets.
So r3 =
2r2.
Warm
Up
Let rn be the size of pow(Sn) where Sn = {a1, a2, …, an} is an n-
element set.
The main idea of recursion is to define rn in terms of the
previous ri.
pow(Sn) = the union of Sn-1 = {Ф, {a1}, {a2}, …, {a1,a2,
…,an-1}}

and {an, {a1,an}, {a2,an}, …, {a1,a2,


while,a
…,a the lower sets are obtained by adding an to the upper
n-1 n}}
sets.

Every subset is counted exactly So rn = 2rn-


once. 1.

Solving this recurrence relation will show that rn = 2n (geometric


sequence).
Number of Bit Strings without a Specific
Pattern
How many n-bit strings are there without the bit pattern
11?
Let rn be the number of such
strings.
e.g. r1 = |{0, 1}| = 2,

r2 = |{00, 01, 10}| = 3

r3 = |{000, 001, 010, 100, 101}| = 5

r4 = |{0000, 0001, 0010, 0100, 0101, 1000, 1001,


1010}| = 8
Can you see the
pattern?
r4 = |{0000, 0001, 0010, 0100, 0101}
union =5+3
=8
{1000, 1001, 1010}|
Number of Bit Strings without a Specific
Pattern
How many n-bit strings are there without the bit pattern
11?
Let rn be the number of such
strings.
How do we compute it using r1,r2,
…,rn-1?
Case 1: The first bit is 0.
Then any (n-1)-bit string without the bit pattern 11
can be appended to the end to form a n-bit string
without 11.
So in this case there are exactly rn-1 such n-bit strings.
0000000000000000 The set of all (n-1)-
0 + 0 bit
0000000000000000
1 strings without 11.
… Totally rn-1 of them.
1010101010101010
Number of Bit Strings without a Specific
Pattern
How many n-bit string without the bit pattern
11?
Let rn be the number of such
strings.
How do we compute it using r1,r2,
…,rn-1?
Case 2: The first bit is 1.
Then the second bit must be 0, because we can’t have 11.
Then any (n-2)-bit string without the bit pattern 11
can be appended to the end to form a n-bit string without
11.
So in this case there are exactly rn-2 such n-bit strings.
000000000000000 The set of all (n-2)-bit
1 + 0
0 000000000000000 strings without 11.
1 Totally rn-2 of them.

Number of Bit Strings without a Specific
Pattern
How many n-bit string without the bit pattern
11?
Let rn be the number of such
strings.
How do we compute it using r1,r2,
…,rn-1?
0000000000000000 The set of all (n-1)-
0 + 0 bit
0000000000000000
1 strings without 11.
… Totally rn-1 of them.
1010101010101010
000000000000000 The set of all (n-2)-bit
101
1 + 0
0 000000000000000 strings without 11.
1 Totally rn-2 of them.

101010101010101
010 Therefore, rn = rn-1 +
In-Class
Exercise
How many n-bit string without the bit pattern
111?
Let rn be the number of such
strings.

0 + rn-1

1 + rn-2
0

rn-3
11 +
0

rn = rn-1 + rn-2 +
rn-3
Domi
no
Given a 2xn puzzle, how many ways to fill it with dominos (2x1
tiles)? 3 3 3

E.g. There are 3 ways to fill a 2x3 puzzle with


dominos.

Let rn be the number of ways to fill a 2xn puzzle with


dominos.

How do we compute it using r1,r2,


…,rn-1?
Domi
no
Given a 2xn puzzle, how many ways to fill it with dominos (2x1
tiles)?
Let rn be the number of ways to fill a 2xn puzzle with
dominos.
Case 1: put the domino
vertically

rn-1 to fill the remaining 2x(n-1)


puzzle

Case 2: put the domino


horizontally

rn-2 to fill the remaining 2x(n-2)


puzzle

Therefore, rn = rn-1 +
rn-2
This
Lecture

• Setting up recurrences
• Fibonacci recurrence
• Catalan recurrence
• Other recurrences

• Writing recursive
program

• Solving recurrences
Parenthe
sis
How many valid ways to add n pairs of
parentheses?

E.g. There are 5 valid ways to add 3 pairs of


parentheses.
((())) (()()) (())() ()(()) ()
()()

Let rn be the number of ways to add n pairs of


parentheses.
How do we compute it using r1,r2,
…,rn-1?
Parenthe
sis
How many valid ways to add n pairs of
parentheses?
Let rn be the number of ways to add n pairs of
parentheses.

Case ()-------------------- So there are rn-1 in this


1: case.
rn-1 ways to add the remaining n-1
pairs.
Case (--)------------------ So there are rn-2 in this
2: case.
1 way to add 1 rn-2 ways to add the remaining n-2
pair pairs.
Case (----)---------------- So there are 2xrn-3 in this
3: case.
2 ways to add 2 rn-3 ways to add the remaining n-3
pairs pairs.
Parenthe
sis
How many valid ways to add n pairs of
parentheses?
Let rn be the number of ways to add n pairs of
parenthese.
Case (----------)----------
k+1:
rk ways to add k rn-k-1 ways to add the remaining n-k-1
pairs pairs.
By the product rule, there are rkrn-k-1 ways in
case k.
The cases are depended on the position of the
matching
close parenthesis of the first open parenthesis,
and so these cases are disjoint.

Therefore, by the sum


rule,
Parenthe
sis
How many valid ways to add n pairs of
parentheses?

It turns out that rn has a very nice


formula:

We are not deriving it here…

This is called the Catalan number.

There are many combinatorial applications of this


formula.
Stair
s
An n-stair is a collection of squares as follows:
For example 1-stair, 2-stair, and 3-stair are like
this:

How many ways to fill up the n-stair by exactly n


rectangles??
For example, there are 5 ways to fill up the 3-stair by 3
rectangles.
Stair
s
Let rn be the number of ways to fill the n-stair by n
rectangles.
How do we compute it using r1,r2,
…,rn-1?
6
5
4
3
2
1

Given the n-stair, the first observation is that the positions on


the
diagonal (red numbers) must be covered by different
rectangles.
Since there are n positions in the diagonal and we can only use n
rectangles,
each rectangle must cover exactly one red number.
Stair
s
Let rn be the number of ways to fill the n-stair by n
rectangles.
How do we compute it using r1,r2,
…,rn-1?
6
5
4
3
2
1 o

Consider the rectangle R that covers the bottom right corner (marked
with o).
We consider different cases depending on which red number that R
contains.
Stair
s
Let rn be the number of ways to fill the n-stair by n
rectangles.
How do we compute it using r1,r2,
…,rn-1?
6
5
4
3
2
1 o

Suppose R contains 3. Then observe that the 6-stair is divided into 3


parts.
One part is the rectangle R. The other two parts are a 2-stair and a 3-
stair.
Therefore, in this case, the number of ways to fill in the remaining
parts
is equal to r2r3
Stair
s
Let rn be the number of ways to fill the n-stair by n
rectangles.
How do we compute it using r1,r2,
…,rn-1?
6
5
4
3
2
1 o

Similarly suppose R contains 2.


Then the rectangle “breaks” the stair into a 1-stair and a 4-
stair.
Therefore, in this case, the number of ways to fill in the remaining
parts
is equal to r1r4
Stair
s
Let rn be the number of ways to fill the n-stair by n
rectangles.
How do we compute it using r1,r2,
…,rn-1?
n


i

1 o

In general suppose the rectangle contains i


Then the rectangle “breaks” the stair into a (i-1)-stair and a (n-i)-
stair.
Therefore, in this case, the number of ways to fill in the remaining
parts
is equal to ri-1rn-i
Stair
s
The number of ways to fill in the remaining parts is equal to
ri-1rn-i
n


i

1 o

Rectangle R containing different i will form different


configurations,
and each configuration must correspond to one of these cases.

Therefore the total number of ways is


equal to
This
Lecture

• Setting up recurrences
• Fibonacci recurrence
• Catalan recurrence
• Other recurrences

• Writing recursive
program

• Solving recurrences
Number of
Partitions
How many ways to partition n elements into r non-empty
groups?

S(4,4)=1 {x1} {x2}


{x3} {x4}
S(4,2)=
7 {x1 x2} {x3 x4}
{x1 x3} {x2 x4}
{x1 x4} {x2 x3}
{x1} {x2 x3 x4}
{x2} {x1 x3 x4}
{x3} {x1 x2 x4}
{x4} {x1 x2 x3}
S(4,3)=
6 {x1 x2} {x3} {x4}

{x2 x3} {x1} {x4}


{x1 x3} {x2} {x4}

{x2 x4} {x1} {x3}


Number of
Partitions
How many ways to partition n elements into r non-empty
groups?

Let S(n,r) be the number of ways to partition n elements into r


groups.

n
elements r
groups
Number of
Partitions
How many ways to partition n elements into r non-empty
groups?

Let S(n,r) be the number of ways to partition n elements into r


groups.

Case 1: The element n forms its own


group.
{xn}
……………
Then any partition of the remaining n-1 n -1
elements elemen
ts
into r-1 groups can be appended to form a
parititon r-1
group
of
Sonthere
elements into r groups.
are S(n-1,r-1) ways in this s
case.
Number of
Partitions
How many ways to partition n elements into r non-empty
groups?
Let S(n,r) be the number of ways to partition n elements into r
groups.
Case 2: The element n is NOT in its own
group.

given any partition of the remaining n-1 elements into r


groups,
n -1
we can extend it in r different ways,
elemen
and any partition in case 2 can be obtained in this way. ts
{x1,x5},{x2,x6,x7},{x3,x11},……,
{x4,x12}
{x {x {x {x
n} n} n} n}
So there are rS(n-1,r) ways to partition in this
case. r
Number of
Partitions
How many ways to partition n elements into r non-empty
groups?

Let S(n,r) be the number of ways to partition n elements into r


groups.

Case 1: The element n forms its own


group.
So there are S(n-1,r-1) ways in this
case.
Case 2: The element n does NOT form its own
group.
So there are rS(n-1,r) ways to partition in this
case.
These two cases are disjoint, thus by the sum rule, we
have
S(n,r) = S(n-1,r-1) + rS(n-
Tower of
Hanoi

The goal is to move all the disks to


post 3.

The rule is that a bigger disk cannot be placed on top of a smaller


disk.
Tower of
Hanoi
It is easy if we only have 2
disks.

A total of 3
steps.
Tower of
Hanoi
It is not difficult if we only have 3
disks.

Continue in next
page
Tower of
Hanoi
It is not difficult if we only have 3
disks.

A total of seven
steps.
Tower of
Hanoi

Can you write a program to solve this


problem?

Think
recursively!
Tower of
Hanoi
Suppose you already have a program for 3
disks.
Can you use it to solve 4 disks? In order to move the largest disk,
I have to move the top 3 disks first,
and I can use the program for 3
disks.

Then I move the largest


disk.
Then I can use the program to
move
the 3 disks from pole 2 to pole 3.

Since the program requires 7


steps,
the total number of steps is 15.
Tower of
Hanoi
This recursion is true for any
n.
In order to move the largest disk,
I must move the top n-1 disks first,
and I can use the program for n-1
disks.

Then I move the largest


disk.
Then I can use the program again to
move
the n-1 disks from pole 2 to pole 3.

Since the program requires rn-1


steps,
the total number
rn = 2rof steps is 2rn-1 +
n-1 +

1. 1
Merge
Sort
Given a sequence of n numbers,
how many steps are required to sort them into non-decreasing
order?
One way to sort the number is called the “bubble
sort”,
in which every step we search for the smallest
number,
Thismove
and algorithm could
it to the take up to roughly n2
front.
steps.
For example, if we are given the reverse sequence n,n-1,n-
2,…,1.
Every time it will search to the end to find the smallest
number,
and the algorithm takes roughly n+(n-1)+(n-2)+…+1 =
Can we design
n(n+1)/2 steps.a faster Think
algorithm? recursively!
Merge
Sort
Suppose we have a program to sort n/2
numbers.

We can use it to sort n numbers in the following


way.
3847216
5
Divide the sequence into two halves.

Use the program to sort the two halves


independently.
347 125
8 6

With these two sorted sequences of n/2 numbers,


we can merge them into a sorted sequence of n numbers
easily!
Merge
Sort
Claim. Suppose we have two sorted sequence of k
numbers.
We can merge them into a sorted sequence
of 2k numbers in 2k steps.

Proof by
example:
35789 1 2 4 6 11
10 12

To decide the smallest number in the two sequences,


we just need to look at the “heads” of the two
sequences.
So, in one step, we can extend the sorted sequence by one
number.

So the total number of steps for 2k numbers is 2k.


Merge
Sort
35 789 12 4 6 11 1
10 12
35 789 12 4 6 11 1 2
10 12
35 789 12 4 6 11 1 2 3
10 12
35 789 12 4 6 11 1 2 3
10 12 4
35 789 12 4 6 11 1 2 3 4 5
10 12
35 789 12 4 6 11 1 2 3 4 5 6
10 12
35 789 12 4 6 11 1 2 3 4 5 6 7
10 12
35 789 12 4 6 11 1 2 3 4 5 6 7 8
10 12
35 789 12 4 6 11 1 2 3 4 5 6 7 8 9
10 12
35 789 12 4 6 11 1 2 3 4 5 6 7 8 9
10 12 10
35 789 12 4 6 11 1 2 3 4 5 6 7 8 9 10
10 12 11
35 789 12 4 6 11 1 2 3 4 5 6 7 8 9 10 11
10 12 12
Merge
Sort
Claim. Suppose we have two sorted sequence of k
numbers.
We can merge them into a sorted sequence
of 2k numbers in 2k steps.

Suppose we can sort k numbers in Tk steps.

Then we can sort 2k numbers in 2Tk + 2k


steps.

Therefore, T2k = 2Tk + 2k.

If we solve this recurrence,

then we see that T2n ≈ n log2 n.

This is significantly faster than bubble sort!


This
Lecture

• Setting up recurrences
• Fibonacci recurrence
• Catalan recurrence
• Other recurrences

• Writing recursive
program

• Solving recurrences
Qui
int hello(int n) z
{
if (n==0)
return 0;
else
printf(“Hello World %d\
n”,n);
hello(n-1);
}

terminate…
Does not
1. What would the program do if I call hello(10)?

2. What if I call hello(-1)?

3. What if the order of printf() and hello(n-1) is


reversed?

else
hello(n-1);
printf(“Hello World %d\
n”,n);
Computing Sum of Arithmetic
Progression
Many programs can be written in a recursive
way.

int AP(int n)
{
if (n==0)
return 0;
else
return (n+AP(n-
1));
}

The way of thinking is quite different.

The idea is very similar to induction.

Always try to reduce it to smaller


problems.
Computing Exponential
Function
This function is to compute
2n.
int EX(int n)
{
if (n==0)
return 1;
else
return (EX(n-1)+EX(n-
1));
}

How many function calls if I run 2n+1 -1


EX(n)? times.

If we replace the last line by return 2EX(n-


1),
then the program will compute the same
thing,
Computing Exponential
Function
This function is to compute
2n.
int EX(int n)
{
if (n==0)
return 1;
else
return (2*EX(n-
1));
}
Computing Exponential
Function
This function is to compute
int EX(int n) 2n.
{
if (n==0)
return 1;
else
return (EX(n-1) + EX(n-
1));
}
Tower of
Hanoi

To move the biggest disk,


Move1,2(n)::= Move1,3(n-1); we must first move the
disks
biggest disk on top to another post.

12;
Move3,2(n-1)
Tower of
Hanoi
Suppose we already have a function T10(a,b) to move 10 disks from pole a
to pole b.
It is then easy to write a program for
T11(a,b)
T11(a,b)
{
T10(a,c);
printf(“Move Disk 11 from a to b\
n”);
T10(c,b);
}
In general you can write exactly the same program
for Tn,
if we are given a program for Tn-1.
Instead of writing one program for each n,
we can take n as an input and write a recursive
program.
Tower of
Hanoi
Tower_of_Hanoi(int origin, int destination, int buffer, int number)
{
if (n==0)
return;
Tower_of_Hanoi(origin, buffer, destination, number-1);
printf(“Move Disk #%d from %d to %d\n”, number, origin,
destination);
Tower_of_Hanoi(buffer, destination, origin, number-1);
}
This is the power of recursive thinking.

The program is very short,

yet there is no easy way to write it without


recursion
ToH(o, d, b, n) Tower of
{
if (n==0)
Hanoi
Tower_of_Hanoi(origin, destination, buffer,
return; number)
ToH(o, b, d, n-1); T(A,B,C,0
Move n from o to T(A,C,B, 1 )
d; 1) T(B,C,A,0
ToH(b, d, o, n-1); move 1 from A to )
} T(A,B,C,2 2 C T(C,A,B,0
) 3 )
move 2 from A to T(C,B,A,
B 1) T(A,B,C,0
move 1 from C to )
T(A,C,B,3 4 B
)
Move 3 from A to
C. T(B,C,A,0
5 )
T(B,A,C,
1) T(C,A,B,0
move 1 from B to
T(B,C,A,2 6 )T(A,B,C,0
A
) 7 )
move 2 from B to T(A,C,B,
C 1) T(B,C,A,0
move 1 from A to )
C
This
Lecture

• Setting up recurrences
• Fibonacci recurrence
• Catalan recurrence
• Other recurrences

• Writing recursive
program

• Solving recurrences
Step 1: Guess
Solving Step 2: Apply
Recurrence Induction
a0=1, ak = ak-1 + 2

a1 = a 0 + 2

a2 = a1 + 2 = (a0 + 2) + 2 =
a0 + 4

a3 = a2 + 2 = (a0 + 4) + 2 =
a0 + 6

a = a3 + 2 = (a0 + 6) + 2 =
See 4the pattern is ak = a0 + 2k =
a0 + 8
2k+1
You can verify by
induction.
Step 1: Guess
Solving Hanoi Step 2: Apply
Sequence Induction
a1=1, ak = 2ak-1 + 1

a2 = 2a1 + 1 = 3

a3 = 2a2 + 1 = 2(2a1 + 1) + 1 = 4a1 + 3


=7

a4 = 2a3 + 1 = 2(4a1 + 3) + 1 = 8a1 + 7


= 15

a5 = 2a4 + 1 = 2(8a1 + 7) + 1 = 16a1 +


15 = 31
Guess the pattern is ak =
a6 = 2a5 k+ 1 = 2(16a1 + 15) + 1 = 32a1
2 -1
+ 31 = 63
You can verify by
induction.
Solving Merge Sort
Recurrence
T2k <= 2Tk +
2k
If we could guess that Tk is at most k log2k,

then we can prove by induction that T2k is at most 2k

log2(2k).
This is T2k <= 2Tk + 2k
because
<= 2klog2k + 2k

Induction
= 2k(log2k + 1)

Step
= 2k(log2k +
log22)

= 2klog22k
Solving Merge Sort
Recurrence
T2k <= 2Tk +
2k
How could we guess Tk <= k log2k in the first
place? If there are n numbers there are log2n levels.

In each level i we need to solve 2i-1 merge


problems.

Each merge problem in level i has two


subsequences
of n/2i numbers, and so can be solved in 2n/2i = n/2i-
Divid

1
steps.
e

So each level requires a total of (2i-1)(n/2i-1)=n


steps.
Level
3
Since there are log2n levels,
Level
Merg

2 the total number of steps is at most nlog2n.


e

Level
1

Level
0
Solving Fibonacci
Sequence
a0=0, a1=1, ak = ak-1 + ak-2

a2 = a 1 + a 0 = 1

a3 = a2 + a1 = 2a1 + a0 = 2

a4 = a3 + a2 = 2a2 + a1 = 3a1 + 2a0 = 3

a5 = a4 + a3 = 2a3 + a2 = 3a2 + 2a1 = 5a1 + 3a0 = 5

a6 = a5 + a4 = 2a4 + a3 = 3a3 + 2a2 = 5a2 + 3a1 = 8a1 + 5a0 = 8

a7 = a6 + a5 = 2a5 + a4 = 3a4 + 2a3 = 5a3 + 3a2 = 8a2 + 5a1 = 13a1 +


8a0 = 13
See the pattern an = an-kak+1 + an-
k-1ak

but this does not give a formula for


computing an
Solving Fibonacci
Sequence
an =aan n-k
=aak+1n-ka+k+1 + an-k-1ak
an-k-
a0=0, a1=1, ak = ak-1 + ak-2
1ak k=2 =>
k=1a=> n = an-2a3 + an-3a2
a2 = a 1 + a 0 = 1 an =So,an-1aa72 =+ aa7-2
n-2a
a31 + a7-
So, a3a7 2= a7-1a2 + a7-
a3 = a2 + a1 = 2a1 + a0 = 2
2a1 = a5a3 + a4a2
= a= 6a5a2 + +5a3a
3 a 1 2
a4 = a3 + a2 = 2a2 + a1 = 3a1 + 2a0 = 3
= 8a2 + 5a1
a5 = a4 + a3 = 2a3 + a2 = 3a2 + 2a1 = 5a1 + 3a0 = 5

a6 = a5 + a4 = 2a4 + a3 = 3a3 + 2a2 = 5a2 + 3a1 = 8a1 + 5a0 = 8

a7 = a6 + a5 = 2a5 + a4 = 3a4 + 2a3 = 5a3 + 3a2 = 8a2 + 5a1 = 13a1 +


8a0 = 13
See the pattern an = an-kak+1 + an-
k-1ak

but this does not give a formula for


computing an
Second Order Recurrence
Relation

ak = Aak-1 +
Bak-2
A and B are real numbers and
B≠0

For example, Fibonacci sequence is when


A=B=1.
Geometric-Sequence
Solution
ak = Aak-1 + Suppose that
Bak-2 for some t ≠
Find solutions of the form (1, t, t2, t3, t4, …, tn, 0, the
…) sequence (1,
t, t2, t3, t4, …,
That is, suppose tn, …),
ak=tk satisfies, ak =
tk = Atk-1 + Btk- Aak-1 + Bak-2
2

t2 = At + B (divide by tk-2, t ≠
0)
Characteristic
t - At – B =
2
equation of the
0 recurrence relation

So t is a root of the quadratic equation t2 - At – B


= 0.
Examp
le
ak = ak-1 +
2ak-2 t2 - At – B =
0
Find solutions of the form (1, t, t2, t3, t4, …, tn, …)
A = 1; B = 2
So t must be a root of the quadratic equation t - t – 2
2

= 0.

This implies that t=2 or t=-1.

So solutions of the form (1, t, t2, t3, t4, …, tn, …) are:

(i) (1,2,4,8,16,32,64,…)

(ii) (1,-1,1,-1,1,-1,…)
Examp
le
ak = ak-1 +
2ak-2

So solutions of the form (1, t, t2, t3, t4, …, tn, …)


are:

(i) (1,2,4,8,16,32,64,…)

(ii) (1,-1,1,-1,1,-1,1,…)
2
Are there other 1
solutions?
5 = 1 + 2*2
Try (2, 1, 5, 7, 17, 31, 7 = 5 + 2*1
65,…)
17 = 7 + 2*5
(0,3,3,9,15,33,63,…)
31 = 17 + 2*7
(4,5,13,23,49,95,193,…)
65 = 31 + 2*17
How to obtain these
solutions?
Linear Combination of Two
Solutions
If (r0,r1,r2,r3,…) and (s0,s1,s2,s3,…) are solutions to ak = Aak-1 +
Bak-2,

then the sequence (a0,a1,a2,a3,…) defined by the formula

ak = Crk + Dsk
also satisfies the same recurrence relation for any C and D.
Linear Combination of Two
Solutions
If (r0,r1,r2,r3,…) and (s0,s1,s2,s3,…) are solutions to ak = Aak-1 +
Bak-2,

then the sequence (a0,a1,a2,a3,…) defined by the formula

ak = Crk + Dsk
also satisfies the same recurrence relation for any C and D.
Proof:
Since (r0,r1,r2,r3,…) and (s0,s1,s2,s3,…) are solutions to ak = Aak-1 +
Bak-2, we

must have: rk = Ark-1 + Brk-2 and sk = Ask-1 + Bsk-2

Now, Aak-1 + Bak-2 = A (Crk-1 + Dsk-1) + B (Crk-2 + Dsk-2)

= ACrk-1 + ADsk-1 + BCrk-2 + BDsk-2

= C(Ark-1 + Brk-2) + D(Ask-1 + Bsk-2)

= Crk + Dsk
Linear Combination of Two
Solutions
If (r0,r1,r2,r3,…) and (s0,s1,s2,s3,…) are solutions to ak = Aak-1 +
Bak-2,

then the sequence (a0,a1,a2,a3,…) defined by the formula

ak = Crk + Dsk
also satisfies
This the any
says that same recurrence
linear relation
combination of for
twoany C and D.
solutions
for
the recurrence relation is also a solution for the
This is easy to check anyway… see
recurrence.
below:
ak = ak-1 +
rk 2a
(2, 1, 5, k-2
7, 17, 31, 65, 2rk
…) +
sk sk
(0, 3, 3, 9, 15, 33, 63, =
a …) a
k k

(4, 5, 13, 23, 49, 95,


Distinct-Roots
Suppose a sequenceTheorem
(a0,a1,a2,a3,…) satisfies a recurrence relation

ak = Aak-1 + Bak-2
If t2 - At – B = 0 has two distinct roots r and s,
then an = Crn + Dsn for some C and D (where values of C, D are

determined by the values of a0 and a1.

The theorem says that all the solutions of the recurrence relation
are a linear combination of the two series (1,r,r 2,r3,r4,…,rn,…)
and (1,s,s2,s3,s4,…,sn,…) defined by the distinct roots of t2 - At – B
= 0.
If we are given a0 and a1, then C and D are uniquely
determined.
Solving Fibonacci
Sequence
a0=0, a1=1, ak = ak-1 +
ak-2 t2 - At – B =
0
First solve the quadratic equation t2 - t – 1
= 0. A = 1; B = 1

So the distinct roots


are:
Distinct-roots theorem:
Solving Fibonacci If (a0,a1,a2,a3,…) satisfies ak =
Aak-1 + Bak-2 then an = Crn + Dsn
Sequence for some C and D.
[Here, r and s are two distinct
a0=0, a1=1, ak = ak-1 +
roots of t2 - At – B = 0]
ak-2

By the distinct-roots theorem, the solutions satisfies the


formula:

To figure out C and D, we substitute the value of a0


and a1:
Solving Fibonacci
Sequence
Solving these two equations, we
get:

Therefore:
Proof of Distinct-Roots
Theorem
Suppose a sequence (a0,a1,a2,a3,…) satisfies a recurrence relation

ak = Aak-1 + Bak-2
If t2 - At – B = 0 has two distinct roots r and s,
then an = Crn + Dsn for some C and D (C and D are determined by a0


Proof by Strong MI:

and a1.
Single-Root
Case
ak = Aak-1 +
Bak-2
Find solutions of the form (1, t, t2, t3, t4, …, tn,
…)

So t is a root of the quadratic equation t2 - At – B


= 0.

Suppose this quadratic equation has only one root r,

then we know that (1, r, r2, r3, r4, …, rn, …) satisfies the recurrence
relation.

Are there other


solutions?
2r2 = a3 = Aa2 +

Another Solution of the Single-Root Ba1


= Ar
Case =>A = 2r
3r3 = a4 = Aa3 +
ak = Aak-1 +
Ba2
Bak-2
= A2r2 + Br
= 4r3 + Br
Let r be the single root of the quadratic equation t - At
2
– B = -r3/r = -r2
=>B
= 0.
(0, r, 2r2, 3r3, 4r4, …, nrn, …) also satisfies the recurrence
relation.
Since r is the single root, A=2r and
B=-r2.
Therefore we just need to verify ak = 2rak-1 - r2ak-
that 2

The right hand side


is:

which is equal to the left hand


side!
Single-Root
Theorem
Suppose a sequence (a0,a1,a2,a3,…) satisfies a recurrence
relation
ak = Aak-1 + Bak-2
If t2 - At – B = 0 has only one root r,
then an = Crn + Dnrn for some C and D.

The theorem says that all the solutions of the recurrence relation
are a linear combination of the two series (1,r,r 2,r3,r4,…,rn,…)
and (0,r,2r2,3r3,4r4,…,nrn,…) defined by the only root of t2 - At – B
= 0.
If we are given a0 and a1, then C and D are uniquely
determined.
Exercis
e
a0=1, a1=3, ak = 4ak-1 -
4ak-2

Solve the quadratic equation t2 – 4t + 4. The only solution


is t=2.
By the single-root theorem, all solutions are of the
form
an = C2n +
Dn2n.
Pluging in a0 and
a1…
1 = a0 = C20 + D*0*20 3 = a1 = C21 + D*1*21 = 2C + 2D
a0=

a1=
=C =2 + 2D
1

⇒C = 1 ⇒2D = 1
⇒D = ½
we solve C=1 and
D=1/2.
So, an = 2n +
n-1
Quick
Summary

Recursion is a very useful and powerful technique in computer


science.

It is very important to learn to think recursively,

by reducing the problem into smaller problems.

This is a necessary skill to acquire to become a professional


programmer.

Make sure you have more practices in setting up recurrence


relations.

You might also like