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

Disc Week 10

The document discusses recursion through examples and definitions. Recursion is defined as defining an object in terms of smaller versions of itself. It has two parts: a base case which cannot be expressed recursively, and a recursive case which can be expressed in terms of smaller versions. Examples are given of recursively defined sequences like factorials and Fibonacci numbers. Recursion provides solutions to problems in computer science and math through functions that refer to themselves.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Disc Week 10

The document discusses recursion through examples and definitions. Recursion is defined as defining an object in terms of smaller versions of itself. It has two parts: a base case which cannot be expressed recursively, and a recursive case which can be expressed in terms of smaller versions. Examples are given of recursively defined sequences like factorials and Fibonacci numbers. Recursion provides solutions to problems in computer science and math through functions that refer to themselves.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Discrete Structures

Recursion
Recursion
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.

REMARK: Recursive definitions can be used in a “generative” manner.


RECURSION

The process of defining an object in terms of smaller


versions of itself is called recursion. A recursive
definition has two parts:
1.BASE:
•An initial simple definition which cannot be expressed
in terms of smaller versions of itself.
2. RECURSION:
•The part of definition which can be expressed in terms
of smaller versions of itself.
RECURSIVELY DEFINED FUNCTIONS

A function is said to be recursively defined if the function refers


to itself such that
1. There are certain arguments, called base values, for which the
function does not refer to itself.
2. Each time the function does refer to itself, the argument of the
function must be closer to a base value.
EXAMPLE

Suppose that f is defined recursively by


•f(0) = 3
•f(n + 1) = 2 f (n) + 3
•Find f(1), f(2), f(3) and f(4)
SOLUTION:
•From the recursive definition it follows that
•f(1) = 2 f(0) + 3 = 2(3) + 3 = 6 + 3 = 9
•In evaluating of f(1) we use the formula given in the example and we note
that it involves f(0) and we are also given the value of that which we use to
find out the functional value at 1. Similarly we will use the preceding value
in evaluating the next values of the functions as we did below.
•f(2) = 2 f(1) + 3 = 2(9) + 3 = 18 + 3 = 21
•f(3) = 2 f(2) + 3 = 2(21) + 3 = 42 + 3 = 45
•f(4) = 2 f(3) + 3 = 2(45) + 3 = 90 + 3 = 93
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
• 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
• 2. F(n) = n F(n-1)
THE FIBONACCI SEQUENCE

• The Fibonacci sequence is defined as follows.


• F0 = 1, F1 = 1
• Fk = Fk-1 + Fk-2 for all integers k ≥ 2
• F2 = F1 + F0 = 1 + 1 = 2
• 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 a , a , a , . . . , is a formula that


0 1 2

relates each term a to certain of its predecessors a , a , . . . , a ,


k k-1 k-2 k-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 a , a , a , . . . , a .
0 1 2 i-1
Example

• 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
Example
• Find the first five terms of the following recursively defined
sequence.
• t = – 1, t = 1
0 1

• t = t + 2 · t , for all integers k ≥ 2


k k-1 k-2

SOLUTION:
• t = – 1, (given in base step)
0

• t = 1 (given in base step)


1

• t = t + 2 · t = 1 + 2 · (–1) = 1 – 2 = –1
2 1 0

• t = t + 2 · t = –1 + 2 · 1 = –1 + 2 = 1
3 2 1

• t = t3 + 2 · t = 1 + 2 · (–1) = 1 – 2 = –1
4 2
EXERCISE

• Define a sequence b0, b1, b2, . . . by the formula


• bn = 5 n , for all integers n ≥ 0.
• Show that this sequence satisfies the recurrence relation
• bk = 5bk – 1, for all integers k ≥ 1.
SOLUTION

• The sequence is given by the formula


• bn = 5 n
• Substituting k for n we get
• bk = 5k . . . . . (1)
• Substituting k – 1 for n we get
• bk-1 = 5 k-1 . . . . . (2)
• Multiplying both sides of (2) by 5 we obtain
• 5 · bk-1 = 5 · 5 k – 1
• = 5k = bk using (1)
• Hence bk = 5bk-1 as required
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

You might also like