0% found this document useful (0 votes)
2 views12 pages

Recursion

The document discusses recursion, defining it as the process of defining an object in terms of smaller versions of itself, with examples of recursively defined functions and the factorial of positive integers. It also covers logarithmic identities and methods for solving recurrences, particularly through iteration. The document provides specific examples and exercises to illustrate these concepts.

Uploaded by

lothbrook313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Recursion

The document discusses recursion, defining it as the process of defining an object in terms of smaller versions of itself, with examples of recursively defined functions and the factorial of positive integers. It also covers logarithmic identities and methods for solving recurrences, particularly through iteration. The document provides specific examples and exercises to illustrate these concepts.

Uploaded by

lothbrook313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Discrete Structures

Lecture#13
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.
RECURSION:

• The process of defining an object in terms of smaller versions of itself


is called recursion.
• A recursive definition has two parts:
• BASE:
• An initial simple definition which cannot be expressed in terms of smaller
versions of itself.
• 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
• There are certain arguments, called base values, for which the function does
not refer to itself.
• 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
• So
• 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
• 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(3) + 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.
Logarithmic identities
y log b ( x) if and only if x b y
log b ( xy ) log b ( x)  log b ( y )
 x
log b   log b ( x)  log b ( y )
 y
log b ( x y )  y log b ( x)

log b  x  logy( x)
y b

x log(y )  y log(x )
log b (b) 1
log b (1) 0
8
Solving the Recurrence (By Iteration
Method)
• If n is assumed to be a power of 2 (2k = n) this will simplify
the recurrence to

1 if n 1 
T (n)  
2T (n / 2)  n otherwise 

9
Solving the Recurrence..
T (n) 2T (n / 2)  n
2(2T (n / 4)  n / 2)  n
4T (n / 4)  n  n
4(2T (n / 8)  n / 4)  n  n
8T (n / 8)  n  n  n
8(2T (n / 16)  n / 8)  n  n  n
16T (n / 16)  n  n  n  n
10
The Iteration Method
(cont’d)
• If n is power of 2 then let n = 2k or k=log n.

k k
T (n) 2 T (n /( 2 ))  (n  n  n  ...  n)
k times
k k
2 T (n /( 2 ))  kn
(log n ) (log n )
2 T (n /( 2 ))  (log n)n
(log n )
2 T (n / n)  (log n)n
nT (1)  n log n n  n log n
11
Example-2: Solving Recurrence
f(n) = f(n -1 ) + n
= f(n- 2 ) + n - 1 + n
= f(n-3 ) + n - 2 + n - 1 + n
= f(n- 4 ) + n - 3 + n - 2 + n - 1 + n

= f( 1 ) + 2 + 3 + 4 + … + n - 1 + n
n(n  1) 2
 (n )
2 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy