CPT212-04-Recursive
CPT212-04-Recursive
CPT212-04-Recursive
Recursion
Algorithms
TAN TIEN PING
Introduction
Recursion is a technique by which a method makes one or more calls to itself during execution
When one invocation of the method makes a recursive call, that invocation is suspended until
the recursive call completes.
Complexity analysis can be easier done on algorithm implemented using loops, for algorithms
implemented using recursive, it is more challenging.
Note: All implementation of loops can be implemented using recursive and vice versa.
Many algorithms that uses divide and conquer techniques uses recursive approach. For divide
and conquer algorithms, there are 2 main steps, division and combination. Analysis of
algorithms can be done on the division step and combination step.
We will discuss few algorithms that uses recursive in solving a problem.
Loop vs Recursion
Loop Recursion
if (i < N){
return recursive(i+1, j+5, N);
Total = 10 + N*5 } else{
return j;
}
}
Loop vs Recursion
Recursion Example: What is the answer when N=3?
public int recursive(int i, int j, int N){ call recursive(1, 15, 3); +5 op
return 1;
Factorial Function
When the number of input, x, increases from 1, 2, 3 … n, the factorial(n)
number of multiplications performed, f(x) increases 1, 2,
3, ..., n respectively.
Complexity: O(n)
n # Activation # Multiplication n
1 2 1
2 3 2
3 4 3
…
n n+1 n
Binary Search
The binary_search() will check whether the key
value is larger or lower than mid. If larger than mid,
then call binary_search() and check upper half, else
if lower, check lower half, else we got the value we
searching for.
Each binary_search() recursively call maximum a
single binary_search()
Binary Search: Worst Case
Complexity
When input size, x=n, there Input size, x
When input size, x=n, there are
are how many comparisons?
or in another word, what is 2k-1 c*n comparisons how many comparisons? or in
another word, what is f(x=n) in
f(x=n) in worst case? worst case?
..
2k-1 = n 15 c*4 comparisons
k lg 2 = lg (n+1)
k = lg (n+1)
Height = log n 7 c*3 comparisons
3 c*2 comparisons
Binary search = O(log n)
Look at the binary_search(), there are 1 c*1 comparison
max 4 if…else statements, so c=3
Binary Search is log n
You have an array. If you shrink the size of your Every time binary_search() is called,
array to half every time. How many times you can maximum 3 if…else statements will be
shrink it? if… executed.
5 x power(5,1) 52 2 multiplications
5 x power(5,0) 51 1 multiplication
Computing Power (Efficient)
How to do better?
Idea: instead of computing 24 = 2 * 2 * 2 * 2 (note: 3 multiplications), we will compute it as (2 2)2
(note: 2 multiplications).
Computing Power (Efficient)
k)
k
k/2);
k
Computing Power (Efficient)
Note: f(k) = xk
When the number of input, x increases from x2, x4, x8 …,xn the number of multiplications
performed, f(x) increases 1, 2, 3, 4...,log n respectively.
Complexity: O(log n)
x2 2 multiplications
x0
Drawing English Ruler
A recursive method can be used to draw
English ruler as shown.
The algorithm uses binary recursion, where
each recursion method made 2 recursion calls.
For this problem, assume the number of
inches is a constant.
Drawing English Ruler
drawLine() majorLength=2
c
drawInterval() c
c
n
drawLine() c c c
c
drawRuler() drawInterval()
If you analyze the number of time drawInterval()
is called. Except on the drawRuler() which is only
called once*x (where x=nInches), other time,
drawInterval() is call 2 times in the
drawInterval().
When majorLength increases from 1, 2, 3 …, n
the number of drawInterval() called, f(n)
increases 1c, 3c, 7c, ...,c(2n-1) respectively.
Assuming c is the constant time for executing
drawLine().
So, f(n) = 2n-1, Complexity O(2n)
a line correspond
to a call to
x=1, f(1)=c*1 drawInterval()