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

DS Lecture 10 - Recursion (Fibonocci, Factorial)

The document discusses recursion, which is a technique for solving problems by solving smaller instances of the same problem. It provides examples of recursive functions like calculating the Fibonacci sequence and factorials. It also compares recursion to iteration and describes properties of recursive methods.

Uploaded by

Waqas Khokhar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

DS Lecture 10 - Recursion (Fibonocci, Factorial)

The document discusses recursion, which is a technique for solving problems by solving smaller instances of the same problem. It provides examples of recursive functions like calculating the Fibonacci sequence and factorials. It also compares recursion to iteration and describes properties of recursive methods.

Uploaded by

Waqas Khokhar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data Structures & Algorithms

Lecture # 10
Recursion
(Fibonocci Sequence, Factorial)
Recursion
 Sometimes, the best way to solve a problem is by solving a
smaller version of the exact same problem first.
 Recursion is a technique that solves a problem by solving a smaller problem of the same type.

Recursion Vs. Iteration


 Iteration can be used in place of recursion.
 An iterative algorithm uses a looping construct,
 A recursive algorithm uses a branching structure
 Recursive solutions are often less efficient, in terms of both time and space, than iterative
solutions
 Recursion can simplify the solution of a problem, often resulting in shorter, more easily
understood source code.
 If a loop is used, the method cycles around the loop n times, adding n to the total the first time,
n-1 the second time and so on, down to 1, quitting the loop when n becomes 0.
 If recursion is used, then a base case is used that determines when the recursion ends.
How to write a recursive function?
 Determine the size factor (Number of cycles)
 Determine the base case(s)
(the one for which you know the answer)
 Determine the general case(s)
(the one where the problem is expressed as a smaller version of itself)
 Verify the algorithm

Characteristics of Recursive Methods


 The recursive method calls itself to solve a smaller problem.
 The base case is the smallest problem that the routine solves and the value is returned to the
calling method. (Terminal condition)
 Calling a method involves certain overhead in transferring the control to the beginning of the
method and in storing the information of the return point.
 Memory is used to store all the intermediate arguments and return values on the internal stack.
 The most important advantage is that it simplifies the problem conceptually.
Types of Recursion
 Direct recursion
In direct recursion, a function calls itself. For example, if f calls f, that is direct recursion.
 Indirect recursion
Indirect recursion occurs when a function is called not by itself but by another function that
it called (either directly or indirectly).  For example, if f calls g which calls f, then that is
indirect recursion of f. Indirect recursion is also called mutual recursion.
 Single recursion
Recursion that only contains a single self-reference is known as single recursion. Standard
examples of single recursion include list traversal, such as in a linear search, or computing
the factorial function.
 Multiple recursion
Recursion that contains multiple self-references is known as multiple recursion. Standard
examples of multiple recursion include tree traversal, such as in a depth-first search, or
computing the Fibonacci sequence.
Fibonacci Sequence
 The Fibonacci Sequence is the series of numbers.
 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
 The next number is found by adding up the two numbers before it.
 Psuedocode (Recursive)
input: integer index n such that n >= 0
output: [(n-1) + (n-2)]
fib(var n)
1. IF (n == 1) OR (n==2) THEN
2. return n
ELSE
3. return fib(n-1) + fib(n-2)
END IF
Fibonacci Sequence
 Pseudo code (iterative)
input: integer n such that n >= 0
output: [n + (n-1) + (n-2) + … + 1]

var fib(var n)
var f[n+1]
f[1] = f[2] = 1
for (var i = 3; i <= n; i++)
f[i] = f[i-1] + f[i-2]
return f[n]
Factorial; A classical Example of recursion
 Pseudo-code (recursive)

INPUT: integer n such that n >= 0


OUTPUT: [n × (n-1) × (n-2) × … × 1]

FUNCTION factorial is:


1. IF n is 0,
2. RETURN 1
ELSE
3. RETURN [ n × factorial(n-1) ]

END factorial
Factorial; A classical Example of recursion
Pseudo-code (iterative)
 FUNCTION factorial is:
 INPUT: integer n such that n >= 0
 OUTPUT: [n × (n-1) × (n-2) × … × 1]

 1. CREATE new variable called running_total with a value of 1


 2. BEGIN loop
for n = 4
 3. IF n is 0, THEN
f4 = 4 * f3
 4. exit loop
= 4 * (3 * f2)
 5. SET running_total to (running_total × n) = 4 * (3 * (2 * f1))
 6. DECREMENT n = 4 * (3 * (2 * (1 * f0)))
= 4 * (3 * (2 * (1 * 1)))
 7. REPEAT loop = 4 * (3 * (2 * 1))
= 4 * (3 * 2)
 8. RETURN running_total
=4*6
 END factorial = 24
Good Luck !
☺←◙+☻

You might also like