RECURRENCES
Adapted from slides by Dr A. Sattar
Recursion
• Recursion is a method where the solution of a problem depends on
solutions to smaller instances of the same problem.
• Factorial.
• Fibonacci Series.
Adapted from slides by Dr A. Sattar
Factorial Example
• function factorial is:
input: integer n such that n >= 0
output: [n × (n-1) × (n-2) × … × 1]
1. if n is 0, return 1
2. otherwise, return [ n × factorial(n-1) ]
end factorial
For n=4;
b4 = 4 * b 3
= 4 * 3 * b2
= 4 * 3 * 2 * b1
= 4 * 3 * 2 * 1 * b0
=4*3*2*1*1
=4*3*2*1
=4*3*2
=4*6
= 24 Adapted from slides by Dr A. Sattar
Adapted from slides by Dr A. Sattar
Recurrences - Definition
Adapted from slides by Dr A. Sattar
Recurrences - Examples
Adapted from slides by Dr A. Sattar
Adapted from slides by Dr A. Sattar
Recurrence Relation
• In an Analysis of Algorithm, recurrence relations are used to analyze
the running time of a recursive function.
• The running time of a recursive function is denoted by T(n) where n is
the size of the input.
• In recurrence relation, the running time of a recursive function of
input size n is expressed in terms of the running time of the lower
value of n.
• For example
Analysis of Algorithms 8
The Process Of Translating A Code Into A
Recurrence Relation
• The first thing to look in the code is the base condition and note
down the running time of the base condition.
• Remember: every recursive function must have a base condition.
• For each recursive call, notice the size of the input passed as a
parameter.
• Calculate the running time of operations that are done after the
recursion calls.
• Finally, write the recurrence relation.
Analysis of Algorithms 9
Solving Recurrence Relation
• Solving the recurrence relation means finding the closed form
expression in terms of n.
• There are various techniques available to solve the recurrence
relations.
• Some techniques can be used for all kind of recurrence relations and
some are restricted to recurrence relations with a specific format.
Analysis of Algorithms 10
Solving Recurrence Relation
Analysis of Algorithms 11
Iteration Method
Adapted from slides by Dr A. Sattar
Iteration Method
Adapted from slides by Dr A. Sattar
Iteration Method
Adapted from slides by Dr A. Sattar
Substitution Method
Adapted from slides by Dr A. Sattar
Substitution Method
Adapted from slides by Dr A. Sattar
Substitution Method
Adapted from slides by Dr A. Sattar
Substitution Method
Adapted from slides by Dr A. Sattar