Recursion - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Recursion

Description:

What graphs have chromatic number one? when there are no edges... How do we estimate the chromatic number of a graph? If there is a complete subgraph of size k, ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 45
Provided by: cse12
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • Lecture 17 Nov 11

2
Quiz
int hello(int n) if (n0) return
0 else printf(Hello World d\n,n) hello(n
-1)
  • What would the program do if I call hello(10)?
  • What if I call hello(-1)?
  • What if the order of printf() and hello() is
    reversed?

3
Computing Sum of Arithmetic Progression
Many programs can be written in a recursive way.
int AP(int n) if (n0) return
0 else return (nAP(n-1))
The way of thinking is quite different. The idea
is very similar to induction. Always try to
reduce it to smaller problems.
4
Computing Exponential Function
This function is to compute 2n.
int EX(int n) if (n0) return
1 else return (EX(n-1)EX(n-1))
How many function calls if I run EX(n)?
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.
5
Recursively Defined Sequences
We can also define a sequence by specifying its
recurrence relation.
  • Arithmetic sequence (a, ad, a2d, a3d, , )
  • recursive definition a0a, ai1aid
  • Geometric sequence (a, ra, r2a, r3a, , )
  • recursive definition a0a, ai1rai
  • Harmonic sequence (1, 1/2, 1/3, 1/4, , )
  • recursive definition a01, ai1iai/(i1)

6
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 1, r0 0

How many rabbits after n months?
7
Rabbit Populations
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.
We will compute the closed form for rn later
8
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 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.
00000000000000000 00000000000000001 101010101010
1010101
The set of all (n-1)-bit strings without
11. Totally rn-1 of them.
0

9
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 cant 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.
0000000000000000 0000000000000001 10101010101010
1010
The set of all (n-2)-bit strings without
11. Totally rn-2 of them.
10

10
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?
00000000000000000 00000000000000001 101010101010
1010101
The set of all (n-1)-bit strings without
11. Totally rn-1 of them.
0

0000000000000000 0000000000000001 10101010101010
1010
The set of all (n-2)-bit strings without
11. Totally rn-2 of them.
10

Therefore, rn rn-1 rn-2
11
In-Class Exercise
How many n-bit string without the bit pattern 111?
Let rn be the number of such strings.
rn-1
0

rn-2
10

rn-3
110

rn rn-1 rn-2 rn-3
12
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?
13
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.
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
14
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?
15
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
So there are rn-1 in this case.
()--------------------
rn-1 ways to add the remaining n-1 pairs.
(--)------------------
Case 2
So there are rn-2 in this case.
1 way to add 1 pair
rn-2 ways to add the remaining n-2 pairs.
(----)----------------
So there are 2xrn-3 in this case.
Case 3
rn-3 ways to add the remaining n-3 pairs.
2 ways to add 2 pairs
16
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,
17
Parenthesis
How many valid ways to add n pairs of parentheses?
It turns out that rn has a very nice
formula Unfortunately we wont derive it in
this course
This is called the Catalan number. There are
many combinatorial applications of this formula.
18
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
19
Number of Partitions
How many ways to partition n elements into r
non-empty groups?
(page 470-472 of the textbook)
Let S(n,r) be the number of ways to partition n
elements into r groups.
Case 1 The element n is in its own group.
xn
Then any partition of the remaining n-1 elements
into r-1 groups can be appended to form a
parititon of n elements into r groups.
So there are S(n-1,r-1) ways in this case.
20
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.
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
This mapping is a r-to-1 mapping.
x1,x5,x2,x6,x7,x3,x11,,x4,x12
This is a partition counted in S(n-1,r).
So there are rS(n-1,r) ways to partition in this
case.
21
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.
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.
22
Number of Partitions
How many ways to partition n elements into r
non-empty groups?
(page 470-472 of the textbook)
Let S(n,r) be the number of ways to partition n
elements into r groups.
Case 1 The element n is in its own group.
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)
23
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.
24
Tower of Hanoi
Can you write a program to solve this problem?
Think recursively!
25
Tower of Hanoi
To move the biggest disk, we must first move the
disks on top to another post.
Move1,2(n) Move1,3(n-1)
biggest disk 1?2 Move3,2(n-1)
http//www.mazeworks.com/hanoi/
26
Tower of Hanoi
Tower_of_Hanoi(int origin, int destination, int
buffer, int number) if (n0) return Tower
_of_Hanoi(origin, buffer, destination,
number-1) printf(Move Disk d from d to
d\n, number, origin, destination) Tower_of_Han
oi(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
27
Tower of Hanoi
Tower_of_Hanoi(origin, destination, buffer,
number)
1
T(A,C,B,1)
move 1 from A to C
T(A,B,C,2)
2
move 2 from A to B
3
T(C,B,A,1)
T(A,C,B,3)
move 1 from C to B
4
Move 3 from A to C.
5
T(B,A,C,1)
move 1 from B to A
T(B,C,A,2)
6
move 2 from B to C
7
T(A,C,B,1)
move 1 from A to C
28
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 thats exactly the idea of the recursive
    program.

29
Solving Recurrence
a01, 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 2k1
You can verify by induction.
30
Solving Hanoi Sequence
a11, 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.
31
Solving Fibonacci Sequence
a00, a11, 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-kak1 an-k-1ak
but this does not give a formula for computing an
32
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 AB1.
33
Geometric-Sequence Solution
ak Aak-1 Bak-2
Find solutions of the form (1, t, t2, t3, t4, ,
tn, )
That is, suppose aktk
tk Atk-1 Btk-2
t2 At B
t2 - At B 0
So t is a root of the quadratic equation t2 - At
B 0.
34
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 t2
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,)
35
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?
36
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.
37
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)
The theorem says that all the solutions of the
recurrence relation are 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.
If we are given a0 and a1, then C and D are
uniquely determined.
38
Solving Fibonacci Sequence
a00, a11, ak ak-1 ak-2
First solve the quadratic equation t2 - t 1 0.
So the distinct roots are
39
Solving Fibonacci Sequence
a00, a11, 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
40
Multinomial Theorem
Solving these two equations, we get
Therefore
41
Single-Root Case
ak Aak-1 Bak-2
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?
42
Another Solution of the Single-Root Case
ak Aak-1 Bak-2
Let r be the single root of the quadratic
equation t2 - At B 0.
(0, r, 2r2, 3r3, 4r4, , nrn, ) also satisfies
the recurrence relation.
Since r is the single root, A2r and B-r2.
ak 2rak-1 - r2ak-2
Therefore we just need to verify that
The right hand side is
which is equal to the left hand side!
43
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,r2,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.
44
Exercise
a01, a13, ak 4ak-1 - 4ak-2
Solve the quadratic equation t2 4t 4. The
only solution is t2.
By the single-root theorem, all solutions are of
the form
an C2n Dn2n.
Plug in a0 and a1, we solve C1 and D1/2.
an 2n n2n-1.
Write a Comment
User Comments (0)
About PowerShow.com