Docsity

Analyzing Algorithm Complexity: Solving Recurrence Relations - Prof. William D. Mcquain, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 3204 Operating Systems
©William D McQuain, January 2005 1
Computer Science Dept Va Tech March 2007 ©2007 McQuain
Complexity
Data Structures & OO Development II
1
Recursion and Complexity
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
N
TN TN 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
2
Solving Recurrence Relations
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)442
(4)4 (3)4442
T
TT
TT
TT
=
=+ =+
=+ =++
=+ =+++ () 4 2FN N
=
−
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:
() 4 2 44 6 44( 1)2 4 ( 1)FN N N N FN= −=+ −=+ −−=+ −
So, F(N) satisfies the recurrence relation.
pf2

Partial preview of the text

Download Analyzing Algorithm Complexity: Solving Recurrence Relations - Prof. William D. Mcquain and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Computer Science Dept Va Tech March 2007 ©2007 McQuain

Complexity

Data Structures & OO Development II

Recursion and Complexity 1

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

Solving Recurrence Relations 2

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

T

T T

T T

T T

= + = + + + F N (^^ )^ =^4 N −^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

Complexity of Recurrence Relations 3

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.