DSAP-Lecture 4 - Recursion
DSAP-Lecture 4 - Recursion
Lecture 4
Outline
● Introduction
● Recursion Examples
● Analyzing the time-complexity of recursions
● Types of Recursion
● Designing Recursive Algorithms
● Tower of Hanoi Problem
● Summary
Introduction
Two ways to define repetition:
➔ Fractal patterns
➔ Russian Matryoshka Dolls
Illustrative Examples of recursion
● Factorial function
● English Ruler
● Binary Search
● File System
The Factorial Function
The number of ways in which n distinct items
can be arranged into a sequence = n!. In other
Definition: words, permutation of n items = n!.
Recursive Definition:
J = 1, Activation Record 2
draw_interval(2)
center_length = 1 > 0
draw_line(3, ‘1’)
draw_interval(0)
draw_line(1)
J=2 Center_length = 1>0
draw_interval(0)
draw_interval(1) draw_interval(0)
draw_line(3, ‘2’) draw_line(1)
Activation Record 4
draw_interval(0)
Activation Record 1
Activation Record 5
Binary Search
● In BS, the sequence is partitioned into two ➔ If target > data[mid], recur the search
in the second half of the sequence.
sub-sequences and then the search is
performed only in one of the ➔ Repeat these steps until the interval
sub-sequence. [low, high] is empty or low > high.
Search ‘22’ in the array
File Systems
● os.path.getsize(path):
○ Return the immediate disk usage (measured in
bytes) for the file or directory that is identified by
the string path
● os.path.isdir(path):
○ Return True if entry designated by string path is
a directory; False otherwise
● os.listdir(path):
○ Return a list of strings that are the names of all
entries within a directory designated by string
path
● os.path.join(path, filename):
○ Compose the path string and filename string
using an appropriate operating system separator
between the two (‘/’ in Linux)
~ 56.8 MB
Analyzing Recursive Algorithms
● Computing Factorials
○ Each individual activation of factorial() executes a constant number of
operations.
○ To compute factorial(n), there are n+1 activations. The main function that
calls the factorial() function has its own activation record.
○ Time complexity ~ O(n)
Analyzing the English Ruler Example
Proof by Induction:
Base case: For c = 0, (no lines are drawn)
Inductive step: Each call to draw_interval(c) for c>0 spawns two calls to
draw_interval(c-1) and a single call to draw_line().
For any general c > 0, the number of lines printed:
Analyzing binary search algorithm
Proposition: The binary search algorithm runs in O(log n) time for a sorted sequence with
n elements.
Proof: The number of candidates to be search with each recursive call = high - low +1
The number of candidates to be searched is reduced by at least one half with each
recursive call. In general, after the jth call in a binary search, the number of candidates
remaining is at most
The maximum number of recursive calls to be performed is the smallest integer r such that
unique(S, 0, 3)
0 1 2 3 4
S[0] != s[4]
0 1 2 3 4
unique(S, 1, 4)
Another bad use of Recursion: Bad_fibonacci
Example:
● Multiple recursion - one recursive call may start three or more others.
Linear Recursion
Examples:
➔ Factorial
➔ Good_fibonacci
➔ Binary search
Linear recursion terminology reflects the structure of the recursion trace, not
the asymptotic analysis of the running time.
Summing the elements of a sequence
n=0 linear_sum(S,0) 0 0
Run time
Reversing a Sequence with Recursion
● Runtime
Recursive algorithms for computing Powers
● Runtime
Let
● Advantages of Recursion
○ provides a succinct way of achieve repetition by avoiding nested loops.
○ Makes the code readable and efficient.
● A recursion is a tail recursion if any recursive call that is made from one context is the
very last operation in that context, with the return value of the recursive call immediately
returned by the enclosing recursion.
● Examples:
binary_search(data, target)
reverse(S, start, stop)
factorial(n)
linear_sum(S,n)
● Any tail recursion can be
re-implemented non recursively by
enclosing the body in a loop for
repetition, and by replacing a
recursive call with new parameters
by a reassignment of the existing
parameters to those values.
Few More Examples of Recursion
Tower of Hanoi
Move 4 Move 5
Move 6 Move 7
Move 8 Move 11
Move 9 Move 10
Move 12 Move 15
Move 13 Move 14
Check for a sorted array