1. Prof. Amr Goneid, AUC 1
Analysis & Design of
Algorithms
(CSCE 321)
Prof. Amr Goneid
Department of Computer Science, AUC
Part 5. Recursive Algorithms
3. Prof. Amr Goneid, AUC 3
Recursive Algorithms
Modeling Recursive Algorithms
Examples of Recurrences
Exclude & Conquer
General Solution of 1st Order Linear
Recurrences
2nd Order Linear Recurrence
4. Prof. Amr Goneid, AUC 4
1. Modeling Recursive Algorithms
Recursive Algorithm
Model as a
Recurrence Relation
T(n) in terms of T(n-1)
Solve to obtain T(n) as
a function of n
5. Prof. Amr Goneid, AUC 5
Recurrence Relations
A recurrence relation is an equation describing a
function in terms of its value for smaller instances.
Many algorithms, particularly exclude & conquer
and divide and conquer algorithms, have time
complexities which are naturally modeled by
recurrence relations.
Many counting problems can be modeled as
recurrences. Solving the recurrence relation
solves the problem.
6. Prof. Amr Goneid, AUC 6
Recurrence Relations
Many natural functions are expressed as
recurrences:
)
(Factorial
1
with
al)
(Exponenti
2
1
with
2
l)
(Polynomia
1
with
1
1
1
1
1
1
1
1
!
n
a
a
na
a
a
a
a
a
n
a
a
a
a
n
n
n
n
n
n
n
n
n
n
7. Prof. Amr Goneid, AUC 7
Recurrence Relations
To derive the recurrence relation:
Identify the problem size (n).
Find the base case size n0
(usually n0 = 0 ,1 or 2) and find T(n0)
The general case consists of “a” recursive
calls to do smaller sub-problems (usually of
size n-1) plus a cost “b” for all the work
needed other than in the recursive calls.
8. Prof. Amr Goneid, AUC 8
Recurrence Relations
Hence, the recurrence relation will have the
form:
0
0
0
)
1
(
)
(
)
(
n
n
for
b
n
aT
n
n
for
n
T
n
T
base case
cost of base case
no. of
recursive
calls
cost of a
recursive
call
cost of
other
operations
9. Prof. Amr Goneid, AUC 9
T(0) = 0
A Recursive Factorial Function:
factorial (n)
{
if (n == 0) return 1;
else return ( factorial (n-1) * n);
}
Find T(n) = number of integer multiplications
2. Examples of Recurrences
T(n)
T(n-1) 1
10. Prof. Amr Goneid, AUC 10
Examples
The base case with n = 0 does zero
multiplications. Hence T(0) = 0.
The general case will cost T(n-1) plus one
multiplication.
Recurrence Relation:
0
T(0)
0 with
n
for
1
1)
-
T(n
T(n)
11. Prof. Amr Goneid, AUC 11
Examples
Recurrence relations of this kind can be solved by
successive substitution:
Substitute several times (m) to show a pattern.
Choose (m) so that each T( ) in which (m) appears
reduces to T(n0)
Substitute for T(n0)
Solve the resulting equation
12. Prof. Amr Goneid, AUC 12
Examples
For example, for the recurrence relation of the
recursive factorial:
)
n
(
)
n
(
T
Hence
n
n
)
(
T
)
n
(
T
then
n
m
With
m
)
m
n
(
T
....
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
(
T
with
)
n
(
T
)
n
(
T
0
3
3
2
2
1
1
2
0
0
1
1
13. Prof. Amr Goneid, AUC 13
A Recursive Function to return xn:
power (x, n)
{
if (n == 0 ) return 1.0;
else return ( power(x,n-1)*x );
}
Find T(n) = number of arithmetic operations
T(n) = T(n-1) + 1 for n > 0 with T(0) = 0
Hence T(n) = n = (n)
Examples
15. Prof. Amr Goneid, AUC 15
// Assume the data are in locations s….e of array a[0..n-1]
// A problem of size (n) arises when the function is invoked as
// selectsort(a,0,n-1), i.e. with s = 0 and e = n-1
Selectsort (a[ ], s, e)
{
if (s < e) {
m = minimum (a , s , e) ;
swap (a[s] , a[m]);
Selectsort (a,s+1,e); }
}
Example: Recursive Selection Sort
T(s,e)
T(s+1,e)
comp
s
e )
(
swap
1
16. Prof. Amr Goneid, AUC 16
Selection Sort (continued)
Recurrence Relation: ( problem of size n when s = 0 , e = n-1):
)
1
n
(
)
n
(
T
that
So
0
)
1
(
T
with
1
n
for
1
)
1
n
(
T
)
n
(
T
Also
2
)
1
n
(
n
i
)
i
n
(
)
1
(
T
)
n
(
T
,
1
n
m
Set
)
i
n
(
)
m
n
(
T
)
1
n
(
)
2
n
(
)
2
n
(
T
)
n
(
T
Hence
0
)
1
(
T
with
1
n
for
)
1
n
(
)
1
n
(
T
)
n
(
T
1
n
e
,
0
s
Let
)
s
e
(
)
e
,
1
s
(
T
)
e
,
s
(
T
swap
swap
swap
1
n
1
i
1
n
1
i
comp
m
1
i
comp
comp
comp
comp
17. Prof. Amr Goneid, AUC 17
4. General Solution of 1st Order
Linear Recurrences
Many recursive algorithms are modeled to the
following general 1st order linear recurrence
relation:
e.g. the factorial algorithm gives an = 1 and
bn = 1, T(0) given.
Select sort gives an = 1 and bn = (n-1),
T(1) given.
T(1)
or
T(0)
given
,
b
1)
-
T(n
a
T(n) n
n
18. Prof. Amr Goneid, AUC 18
General Solution of 1st Order
Linear Recurrences
Such recurrence can be solved by successive
substitution. e.g. for T(0) given:
n
n
i
j
j
n
i
i
n
i
i
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
b
a
b
a
)
(
T
...
b
b
a
b
a
a
)
n
(
T
a
a
a
........
b
b
a
)
n
(
T
a
a
b
}
b
)
n
(
T
a
{
a
b
)
n
(
T
a
)
n
(
T
1
1
1
1
1
2
1
2
1
1
1
1
1
0
3
2
2
1
19. Prof. Amr Goneid, AUC 19
General Solution of 1st Order
Linear Recurrences
Successive substitution gives for the cases of
T(0) given and T(1) given:
n
n
i
j
j
n
i
i
n
i
i
n
n
i
j
j
n
i
i
n
i
i
b
a
b
a
)
(
T
)
n
(
T
b
a
b
a
)
(
T
)
n
(
T
1
1
2
2
1
1
1
1
1
0
20. Prof. Amr Goneid, AUC 20
Examples
For the recursive factorial function, an=1 so that ai=1 and
similarly bi=1 with T(0)=0. Substitution in the general
solution gives:
hence T(n) = n as before.
For the selection sort algorithm, ai=1,
bi= (i-1) with T(1) = 0 so that
n
)
n
(
T
n
i
n
i
n
i
j
1
1
1 1
1
1
1
1
n
comp
i
comp
T ( n ) ( i ) ... ( n )
T ( n ) n( n ) /
2
1 1 2 3 4 1
1 2
21. Prof. Amr Goneid, AUC 21
Examples
T(n) = 2 T(n-1) + 1 with T(0) = 0
T(n) = n T(n-1) with T(0) = 1
)
2
(
1
2
2
1
2
1
)
(
1
0
1
1 1
n
n
n
k
k
n
i
n
i
j
O
n
T
)
!
(
!
)
0
(
)
(
1
1
n
O
n
i
a
T
n
T
n
i
n
i
i
22. Prof. Amr Goneid, AUC 22
Exercise(1): Exclude & Conquer
with a Linear Process
int FOO (int a[ ], int n)
{
if (n > 0)
{
int m = 5 * Process (a, n);
return m * FOO(a,n-1);
}
}
Show that the number of integer multiplications if Process
makes 2n such operations is:
)
n
(
O
n
n
)
n
(
T 2
2
3
23. Prof. Amr Goneid, AUC 23
Solution
)
n
(
O
n
n
}
n
/
)
n
(
n
{
)
i
(
b
b
b
)
n
(
T
)
i
(
b
,
a
)
(
T
with
)
n
(
)
n
(
T
)
n
(
T
n
i
n
i
i
n
n
i
i
i
i
2
2
1
1
1
1
3
2
1
2
1
2
1
2
1
0
0
1
2
1
24. Prof. Amr Goneid, AUC 24
Exercise(2): History Problem
double Eval ( int n )
{ int k ; double sum = 0.0;
if (n == 0) return 1.0 ;
else
{ for (k = 0; k < n; k++) sum += Eval (k);
return 2.0 * sum / n + n ; }
}
Show that the number of double arithmetic
operations performed as a function of (n) is:
)
(
O
)
n
(
T n
n
2
1
2
25. Prof. Amr Goneid, AUC 25
Solution
1
1
2
1
1
1
2
1
3
0
0
3
1
2
0
1
0
1
0
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
)
k
(
T
)
n
(
T
)
n
(
)
k
(
T
)
n
(
T
)
(
T
with
}
)
k
(
T
{
)
n
(
T
n
k
n
k
n
k
26. Prof. Amr Goneid, AUC 26
Solution
)
(
O
b
a
b
)
n
(
T
)
(
T
,
b
,
a
n
n
n
i
i
n
i
i
n
n
n
i
j
j
n
i
i
i
i
2
1
2
2
1
2
0
0
1
2
1
0
1
1
1
1
1
27. Prof. Amr Goneid, AUC 27
Exercise (3): Hanoi Towers Game
Find the number of moves required for a Tower of
Hanoi with n discs. An animation is available at:
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal
/ToHdb.html
28. Prof. Amr Goneid, AUC 28
5. 2nd Order Linear Recurrence
Example: Fibonacci Sequence
F(n) = F(n-1) + F(n-2) , F(0) = 0 F(1) = 1
Let to obtain the characteristic equation
Solutions:
n
n n
n n
F(n) r r r
,
F( n )
With F( ) and F( ) then and
F( n ) , Golden Ratio .
2
1 0
1 5 1 5
1
2 2
1
0 0 1 1
5
1 618034
5