Post on 22-Dec-2015
Recursion
Lecture 17: Nov 11
Quiz
int hello(int n){
if (n==0)return 0;
elseprintf(“Hello World %d\n”,n);hello(n-1);
}
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() is reversed?
Computing Sum of Arithmetic Progression
int AP(int n){
if (n==0)return 0;
elsereturn (n+AP(n-1));
}
Many programs can be written in a recursive way.
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
int EX(int n){
if (n==0)return 1;
elsereturn (EX(n-1)+EX(n-1));
}
How many function calls if I run EX(n)?
This function is to compute 2n.
2n times.
If we replace the last line by return 2EX(n-1),
then the program will compute the same thing,
but there will be only n function calls.
Recursively Defined Sequences
We can also define a sequence by specifying its recurrence relation.
•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, ai+1=iai/(i+1)
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 =1, r0 = 0
Rabbit Populations
How many rabbits after n months?
wn::= # newborn pairs after n months
rn::= # reproducing pairs after n months
r1 = 1
rn = rn-1 + wn-1
wn = rn-1 so
rn = rn-1 + rn-2
It was Fibonacci who was studying rabbit population growth.
Rabbit Populations
We will compute the closed form for rn later…
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.
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.
How do we compute it using r1,r2,…,rn-1?
0 +0000000000000000000000000000000001…1010101010101010101
The set of all (n-1)-bit
strings without 11.
Totally rn-1 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?
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.
10 +00000000000000000000000000000001…101010101010101010
The set of all (n-2)-bit
strings without 11.
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?
10 +00000000000000000000000000000001…101010101010101010
The set of all (n-2)-bit
strings without 11.
Totally rn-2 of them.
0 +0000000000000000000000000000000001…1010101010101010101
The set of all (n-1)-bit
strings without 11.
Totally rn-1 of them.
Therefore, rn = rn-1 + rn-2
In-Class Exercise
How many n-bit string without the bit pattern 111?
Let rn be the number of such strings.
10 + rn-2
0 + rn-1
rn = rn-1 + rn-2 + rn-3
110 +rn-3
Domino
Given a 2xn puzzle, how many ways to fill it with dominos (2x1 tiles)?
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?
Domino
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.
rn-1 to fill the remaining 2x(n-1) puzzle
rn-2 to fill the remaining 2x(n-2) puzzle
Case 1: put the domino vertically
Case 2: put the domino horizontally
Therefore, rn = rn-1 + rn-2
Parenthesis
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?
Parenthesis
How many valid ways to add n pairs of parentheses?
Let rn be the number of ways to add n pairs of parentheses.
Case 1: ()--------------------
rn-1 ways to add the remaining n-1 pairs.
Case 2: (--)------------------
rn-2 ways to add the remaining n-2 pairs.1 way to add 1 pair
Case 3: (----)----------------
rn-3 ways to add the remaining n-3 pairs.2 ways to add 2 pairs
So there are 2xrn-3 in this case.
So there are rn-2 in this case.
So there are rn-1 in this case.
Parenthesis
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: (----------)----------
rn-k-1 ways to add the remaining n-k-1 pairs.rk ways to add k pairs
By the product rule, there are rkrn-k-1 ways in case k.
The cases are depended on the position of the matching
closing parenthesis of the first opening parenthesis,
and so these cases are disjoint.
Therefore, by the sum rule,
Parenthesis
How many valid ways to add n pairs of parentheses?
It turns out that rn has a very nice formula:
Unfortunately we won’t derive it in this course…
This is called the Catalan number.
There are many combinatorial applications of this formula.
Number of Partitions
How many ways to partition n elements into r non-empty groups?
S(4,4)=1 {x1} {x2} {x3} {x4}
{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}
{x1 x2} {x3} {x4}{x2 x3} {x1} {x4}{x1 x3} {x2} {x4}{x2 x4} {x1} {x3}{x1 x4} {x2} {x3}{x3 x4} {x1} {x2}
S(4,2)=7
S(4,3)=6
Number of Partitions
How many ways to partition n elements into r non-empty groups?
(page 470-472 of the textbook)
Case 1: The element n is in its own group.
{xn} ……………
Let S(n,r) be the number of ways to partition n elements into r groups.
Then any partition of the remaining n-1 elements into r-1 groups can be appended to form a parititonof n elements into r groups.
So there are S(n-1,r-1) ways in this case.
Number of Partitions
How many ways to partition n elements into r non-empty groups?
Case 2: The element n is NOT in its own group.
Let S(n,r) be the number of ways to partition n elements into r groups.
In this case, for any partition of n elements into r groups,map this into a partition of n-1 elements into r groups.
{x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12,xn}
{x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12}This is a partition counted in S(n-1,r).
This mapping is a r-to-1 mapping.
So there are rS(n-1,r) ways to partition in this case.
Number of Partitions
How many ways to partition n elements into r non-empty groups?
Case 2: The element n is NOT in its own group.
Let S(n,r) be the number of ways to partition n elements into r groups.
To think of it in another way,
given any partition of the remaining n-1 elements into r groups,
we can extend it in r different ways,
and any partition in case 2 can be obtained in this way.
{x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12}
{xn} {xn} {xn} {xn}
So there are rS(n-1,r) ways to partition in this case.
Number of Partitions
How many ways to partition n elements into r non-empty groups?
(page 470-472 of the textbook)
Case 1: The element n is in its own group.
Let S(n,r) be the number of ways to partition n elements into r groups.
So there are S(n-1,r-1) ways in this case.
Case 2: The element n is NOT in 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-1,r)
Post #1 Post #2 Post #3
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
Can you write a program to solve this problem?
Think recursively!
Post #1 Post #2 Post #3
Move1,2(n)::= Move1,3(n-1);
biggest disk 12;
Move3,2(n-1)
Tower of Hanoi
http://www.mazeworks.com/hanoi/
To move the biggest disk,we must first move the disks on top to another post.
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
Tower of Hanoi
T(A,C,B,3)
Tower_of_Hanoi(origin, destination, buffer, number)
Move 3 from A to C.
T(A,B,C,2)
T(B,C,A,2)
move 2 from A to B
move 2 from B to C
T(A,C,B,1)
T(C,B,A,1)
T(B,A,C,1)
T(A,C,B,1)
move 1 from A to C
move 1 from C to B
move 1 from B to A
move 1 from A to C
1
2
3
4
5
6
7
Tower of Hanoi
•Suppose your friend gave you a program for moving 9 disks.
•Call this program T9.
•Now you want to write a program for moving 10 disks, say T10.
•Then you use T9 to move the first 9 disks from A to B,
move the largest disk from A to C,
and then use T9 again to move the first 9 disks from B to C.
•Once you have a program for T10, you could also write T11 similarly.
•So, in fact, without recursion, you can write a program for
Tower of Hanoi for any n, but one program for each n.
•There is no mystery here.
•Then, you realize that the programs are very similar.
•Like the idea of array of numbers,
you can also write it like an array of functions,
and that’s exactly the idea of the recursive program.
Solving Recurrence
a0=1, ak = ak-1 + 2
a1 = a0 + 2
a2 = a1 + 2 = (a0 + 2) + 2 = a0 + 4
a3 = a2 + 2 = (a0 + 4) + 2 = a0 + 6
a4 = a3 + 2 = (a0 + 6) + 2 = a0 + 8
See the pattern is ak = a0 + 2k = 2k+1
You can verify by induction.
Solving Hanoi Sequence
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
a6 = 2a5 + 1 = 2(16a1 + 15) + 1 = 32a1 + 31 = 63
Guess the pattern is ak = 2k-1
You can verify by induction.
Solving Fibonacci Sequence
a0=0, a1=1, ak = ak-1 + ak-2
a2 = a1 + a0 = 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
Second Order Recurrence Relation
In the book it is called “second-order linear homogeneous recurrence
relation with constant coefficients”.
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 + Bak-2
Find solutions of the form (1, t, t2, t3, t4, …, tn, …)
tk = Atk-1 + Btk-2
That is, suppose ak=tk
t2 = At + B
t2 - At – B = 0
So t is a root of the quadratic equation t2 - At – B = 0.
Example
ak = ak-1 + 2ak-2
Find solutions of the form (1, t, t2, t3, t4, …, tn, …)
So t must be a root of the quadratic equation t2 - t – 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,…)
Example
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,…)
Are there other solutions?
Try (2,1,5,7,17,31,65,…)
(0,3,3,9,15,33,63,…)
(4,5,13,23,49,95,193,…)
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.
(page 490 of the textbook)
This is easy to check anyway.
This says that any linear combination of two solutions for
the recurrence relation is also a solution for the recurrence.
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.
(page 491-493 of the textbook)
If we are given a0 and a1, then C and D are uniquely determined.
The theorem says that all the solutions of the recurrence relationare a linear combination of the two series (1,r,r2,r3,r4,…,rn,…) and (1,s,s2,s3,s4,…,sn,…) defined by the distinct roots of t2 - At – B = 0.
Solving Fibonacci Sequence
a0=0, a1=1, ak = ak-1 + ak-2
First solve the quadratic equation t2 - t – 1 = 0.
So the distinct roots are:
Solving Fibonacci Sequence
a0=0, a1=1, ak = ak-1 + 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:
Multinomial Theorem
Solving these two equations, we get:
Therefore:
Single-Root Case
ak = Aak-1 + Bak-2
Find solutions of the form (1, t, t2, t3, t4, …, tn, …)
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?
ak = Aak-1 + Bak-2
So t is a root of the quadratic equation t2 - At – B = 0.
Another Solution of the Single-Root Case
(0, r, 2r2, 3r3, 4r4, …, nrn, …) also satisfies the recurrence relation.
ak = Aak-1 + Bak-2
Let r be the single root of the quadratic equation t2 - At – B = 0.
Since r is the single root, A=2r and B=-r2.
ak = 2rak-1 - r2ak-2Therefore we just need to verify that
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.
If we are given a0 and a1, then C and D are uniquely determined.
The theorem says that all the solutions of the recurrence relationare a linear combination of the two series (1,r,r2,r3,r4,…,rn,…) and (0,r,2r2,3r3,4r4,…,nrn,…) defined by the only root of t2 - At – B = 0.
Exercise
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.
Plug in a0 and a1, we solve C=1 and D=1/2.
an = 2n + n2n-1.