Lecture 21
Lecture 21
LECTURE # 21
First of all instead of giving the definition of Recursion we give you an example, you
already know the Set of Odd numbers Here we give the new definition of the same set
that is the set of Odd numbers.
Definition for odd positive integers may be given as:
BASE:
1 is an odd positive integer.
RECURSION:
If k is an odd positive integer, then k + 2 is an odd positive integer.
Now, 1 is an odd positive integer by the definition base.
With k = 1, 1 + 2 = 3, so 3 is an odd positive integer.
With k = 3, 3 + 2 = 5, so 5 is an odd positive integer
and so, 7, 9, 11, … are odd positive integers.
Page 1 of 6
© Copyright Virtual University of Pakistan
Discrete Mathematics (MTH202)
EXERCISE:
Find f(2), f(3), and f(4) if f is defined recursively by
f(0) = -1, f(1)=2 and for n = 1, 2, 3, …
f(n+1) = f(n) + 3 f(n - 1)
SOLUTION:
From the recursive definition it follows that
f(2) = f(1) + 3 f (1-1)
= f(1) + 3 f (0)
= 2 + 3 (-1)
= -1
Now in order to find out the other values we will need the values of the preceding .So we
write these values here again
f(0) = -1, f(1)=2 f(n+1) = f(n) + 3 f(n - 1)
f(2) = -1
By recursive formula we have
f(3) = f(2) + 3 f (2-1)
= f(2) + 3 f (1)
= (-1) + 3 (2)
=5
f(4) = f(3) + 3 f (3-1)
= f(2) + 3 f (2)
= 5 + 3 (-1)
=2
THE FACTORIAL OF A POSITIVE INTEGER:
For each positive integer n, the factorial of n denoted n! is defined to be the product of all
the integers from 1 to n:
n! = n·(n - 1)·(n - 2) · · · 3 · 2 · 1
Zero factorial is defined to be 1
0! = 1
EXAMPLE:
0! = 1 1! = 1
2! = 2·1 = 2 3! = 3·2·1 = 6
4! = 4·3·2·1 = 24 5! = 5·4·3·2·1 = 120
6! = 6·5·4·3·2·1= 720 7! = 7·6·5·4·3·2·1= 5040
REMARK:
5! = 5 · 4 · 3 · 2 · 1
= 5 ·(4 · 3 · 2 · 1)
= 5 · 4!
In general,
n! = n(n-1)! for each positive integer n.
THE FACTORIAL FUNCTION DEFINED RECURSIVELY:
We can define the factorial function F(n) = n! recursively by specifying the initial value
of this function, namely, F(0) = 1, and giving a rule for finding F(n) from F(n-1).{(n! =
n(n-1)!}
Thus, the recursive definition of factorial function F(n) is:
1. F(0) = 1
Page 2 of 6
© Copyright Virtual University of Pakistan
Discrete Mathematics (MTH202)
2. F(n) = n F(n-1)
EXERCISE:
Let S be the function such that S(n) is the sum of the first n positive integers. Give a
recursive definition of S(n).
SOLUTION:
The initial value of this function may be specified as S(0) = 0
Since
S(n) = n + (n - 1) + (n - 2) + … + 3 + 2 + 1
= n + [(n - 1) + (n - 2) + … + 3 + 2 + 1]
= n + S(n-1)
which defines the recursive step.
Accordingly S may be defined as:
1. S(0)= 0
2. S(n) = n + S(n - 1) for n ≥ 1
EXERCISE:
Let a and b denote positive integers. Suppose a function Q is defined recursively as
follows:
(a) Find the value of Q(2,3) and Q(14,3)
(b) What does this function do? Find Q (3355, 7)
SOLUTION:
0 if a 〈b
Q ( a, b) =
Q(a − b, b) + 1 if b ≤ a
F2 = F1 + F0 = 1 + 1 = 2
Page 3 of 6
© Copyright Virtual University of Pakistan
Discrete Mathematics (MTH202)
F3 = F2 + F1 = 2 + 1 = 3
F4 = F3 + F2 = 3 + 2 = 5
F5 = F4 + F3 = 5 + 3 = 8
.
.
.
RECURRENCE RELATION:
A recurrence relation for a sequence a0, a1, a2, . . . , is a formula that relates each term ak
to certain of its predecessors ak-1, ak-2, . . . , ak-i ,
where i is a fixed integer and k is any integer greater than or equal to i. The initial
conditions for such a recurrence relation specify the values of
a0, a1, a2, . . . , ai-1.
EXERCISE:
Find the first four terms of the following recursively defined sequence.
b1 = 2
bk = bk-1 + 2 · k, for all integers k ≥ 2
\
SOLUTION:
b1 = 2 (given in base step)
b2 = b1 + 2 · 2 = 2 + 4 = 6
b3 = b2 + 2 · 3 = 6 + 6 = 12
b4 = b3 + 2 · 4 = 12 + 8 = 20
EXERCISE:
Find the first five terms of the following recursively defined sequence.
t0 = – 1, t1 = 1
tk = tk-1 + 2 · tk-2, for all integers k ≥ 2
SOLUTION:
Page 4 of 6
© Copyright Virtual University of Pakistan
Discrete Mathematics (MTH202)
SOLUTION:
The sequence is given by the formula
n
bn = 5
Substituting k for n we get
k
bk = 5 . . . . . (1)
Substituting k – 1 for n we get
k-1
bk-1 = 5 . . . . . (2)
Multiplying both sides of (2) by 5 we obtain
k
5 · bk-1 = 5 · 5 – 1
k
= 5 = bk using (1)
Hence bk = 5bk-1 as required
EXERCISE:
n
Show that the sequence 0, 1, 3, 7, . . . , 2 – 1, . . . , for n ≥ 0, satisfies the recurrence
relation
dk = 3dk-1 – 2dk-2, for all integers k ≥ 2
SOLUTION:
The sequence is given by the formula
n
dn = 2 – 1 for n ≥ 0
k-1
Substituting k – 1 for n we get dk-1 = 2 –1
k-2
Substituting k – 2 for n we get dk-2 = 2 – 1
We want to prove that
dk = 3dk-1 – 2dk-2
k k
R.H.S. = 3(2 – 1 – 1) – 2(2 – 2 – 1)
k k
=3·2 –1–3–2·2 –2+2
k k
=3·2 –1–2 –1–1
k
= (3 – 1) · 2 – 1 – 1
k k
=2·2 –1–1= 2 –1=d = L.H.S.
k
THE TOWER OF HANOI:
The puzzle was invented by a French Mathematician Adouard Lucas in 1883. It is well
known to students of Computer Science since it appears in virtually any introductory text
on data structures or algorithms.
There are three poles on first of which are stacked a number of disks that decrease in size
as they rise from the base. The goal is to transfer all the disks one by one from the first
pole to one of the others, but they must never place a larger disk on top of a smaller one.
Let mn be the minimum number of moves needed to move a tower of n disks from one
pole to another. Then mn can be obtained recursively as follows.
• m1 = 1
• mk = 2 mk-1 + 1
m2 = 2 · m1 + 1 = 2 · 1 + 1 = 3
Page 5 of 6
© Copyright Virtual University of Pakistan
Discrete Mathematics (MTH202)
m3 = 2 · m2 + 1 = 2 · 3 + 1 = 7
m4 = 2 · m3 + 1 = 2 · 7 + 1 = 15
m5 = 2 · m4 + 1 = 2 · 15 + 1 = 31
m6 = 2 · m5 + 1 = 2 · 31 + 1 = 65
Note that
n
mn = 2 – 1
64
m64 = 2 – 1
≅ 584.5 billion years
USE OF RECURSION:
At first recursion may seem hard or impossible, may be magical at best. However,
recursion often provides elegant, short algorithmic solutions to many problems in
computer science and mathematics.
Examples where recursion is often used
• math functions
• number sequences
• data structure definitions
• data structure manipulations
• language definitions
Page 6 of 6
© Copyright Virtual University of Pakistan