Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Asymptotic Analysis of Algorithms

The previous chapter presents a detailed model of the computer which involves a number of different timing parameters-- tex2html_wrap_inline58177, tex2html_wrap_inline58179, tex2html_wrap_inline58189, tex2html_wrap_inline58191, tex2html_wrap_inline58193, tex2html_wrap_inline58195, tex2html_wrap_inline58197, tex2html_wrap_inline58201, tex2html_wrap_inline58203, tex2html_wrap_inline58579, tex2html_wrap_inline58581, and tex2html_wrap_inline58237. We show that keeping track of the details is messy and tiresome. So we simplify the model by measuring time in clock cycles, and by assuming that each of the parameters is equal to one cycle. Nevertheless, keeping track of and carefully counting all the cycles is still a tedious task.

In this chapter we introduce the notion of asymptotic bounds, principally big oh, and examine the properties of such bounds. As it turns out, the rules for computing and manipulating big oh expressions greatly simplify the analysis of the running time of a program when all we are interested in is its asymptotic behavior.

For example, consider the analysis of the running time of Program gif, which is just Program gif again, an algorithm to evaluate a polynomial using Horner's rule.

   program1841
Program: Program gif Again

 

 

statement detailed model simple big oh
model
3 tex2html_wrap_inline58239 5 O(1)
4a tex2html_wrap_inline58255 4 O(1)
4b tex2html_wrap_inline58219 3n+3 O(n)
4c tex2html_wrap_inline58259 4n O(n)
5 tex2html_wrap_inline58261 9n O(n)
6 tex2html_wrap_inline58225 2 O(1)
TOTAL tex2html_wrap_inline60243 16n+14 O(n)
tex2html_wrap_inline60249
tex2html_wrap_inline58267
Table: Computing the running time of Program gif

Table gif shows the running time analysis of Program gif done in three ways--a detailed analysis, a simplified analysis, and an asymptotic analysis. In particular, note that all three methods of analysis are in agreement: Statements 3, 4a, and 6 execute in a constant amount of time; 4b, 4c, and 5 execute in an amount of time which is proportional to n, plus a constant.

The most important observation to make is that, regardless of what the actual constants are, the asymptotic analysis always produces the same answer! Since the result does not depend upon the values of the constants, the asymptotic bound tells us something fundamental about the running time of the algorithm. And this fundamental result does not depend upon the characteristics of the computer and compiler actually used to execute the program!

Of course, you don't get something for nothing. While the asymptotic analysis may be significantly easier to do, all that we get is an upper bound on the running time of the algorithm. In particular, we know nothing about the actual running time of a particular program. (Recall Fallacies gif and gif).




next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.