L11 Recursion
L11 Recursion
This
Lecture
Recursion is one of the most important techniques in computer
science.
• 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.
ai+1=ai+d
The Rabbit
Population
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.
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}} )
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
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?
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
• 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?
n
elements r
groups
Number of
Partitions
How many ways to partition n elements into r non-empty
groups?
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
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.
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.
Proof by
example:
35789 1 2 4 6 11
10 12
• 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)?
int AP(int n)
{
if (n==0)
return 0;
else
return (n+AP(n-
1));
}
12;
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.
• 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
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.
1
steps.
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
ak = Aak-1 +
Bak-2
A and B are real numbers and
B≠0
t2 = At + B (divide by tk-2, t ≠
0)
Characteristic
t - At – B =
2
equation of the
0 recurrence relation
= 0.
(i) (1,2,4,8,16,32,64,…)
(ii) (1,-1,1,-1,1,-1,…)
Examp
le
ak = ak-1 +
2ak-2
(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,
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,
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
= 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,
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
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
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
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,
…)
then we know that (1, r, r2, r3, r4, …, rn, …) satisfies the recurrence
relation.
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
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