e.g. 1, 1, 2, 3, 5, 8, 13, 21, 34, … Fibonacci Numbers (introduced by

e.g.
Section 2.4 Recursion and 2.5 Recurrence Relations
Recursion: used to describe something which is infinite.
2, 4, 8, 16, 32, …
use recurrence relation
S(1) = 2
to describe the list
S(n) = 2S(n-1) ∀n ≥ 2
e.g. S(5) = 2S(4) = 2(2S(3)) = 4*S(3) = 4*(2*S(2))
Many definitions can be defined recursively .
= 2(2(2(2(S(1) )))) = 32
e.g. propositional wff.
¾Many sequences of numbers can also be defined
recursively.
2
apply the def. recursively
Note: If the set of numbers is finite, to describe the
set, we can enumerate them. But if the set contains
1
infinite number of elements, we cannot.
e.g.
given
until the base is reached
2
T(1) = 1
T(n) = T(n-1) + 3
∀n≥2
e.g. 1, 1, 2, 3, 5, 8, 13, 21, 34, …
Fibonacci Numbers
What are the 1st five values?
(introduced by Leonardo Fibonacci
T(1) = 1
in 1202)
T(2) = T(1) + 3 = 4
T(3) = T(2) + 3 = 7
F(1) = F(2) = 1
T(4) = T(3) + 3 = 10
F(n) = F(n-1) + F(n-2)
T(5) = 13
∀n≥2
3
For example: PASCAL is an identifier
¾ Many sets can also be defined recursively.
e.g.
4
Q <id> ::= <id><letter> = <id>L
to define that the set of identifiers to be the set of strings
starting from a letter followed by any combination of
letters and digits.
= <id><letter>L = <id>AL
…
= <id><letter> SCAL = <id>ASCAL
Use Backus Normal From(BNF) to define it:
= <letter>ASCAL = PASCAL
<id>
<id> ::= <letter> | <id><letter> |<id><digit>
<id>
<letter> ::= a|b|c …|z|A|B|…Z
<id>
<digit> ::= 0|1| …9
<id>
<id>
<id>
5
< letter >
<letter>
<letter>
<letter>
< letter >
< letter >
6
1
e.g.
give a recursive definition for a set of palindromes
over ∑={0, 1}, that is, strings that read the same
forwards and backwards.
e.g.
E.g. : 00111100, 010, …
empty string
write a recursive function for an arithmetic
progression with initial term a and common
difference d, a, a+d, a+2d, …
S(1) = a
S(n) = S(n-1)+d
∀n≥2
1. e, 0, 1 are palindromes.
2. If X is a palindrome, so are 0X0 and 1X1.
7
e.g.
8
¾How to solve recurrence relation:
e.g. S(1) = 2
S(n) = 2S(n-1)
∀n≥2
write a recursive function for an geometric
progression with initial term a and common
ratio r , a, ar, ar2, …
S(n) = 2S(n-1)
= 2*2*S(n-2) = 22*S(n-2)
= 22*2*S(n-3) = 23*S(n-3)
…
= 2k*S(n-k)
S(1) = a
S(n) = r*S(n-1)
∀n≥2
∴when k = n-1, S(n) = 2n-1*S(1) = 2n-1*2 = 2n
9
e.g.
T(1) = 1
T(n) = T(n-1)+3
10
e.g.
∀n≥2
T(n) = T(n-1) + 3
= ( T(n-2) + 3 ) + 3 = T(n-2) + 3+ 3
= ( T(n-3) + 3) + 3+ 3
…
T(1) = 1
T(n) = 2T(n-1) + 1
∀n≥2
T(n) = 2T(n-1) + 1
= 2(2T(n-2)+1) + 1
= 22T(n-2)+2+1
= 22(2T(n-3)+1)+2+1
= 23T(n-3)+22+21+20
…
= 2kT(n-k)+2k-1+2k-2+ … + 21+20
= T(n-k) + k*3
∴when k = n-1,
T(n) = T(1) + (n-1)*3 = 1+3n – 3 = 3n-211
12
2
∴ when k = n-1
T(n) = 2n-1T(1)+2n-2+2n-3+…+20
= 2n-1+2n-2+ … +20
= 1*(2n-1)
= 2n-1
2-1
13
3