Recursion - Department of Computer Science

Recursion
0657.140B Foundations of Computer Science
§ Recursion
Example
In some cases, recursive definitions are also called inductive
definitions.
When ascending a flight of stairs, an elf can take 1 stair in one stride
or 2 stairs in one stride.
Let an be the number of different ways for the elf to ascend an n-stair
staircase. We wish to obtain a recursive definition for the numbers an ,
n ≥ 1.
The recursive definition of a sequence has two parts:
Step 1: list the first few numbers to get a feel of what’s going on
Recursion is a process of defining an object in terms of itself. It
can be used to define sequences, functions, and sets.
the initial values
the recurrence relation
Ú The number of initial values required depends on the
recurrence relation.
Example
The Fibonacci numbers Fn are defined by
F0 = 1, F1 = 1;
-- initial values
-- recurrence relation
and Fn = Fn −1 + Fn − 2 for n ≥ 2 .
From the definition we can calculate
F2 = F1 + F0 = 1 + 1 = 2
F3 = F2 + F1 = 2 + 1 = 3
F4 = F3 + F2 = 3 + 2 = 5
F5 = F4 + F3 = 5 + 3 = 8
etc.
1-stair staircase:
∴ a1 = 1
2-stair staircase:
∴ a2 = 2
3-stair staircase:
∴ a3 = 3
etc.
Step 2: try and establish the recurrence relation
To reach the nth stair, the elf can
either take a 1-stair stride from the ( n − 1)th stair;
n
n−1
n−2
or
The recurrence relation can also be written as
either Fn +1 = Fn + Fn −1 , n ≥ 1 ;
Fn + 2 = Fn +1 + Fn , n ≥ 0 .
or
take a 2-stair stride from the ( n − 2 )th stair.
n
n−1
n−2
Exercise
A sequence of numbers an are defined by a0 = 1 , a1 = 3 , a2 = 4 , and
an +1 = 2an + an − 2 for n ≥ 2 . Calculate an for 3 ≤ n ≤ 5 .
There are an −1 different ways the elf can ascend n − 1 stairs.
There are an − 2 different ways the elf can ascend n − 2 stairs.
Thus an = an −1 + an − 2 .
Hence the recursive definition is
a1 = 1 , a2 = 2 , and an = an −1 + an − 2 for n ≥ 3 .
1
Frances 23-7-2002
2
Recursion
Recursion
Example
Binary strings are constructed from the alphabet {0,1} with the
restriction that the strings may not contain two consecutive 1’s.
Let Tn be the total number of such strings of length n. We wish to
obtain a recursive definition for the numbers Tn , n ≥ 0.
Step 1: list the first few numbers
Strings of length 0:
Strings of length 1:
Strings of length 2:
Strings of length 3:
etc.
Λ (the empty string)
0, 1
00, 01, 10
000, 001, 010, 100, 101
∴ T0 = 1
∴ T1 = 2
∴ T2 = 3
∴ T3 = 5
Step 2: try and establish the recurrence relation
Let an be the number of strings of length n ending in a 0.
Let bn be the number of strings of length n ending in a 1.
Clearly Tn = an + bn .
Now combine all the information above…
It follows from (2) and (1) that an = Tn −1 .
--- (4)
It follows from (3) and (4) that bn = Tn − 2 .
--- (5)
It follows from (1), (4), and (5) that Tn = Tn −1 + Tn − 2 .
Hence the recursive definition is
T0 = 1, T1 = 2 , and Tn = Tn −1 + Tn − 2 for n ≥ 2 .
Exercise
Straight lines are drawn on a piece of paper so that every pair of lines
intersect but no three lines intersect at a common point.
Let xn be the number of regions formed by n such lines. Show that
x0 = 1, and xn = xn −1 + n for n ≥ 1 .
--- (1)
Consider strings of length n ending in a 0…
The alphabet at the ( n − 1)th position can be either a 0 or a 1.
0 0
1 0
n−1 n
n−1 n
There are an −1 strings of length n − 1 ending in a 0.
There are bn −1 strings of length n − 1 ending in a 1.
--- (2)
Thus an = an −1 + bn −1 .
Consider strings of length n ending in a 1…
The alphabet at the ( n − 1)th position can only be a 0.
0 1
n−1 n
There are an −1 strings of length n − 1 ending in a 0.
--- (3)
Thus bn = an −1 .
3
4
Recursion
Recursion
Exercise
Exercise
Leonardo of Pisa in 1202 modeled the population growth of a colony
of rabbits. His assumptions were
i) Start, at month 0, with a pair (one male, one female) of newborn
rabbits.
ii) A pair of rabbits which are at least one month old produces a new
pair each month.
Let
an be the number of pairs of newborn rabbits after n months,
bn be the number of pairs of rabbits which are at least one month
old after n months, and
Fn be the total number of pairs of rabbits after n months.
a) Complete the following table:
an
bn
n
0
1
0
1
2
3
4
Fn
1
In the Tower of Hanoi game, we have three pegs A, B, and C, with a
pile of n rings on peg A such that each ring has smaller diameter than
the ring immediately below it and thus the largest ring is at the bottom
of the pile. The aim of the game is to move the rings individually
between pegs, at no stage placing a larger ring on top of a smaller, so
that the whole pile is ultimately shifted to another peg, say B.
A
B
Let t n be the minimum number of moves required to shift a pile of n
rings from one peg to another.
a) Find t1 , t 2 , and t3 .
b) Obtain a recurrence relation for t n .
b) Write down the expressions for an and bn .
c) Obtain a recurrence relation for Fn .
5
C
6
Recursion
Recursion
Recursive definitions can be used to define functions of numbers
or other structures such as strings, lists, or trees.
The recursive definition of a function has two parts:
the non-recursive part − terminates the recursion
the recursive part
Recursive definitions of sets have three parts:
the base clause
the recursion clause
the extremal clause − excludes other things (???)
Example
We can define a set S of numbers recursively by
Example
Factorials
0! = 1, and
n! = n × (n − 1)! for n ≥ 1.
Powers
a 0 = 1, and
a n = a × a n −1 for n ≥ 1.
Binomial coefficients
C (n,0) = C (n, n) = 1 for n ≥ 0 , and
C (n, k ) = C (n − 1, k − 1) + C (n − 1, k ) for n ≥ k ≥ 0 .
Pascal’s Triangle
1
n=2
1
1
k=3
k=4
1
1
3
4
1
k=2
1
2
Similarly, the set of positive integers can be defined recursively by
k=1
1
n=1
n=4
Clearly, S = {3,6,9,12,15, K} is the set of all positive multiples of 3.
k=0
n=0
n=3
Base clause:
The number 3 is an element of the set S.
Recursion clause:
If the numbers x and y are both elements of S, then so is their sum
x + y.
Extremal clause:
A number is an element of S only if it is 3 or it can be constructed
from 3 by a finite number of applications of the recursion clause.
6
1
These are examples of recursively-defined functions on numbers. See
later lectures on Strings for examples of recursively-defined functions
on strings.
Exercise
Evaluate 3! using the recursive definition above.
7
Base clause:
The number 1 is a positive integer.
Recursion clause:
If the numbers x and y are both positive integers, then so is their
sum x + y.
Extremal clause:
A number is a positive integer only if it is 1 or it can be
constructed from 1 by a finite number of applications of the
recursion clause.
These are examples of recursively-defined sets containing numbers.
See later lectures on Strings for examples of recursively-defined sets
containing strings.
8