Mathematical Analysis of Recursive Algorithms
Mathematical Analysis of Recursive Algorithms
Recursive Algorithms
Design and Analysis of Algorithms
(CS3024)
28/02/2006
CS3024-FAZ
Example 1
Algorithm F(n)
// compute n! recursively
// input: a nonnegative integer n
// output: the value of n!
if n = 0 return 1
else return F(n-1)*n
CS3024-FAZ
Analyzing Efficiency of
Recursive Algorithms (1)
1. Decide on a parameter(s) indicating an inputs
size
2. Identify the algorithms basic operation
(typically, it is located in its inner most loop)
3. Check whether the number of times the basic
operation is executed depends only on the size
of an input. If it also depend on some
additional property, the worst-case, averagecase, and, if necessary, the best-case
efficiencies have to be investigated separately
CS3024-FAZ
Analyzing Efficiency of
Recursive Algorithms (2)
4. Set up a recurrence relation, with an
appropriate initial condition, for the
number of times the algorithms basic
operation is executed
5. Solve the recurrence or at the least
ascertain(memastikan) the order of
growth of its solution
CS3024-FAZ
Goal
Initial
CS3024-FAZ
Tower of Hanoi:
Recursive Solution (1)
CS3024-FAZ
CS3024-FAZ
10
CS3024-FAZ
11
12
13
Example 3
Algorithm BinRec(n)
//input: a positive decimal integer n
//output: the number of binary digits in ns binary
// representation
if n = 1 return 1
else return BinRec(n/2) + 1
CS3024-FAZ
14
15
CS3024-FAZ
16
= A(2k-i) + i
= A(2k-k) + k = A(1) + k = k
CS3024-FAZ
17
CS3024-FAZ
18
Exercises
Solve the following recurrence relations:
x(n)=x(n-1)+5 for n>1, x(1)=0
x(n)=3x(n-1)+5 for n>1, x(1)=4
x(n)=x(n-1)+n for n>0, x(0)=0
x(n)=x(n/2)+n for n>1, x(1)=1 (solve for n=2k)
x(n)=x(n/3)+1 for n>1, x(1)=1 (solve for n=3k)
CS3024-FAZ
19