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

Understanding Recursion in Algorithms

Uploaded by

minopath
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 views41 pages

Understanding Recursion in Algorithms

Uploaded by

minopath
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

Advanced Algorithms

Chapter 3: Recursion

By Noureddine Amraoui, Ph.D.


Algorithmic Strategies: Definition

A taxonomy of high-level approaches/patterns for solving


computational problems:

Recursion. Dynamic Programming.


Divide and Conquer. Branch and Bound (not included).
Greedy. Backtracking (not included).
Dynamic Programming.

2
Algorithmic Strategies: Properties

Choice of strategy is problem-dependent, no rule/formula to


choose which strategy is more suitable (it comes with practice).

Multiple paradigms might be applicable to a specific problem.

Temporal and spatial requirements can be analyzed precisely.

3
Recursion: Content

Definition Tracing Tree and Stack Usage


Properties Tail vs Head Recursion
Why Recursion? Decreasing vs Dividing Recursion
General Pattern Time Complexity.
Iterative vs Recursive

4
Recursion: Definition

A strategy for solving a problem:


 By having a function calling itself.
 Where the solution depends on the solution to the smaller
instance of same problem.

Such problem can generally be solved by iteration as well.

5
Recursion: Properties

Perform the same operation multiple times with different


inputs.

In each subsequent recursion, we try smaller inputs to make


the problem smaller.

Termination condition is needed to stop the recursion,


otherwise, infinite loop will occur.

6
Recursion: Why?

Basis of many other strategies: divide and conquer, greedy,


backtracking, etc.

Prominent in non-linear data structures such as graphs and


trees (e.g., Tree DFS traversal).

7
Recursion: General Pattern

8
Recursion: Example

Factorial:

The function needs itself to give the result.


To calculate n!, we have to know how to calculate (n-1)!
To calculate (n-1)!, we have to know how to calculate (n-2)!
Up to 1! or 0! which equal to 1.
1! or 0! allow the recursion to stop after a series of self-calls.

9
Recursion: Iterative vs Recursive

Example: Factorial

10
Recursion: Tracing Tree and Stack Usage

Tracing Tree:
Initial Call

Additional Winding
Call #2
operation

Additional
Call #3
Unwinding operation

Additional
Call #n
operation
Termination 11
Recursion: Tracing Tree and Stack Usage

Tracing Tree: Two Phases:

1. Winding:
Starts when the recursive function is called for the first time.
Each recursive call makes another one.
No return statement is executed.
Ends when the termination condition becomes true.

12
Recursion: Tracing Tree and Stack Usage

Tracing Tree: Two Phases:

2. Unwinding:
Starts upon arriving at the termination condition.
Start returning in reverse order till the first instance of function
returns.
Continues until the initial call is terminated.

13
Recursion: Tracing Tree and Stack Usage

Example: Factorial (version 1)


facto1 (4) Return 24

4 x facto1 (3) Return 6

3 x facto1 (2) Return 2

2 x facto1 (1) Return 1


facto1 n=1
1 x facto1 (0) Return 1 facto1 n=2
facto1 n=3
1 facto1 n=4 14
Recursion: Tracing Tree and Stack Usage

Example: Factorial (version 2)


facto2 (4) Return 24

4 x facto2 (3) Return 6

3 x facto2 (2) Return 2

2 x facto2 (1) Return 1


facto2 n=2
facto2 n=3
1 facto2 n=4 15
Recursion: Tracing Tree and Stack Usage

Example:

Factorial (version 1): n+1 function calls

Factorial (version 2): n function calls

16
Recursion: Tail vs Head

Tail Recursion: no processing is performed after the recursive


call.
Head Recursion: other processing is performed after the
recursive call.
Example:

17
Recursion: Decreasing vs Dividing

Decreasing Recursion:
The problem is reduced by a constant into a small-size subproblem(s).

18
Recursion: Decreasing vs Dividing

Dividing Recursion:
The problem is divided by a constant into small-size subproblem (s).

19
Recursion: Time Complexity

Recursive Algorithm

 Find the Recurrence Relation of Time Complexity

 Solve RR to get exact Time Complexity

20
Recursion: Time Complexity

We recursively solve the problem using the solution of smaller


problems, besides additional operations.
T(n) =
T(input size of 1st sub-problem)
+ T(input size of 2nd sub-problem)
+ ... +
+ T(input size of kth sub- sub-problem)
+ T(additional operations other than recursive calls)

Recurrence Relation
21
Time Complexity: Recurrence Relation

Decreasing Functions:
Example 1:

22
Time Complexity: Recurrence Relation

Decreasing Functions:
Example 2:

23
Time Complexity: Recurrence Relation

For both decreasing and dividing functions:


Can be solved by:

Substitution.

Master Theorem.

24
Time Complexity: Decreasing Recursion

Solved by Substitution:
Example 1:

25
Time Complexity: Decreasing Recursion

Solved by Substitution:
Example 1:

26
Time Complexity: Decreasing Recursion

Solved by Substitution:
Example 2:

27
Time Complexity: Decreasing Recursion

Solved by Substitution:
Example 2:

28
Time Complexity: Decreasing Recursion






29
Time Complexity: Decreasing Recursion

Solved by Master Theorem:


Cases:

 /

30
Time Complexity: Decreasing Recursion

Solved by Master Theorem:

31
Time Complexity: Decreasing Recursion

Solved by Master Theorem:

32
Time Complexity: Decreasing Recursion

Solved by Master Theorem:

33
Time Complexity: Dividing Recursion

Example 1:

34
Time Complexity: Dividing Recursion

Solved by Substitution:
Example 1:

….

35
Time Complexity: Dividing Recursion

Solved by Substitution:
Example 1:

36
Time Complexity: Dividing Recursion

Solved by Master Theorem:

Cases:





 37
Time Complexity: Dividing Recursion

Solved by Master Theorem:


Case 1 Examples:





38
Time Complexity: Dividing Recursion

Solved by Master Theorem:


Case 2 Examples:





 )

39
Time Complexity: Dividing Recursion

Solved by Master Theorem:


Case 3 Examples:




40
Any questions ?
You can contact me by:
Telegram: @noureddine_amraoui
Email: [Link]@[Link]

41

You might also like