Professional Documents
Culture Documents
The running time of an algorithm varies with the input and typically grows with the input size Average case difficult to determine We focus on the worst case running time
Easier to analyze Crucial to applications such as games, finance and robotics
best case average case worst case
120 100
Running Time
80 60 40 20 0
1000
2000
3000
4000
Input Size
Experimental Studies
Write a program implementing the algorithm Run the program with inputs of varying size and composition Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time Plot the results
9000 8000 7000
Time (ms)
Input Size
Limitations of Experiments
It is necessary to implement and test the algorithm in order to determine its running time which may be difficult Results may not be indicative of the running time on other inputs not included in the experiment.
In order to compare two algorithms, the same hardware and software environments must be used
Pseudocode
Example: find max High-level description element of an array of an algorithm More structured than Algorithm arrayMax(A, n) English prose Input array A of n integers Less detailed than a Output maximum element of A program currentMax A[0] Preferred notation for for i 1 to n 1 do describing algorithms if A[i] currentMax then Hides program design currentMax A[i] issues return currentMax
Pseudocode Details
Control flow
if then [else ] while do repeat until for do Indentation replaces braces
Method call
var.method (arg [, arg])
Return value
return expression
Expressions
Assignment (like in Java) Equality testing (like in Java) n2 Superscripts and other mathematical formatting allowed
Method declaration
Algorithm method (arg [, arg]) Input Output
Primitive Operations
Examples: Basic computations Evaluating an expression performed by an algorithm (if i<n) Identifiable in pseudocode Assigning a value to a variable (i5) Largely independent from Indexing into an array the programming language (A[4]) Exact definition not Calling a method important (we will see why (O.method(arg)) later) Returning from a method Assumed to take a (return i) constant amount of time in Performing an arithmetic operation (i+j) the RAM model
# operations 1 n (n 1) (n 1) (n 1) 1
Total 4n 1
The linear growth rate of the running time T(n) is an intrinsic property of algorithm arrayMax
Growth Rates
Growth rates of functions:
Linear n Quadratic n2 Cubic n3
n 1 2 4 8 10 100 1000
Constant Factors
The growth rate is not affected by constant factors or lower-order terms Examples 10n is a linear function n2 + 10n is a quadratic function
Example:
We determine that algorithm arrayMax executes at most 8n - 2 primitive operations We say that algorithm arrayMax runs in O(n) time
Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitive operations
Arithmetic Progression
The running time of prefixAverages1 is O(1 + 2 + + n) The sum of the first n integers is n(n + 1) / 2 - There is a simple visual proof of this fact Thus, algorithm prefixAverages1 runs in O(n2) time
(Try it yourself)
Algorithm arrayMax(A, n) Input array A of n integers Output maximum element of A currentMax A[0]