Sei sulla pagina 1di 12

Introduction to analysis of algorithms

Analysis of Recursive Algorithms




Dr. Abeer Hamdy
Decide on a parameter indicating an inputs size.

Identify the algorithms basic operation.

Check whether the number of times the basic op. is
executed may vary on different inputs of the same
size. (If it may, the worst, average, and best cases
must be investigated separately.)

Set up a recurrence relation with an appropriate
initial condition expressing the number of times the
basic op. is executed.

Solve the recurrence equation.
Plan of Analysis of Recursive Algorithms










Input Size: n

Basic operation: multiplication

Recurrence relation: T(n)=T(n-1)+1 for n>0, T(0)=0



Example 1: Recursive evaluation of n!
Solving recurrence equations
It means finding a closed form solution to the
recurrence. i.e. Explicit formula expresses
T(n) in terms of n only.

Methods of solving recurrence:
Iterative substitution.
The master method.
Recursion trees.
Guess-and-test.
Iterative Substitution
In the iterative substitution, or plug-and-chug,
technique, we iteratively apply the recurrence
equation to itself and see if we can find a pattern:

Recurrence relation: T(n) = T(n-1)+1 for n>0 , M(0)=0

T(n) = T(n-2) +2 as ,T(n-1) = T(n-2)+1

T(n) = T(n-3) +3 as, T(n-2)= T(n-3) +1
..
T(n) = T(n-i) +i
The base case occurs when i=n
T(n) = T(n-n) +n = 0+n
= n


Iterative Substitution
Given the following recurrence relation:

> +
<
=
2 if ) 2 / ( 2
2 if
) (
n bn n T
n b
n T
Find a closed form for T(n) using iterative
substitution?
Iterative Substitution
Base case occurs when n= 2
i
, i=logn
So, T(n) = nT(1) + bn logn = bn + bnlogn = O(nlogn)



ibn n T
bn n T
bn n T
bn n T
bn n b n T
bn n T n T
i i
+ =
=
+ =
+ =
+ =
+ + =
+ =
) 2 / ( 2
...
4 ) 2 / ( 2
3 ) 2 / ( 2
2 ) 2 / ( 2
)) 2 / ( )) 2 / ( 2 ( 2
) 2 / ( 2 ) (
4 4
3 3
2 2
2
Master Theorem
Many divide-and-conquer recurrence equations have the form:





The Master Theorem:

> +
<
=
d n n f b n aT
d n c
n T
if ) ( ) / (
if
) (
. 1 some for ) ( ) / ( provided
)), ( ( is ) ( then ), ( is ) ( if 3.
) log ( is ) ( then ), ( is ) ( if 2.
) ( is ) ( then ), ( is ) ( if 1.
log
log log
log log
< s
O O
O O
O
+

o o
c
c
n f b n af
n f n T n n f
n n n T n n f
n n T n O n f
a
a a
a a
b
b b
b b
Master Theorem Examples
Example1 :

Solution: log
b
a=2, so case 1 says T(n) is O(n
2
).

Example2:


Solution: : log
b
a=1, so case 2 says T(n) is O(n log
2
n).

Example3:

Solution: log
b
a=0, so case 3 says T(n) is O(n log

n).



n n T n T + = ) 2 / ( 4 ) (
n n n T n T log ) 2 / ( 2 ) ( + =
n n n T n T log ) 3 / ( ) ( + =
Master Theorem Examples
Example4 :

Solution: log
b
a=0, so case 2 says T(n) is O(log n).

Example5:


Solution: log
b
a=2, so case 3 says T(n) is O(n
3
).

Example6:

Solution: log
b
a=3, so case 1 says T(n) is O(n
3
).



1 ) 2 / ( ) ( + = n T n T
3
) 3 / ( 9 ) ( n n T n T + =
2
) 2 / ( 8 ) ( n n T n T + =
Guess-and-Test Method
In the guess-and-test method, we guess a closed form solution
and then try to prove it is true by induction.

Example: Consider the following recurrence:



Guess1: T(n) < cn log n.






Wrong guess: we cannot make this last line be less than cn log n

> +
<
=
2 if log ) 2 / ( 2
2 if
) (
n n bn n T
n b
n T
n bn cn n cn
n bn n cn
n bn n n c
n bn n T n T
log log
log ) 2 log (log
log )) 2 / log( ) 2 / ( ( 2
log ) 2 / ( 2 ) (
+ =
+ =
+ =
+ =
Guess-and-Test Method
Guess 2: T(n) < cn log
2
n.







if c>b

So, T(n) is O(n log
2
n).

In general, to use this method, you need to have a good guess
and you need to be good at induction proofs.


n cn
n bn cn n cn n cn
n bn n cn
n bn n n c
n bn n T n T
2
2
2
2
log
log log 2 log
log ) 2 log (log
log )) 2 / ( log ) 2 / ( ( 2
log ) 2 / ( 2 ) (
s
+ + =
+ =
+ =
+ =

Potrebbero piacerti anche