

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Computer Science Dept Va Tech March 2007 ©2007 McQuain
Complexity
Data Structures & OO Development II
Recursive algorithms naturally lead to complexity functions that are themselves recursive:
unsigned int Factorial(unsigned int N) { if ( N <= 1) return 1; return N * Factorial(N – 1); }
The complexity function would naturally be expressed as: (^) ( ) 2, if 1 4 ( 1), if 1
T N N T N N
= ^ ≤ (^) + − >
Such a function is called a recurrence relation. While the equation certainly expresses the complexity function, a non-recursive (or closed-form ) version would be more useful.
Computer Science Dept Va Tech March 2007 ©2007 McQuain
Complexity
Data Structures & OO Development II
General techniques exist for solving recurrence relations, and closely resemble techniques for solving differential equations. For simple ones, like the one on the preceding slide, examining a few cases will usually indicate a pattern leading to a reasonable guess as to what the closed form might be:
(1) 2 (2) 4 (1) 4 2 (3) 4 (2) 4 4 2 (4) 4 (3) 4 4 4 2
Of course, the guess could be incorrect, but it's easy enough to verify that it satisfies the original recurrence relation. It's obvious that F (1) = 2. And, a little algebra shows that:
F N ( ) = 4 N − 2 = 4 + 4 N − 6 = 4 + 4( N −1) − 2 = 4 + F N ( −1)
So, F ( N ) satisfies the recurrence relation.
Computer Science Dept Va Tech March 2007 ©2007 McQuain
Complexity
Data Structures & OO Development II
There is a particularly elegant result that covers many recurrence relations which are commonly found when analyzing simple recursive algorithms:
Let a , b , and c be nonnegative constants. The solution to the recurrence relation
( ) ,^ if^1 ( / ) , if 1
T n b^ n aT n c bn n
log
( ), if ( ) is ( log ), if ( c^ a ), if
n a c T n n n a c n a c
for n a power of c satisfies:
The Design and Analysis of Computer Algorithms Aho Hopcroft & Ullman, adapted from p. 64
This result will be of considerable use when analyzing a number of algorithms.