0% found this document useful (0 votes)
27 views18 pages

CEN 235 2. Recursion

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)
27 views18 pages

CEN 235 2. Recursion

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/ 18

CEN 235 Data Structures

2. Recursion

Assoc. Prof. Dr. Volkan TUNALI


Faculty of Engineering and Natural Sciences
Department of Software Engineering
Outline

▪ Recursion
▪ Factorial
▪ Fibonacci Series
▪ Warning on Recursion
Recursion – Definition

▪ A recursive program is one that calls itself.


▪ An essential ingredient is a termination condition.
▪ Many interesting algorithms are quite simply expressed with
recursive programs.
Recursion – Concept

▪ A recursive function divides the problem into two conceptual


pieces:
▪ a piece that the function knows how to solve (base case),
▪ a piece that is very similar to, but a little simpler than the original problem,
hence still unknown how to solve by the function (call(s) of the function
to itself).

▪ Base case: the simplest version of the problem that is not further
reducible.
▪ The function actually knows how to solve this version of the problem.
Recursion – Concept Visualised
Factorial

Termination
▪ N! = N×(N-1)! for N≥1 with 0! = 1 Condition

▪ N! = N×(N-1)×(N-2)!
▪ N! = N×(N-1)×(N-2) … 0!
Factorial – C Code

▪ N! = N×(N-1)! for N≥1 with 0! = 1

int factorial(int N)
{
Termination
if (N==0) return 1; Condition
return N*factorial(N-1);
}

▪ Note: We don’t call this function with a negative N.


Factorial – Function Call Stack
Fibonacci Series

Termination
▪ FN = FN-1 + FN-2 for N≥2 with F0=F1=1 Condition

▪ This defines the sequence:


1 1 2 3 5 8 13 21 34 55 89 144 …
F0 F1 F2 F3 F4 F5 F6 …
Fibonacci Series – C Code

▪ FN = FN-1 + FN-2 for N≥2 with F0=F1=1

int fib(int N)
{
Termination
if (N<=1) return 1; Condition
return fib(N-1) + fib(N-2);
}
Fibonacci Series – Interesting Properties

▪ FN is the number of calls needed to compute FN, unless N=0 or


N=1.
▪ FN ≈ ΦN, where Φ=1.61803… so-called “Golden Ratio”
Fibonacci Series – Call Tree
Fibonacci Series – Number of Function Calls

Num of Func Calls Num of Func Calls


N FN
Including Calls to F0 and F1 Excluding Calls to F0 and F1
0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21
It turns out to be 2FN -1
Fibonacci Series – Number of Function Calls

Num of Func Calls Num of Func Calls


N FN
Including Calls to F0 and F1 Excluding Calls to F0 and F1
0 1 1 1
1 1 1 1
2 2 3 2
3 3 5 3
4 5 9 5
5 8 15 8
6 13 25 13
7 21 41 21
It turns out to be 2FN -1
Warning About Recursive Factorial and Fibonacci Series

▪ These two examples do not show the power of recursion.


▪ Why is that?
▪ Because these implementations are so inefficient to be useful.
▪ We have better iterative solutions, of course.
▪ However, we’ll see algorithms where recursion will demonstrate its
real power.
Summary

▪ A recursive program is one that calls itself.


▪ An essential ingredient is a termination condition.
▪ Many interesting algorithms are quite simply expressed with
recursive programs.
▪ Some problems are nice to define recursively but unwise to
implement recursively!
Coding Practice

1. fib(n) with recursion


2. fib(n) without recursion
1. without a lookup table
2. using a lookup table (Iterative and Recursive Versions)
1. There is a story (cos/sin)
3. fib(n) call count, visualization, and timing
Thank you for your attention!

Assoc. Prof. Dr. Volkan TUNALI


Faculty of Engineering and Natural Sciences
Department of Software Engineering

You might also like