0% found this document useful (0 votes)
5 views

recursion descrete

The document discusses recursion, defining recursive objects, functions, sequences, and sets. It provides examples of recursive definitions, such as factorials and Fibonacci numbers, and explains how strings can be defined and manipulated recursively. Additionally, it includes exercises for further practice on the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

recursion descrete

The document discusses recursion, defining recursive objects, functions, sequences, and sets. It provides examples of recursive definitions, such as factorials and Fibonacci numbers, and explains how strings can be defined and manipulated recursively. Additionally, it includes exercises for further practice on the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Discrete Structures

Week 12
Recursion
Recursive Objects
• Recursion is the process of repeating items in a self-
similar way.

• Sometimes it is difficult to define an object explicitly, but it


is easier to define it in terms of itself.
Recursive Functions

• We can define a function recursively by specifying:


• Basis: the value of the function at the smallest element of the
domain.
• e.g. : f(0) = 1

• Recursive step: A rule for finding the value of the function at an


integer from its values at smaller integers.
• e.g. : f(n+1) = 2 * f(n)

• Many common functions can be defined recursively.


Recursion
• We can use recursion to define sequences, functions, and
sets.

Example:
an = 2n for n = 1,2,…
2,4,8,16,32,…

After giving the first term, each term of the sequence can be defined
from the previous term.
a1=2 an+1= 2an
Example
Suppose
• f(0) = 3
• f(n+1) = 2f(n)+2, ∀𝑛 ≥ 0.
Find f(1), f(2) and f(3).
Solution:
• f(1) =
2f(0) + 2 = 2(3) + 2 = 8
• f(2) =
2f(1) + 2 = 2(8) + 2 = 18
• f(3) =
2f(2) + 2 = 2(18) + 2 = 38
Example
Suppose
• f(0) = -1, f(1) = 2
• f(n+1) = f(n)+3f(n-1), ∀𝑛 ≥ 1.
Find f(2), f(3) and f(4).
Solution:
f(2) = -1
f(3) = 5
f(4) = 2
Examples
• Can you write n! as a recursive function
• F(0) = 1
• F(n) = n * F(n-1)
• What is the value of F(5)?

• F(5) = 5F(4)
= 5 . 4F(3)
= 5 . 4 . 3F(2)
= 5 . 4 . 3 . 2F(1)
= 5 . 4 . 3 . 2 . 1F(0)
= 5 . 4 . 3 . 2 . 1 . 1 = 120
Examples
• Fibonacci Numbers
• F(0) = 0, F(1) = 1
• F(n) = F(n-1) + F(n-2) for n = 2, 3, 4,……
• Find the Fibonacci number F(4).
Recursively Defined Sets
• Assume S is a set.
• We use two steps to define the elements of S.
• Basis step:
• Specify an initial collection of elements.
• Recursive step:
• Give a rule for forming new elements from those
already known to be in S.
Recursively Defined Sets
• Consider S ⊆ Z defined by
• Basis step: (Specify initial elements.)
• 3S
• Recursive step: (Give a rule using existing elements)
• If x  S and y  S, then x + y  S.
• 3S
• 3+3=6S
• 6+3=9S
• 6 + 6 = 12  S
• …
• This is the set of all positive multiples of 3.
Strings can be defined Recursively
• Finite sequences of form a1, a2, a3…, an, are called strings.
• The set * of strings over the alphabet  can be defined
by
• Basis: *
• Recursive Step:
If w  * and x  , then wx  *.
wx: string w followed by symbol x
where
• : Alphabet (set of symbols)
• : Empty String
• *: set of all strings over the alphabet
Recursive Strings

• Let  = {a, b, c}.

• * = {, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa,
aab,…}
String Example
• Two strings can be combined via the operation of
concatenation.
• Let  be the set of symbols and * the set of strings
formed from symbols in . We can define the
concatenation of two strings, denoted by ‘.’ symbol ,
recursively as follows.
• Basis Step:
• If w  *, then w .  = w, where  is the empty string.

• Recursive Step:
• If w1  * and w2  * and x  , then w1 . (w x) = (w . w )x.
2 1 2)
String Example
• Recursive definition of the length of strings (the length of
string w.
• Basis Step:
• L() = 0
• Recursive Step:
• L(wx) = L(w) + 1, If x  , and w  *.
String Example
• Definition of the reversal of a string (the reversal of string
w is written wR.):
• R = 
• If x  , and w  *, then (wx)R = x(w)R
• For example (abc)R = c(ab)R
= cb(a)R
= cba()R
= cba
= cba
Exercise Questions

Chapter # 4
Topic # 4.3
Q. 1, 2, 3, 4, 7, 27-a, 48, 51

You might also like