Introduction to Algorithm Complexity Analysis
Introduction to Algorithm Complexity Analysis
Joost-Pieter Katoen
Formal Methods and Tools Group
E-mail: katoen@[Link]
c JPK
#1: Introduction to Algorithm Analysis ADC (214020)
Overview
Introduction
– Measuring algorithm complexity: what, how and why?
– Practical relevance
Space complexity
Organisational matters
c JPK 1
#1: Introduction to Algorithm Analysis ADC (214020)
c JPK 2
#1: Introduction to Algorithm Analysis ADC (214020)
Assess efficiency of algorithms, independent of:
c JPK 3
#1: Introduction to Algorithm Analysis ADC (214020)
logarithmic quadratic
linear exponential
c JPK 4
#1: Introduction to Algorithm Analysis ADC (214020)
Overview
Introduction
– Practical relevance
Space complexity
Organisational matters
c JPK 5
#1: Introduction to Algorithm Analysis ADC (214020)
c JPK 6
#1: Introduction to Algorithm Analysis ADC (214020)
Run time
Input size
c JPK 7
#1: Introduction to Algorithm Analysis ADC (214020)
The best case complexity:
min
The average case complexity:
How do we know:
– by analyzing the algorithm under consideration
c JPK 8
#1: Introduction to Algorithm Analysis ADC (214020)
Linear search
Input: array with entries and item to be looked up
int index ; // start at the front
bool found false; // assume absence of
while index found
found index ;
index index
return found
c JPK 9
#1: Introduction to Algorithm Analysis ADC (214020)
is all permutations of elements out of a set of elements
in in
c JPK 10
#1: Introduction to Algorithm Analysis ADC (214020)
– does not occur in array ; this yields average complexity
in not in
not in in
in time
c JPK 11
#1: Introduction to Algorithm Analysis ADC (214020)
in time
in
(* calculus *)
c JPK 12
#1: Introduction to Algorithm Analysis ADC (214020)
in in
Note that if in equals
c JPK 13
#1: Introduction to Algorithm Analysis ADC (214020)
Optimality (I)
– algorithm class: set of algorithms that use the same allowed operations
– measure of complexity: the basic operations to be considered
Lower bound : for any algorithm in a class and any input size
Algorithm is optimal if
c JPK 14
#1: Introduction to Algorithm Analysis ADC (214020)
Optimality (II)
Problem: find largest element in array of real (of size )
Class: comparisons of reals allowed, but no other manipulations
Measure of complexity: comparison of reals
real max
if max index max index
return max
c JPK 15
#1: Introduction to Algorithm Analysis ADC (214020)
Optimality (III)
Problem: compute the product of square matrices and
Class: , , and division of real numbers
for
for
for
return
c JPK 16
#1: Introduction to Algorithm Analysis ADC (214020)
Overview
Introduction
– Practical relevance
Space complexity
Organisational matters
c JPK 17
#1: Introduction to Algorithm Analysis ADC (214020)
Asymptotic analysis
– use for comparison doubtful: is better than ?
– we want to ignore machine-dependent constants (e.g., processor speed)
– thus ignoring small inputs and constant factors
– intuition: drop lower order terms, e.g.,
(i.e., is the dominating factor when goes to )
– thus, we obtain lower/upper bounds on , and now!
– mathematical ingredient: asymptotic order of functions (classes , and )
c JPK 18
#1: Introduction to Algorithm Analysis ADC (214020)
Let and be functions from (input size) to (run time)
is the set of functions that grow at least as fast as
is the set of functions that grow at the same rate as
and is a lower bound on
c JPK 19
#1: Introduction to Algorithm Analysis ADC (214020)
Run time
Run time
Input size Input size
Run time
Input size
c JPK 20
#1: Introduction to Algorithm Analysis ADC (214020)
Functions that grow
at least as fast as Functions that grow Functions that grow
no faster than
c JPK 21
#1: Introduction to Algorithm Analysis ADC (214020)
Handy alternative: if for
– note that if are differentiable then
Example: consider . We have:
– since
– since for
c JPK 22
#1: Introduction to Algorithm Analysis ADC (214020)
Handy alternative: if
– recall that if are differentiable then
Example: consider . We have:
– since
– since
– since for
c JPK 23
#1: Introduction to Algorithm Analysis ADC (214020)
if such that
Handy alternative: if for some
– recall if and only if and
Example: consider . We have:
– since
– since
– since but
of a function
c JPK 24
#1: Introduction to Algorithm Analysis ADC (214020)
–
–
–
Transitivity:
– and imply
– and imply
– and imply
Symmetry:
– if and only if
– if and only if
c JPK 25
#1: Introduction to Algorithm Analysis ADC (214020)
Compl.
Solution time
10 .00033 sec .0015 sec .0013 sec .0034 sec .001 sec
10 .003 sec .03 sec .13 sec 3.4 sec 4 yr
10 .033 sec .45 sec 13 sec .94 hour
c JPK 26
#1: Introduction to Algorithm Analysis ADC (214020)
Compl.
time allowed Maximum solvable input size
We cannot handle input 60 times larger if we increase time (or speed) by factor 60
c JPK 27
#1: Introduction to Algorithm Analysis ADC (214020)
# steps performed maximum feasible input size
on input of size
c JPK 28
#1: Introduction to Algorithm Analysis ADC (214020)
Overview
Introduction
– Practical relevance
Space complexity
Organisational matters
c JPK 29
#1: Introduction to Algorithm Analysis ADC (214020)
Space complexity
– since can be large we memorize songs which require a small amount of brain
– let be the space complexity of songs of length
–
since in the worst case we must memorize each word
c JPK 30
#1: Introduction to Algorithm Analysis ADC (214020)
The refrain
Memorize a refrain only once, but sing it times. Does this help?
No. Not in terms of asymptotic complexity
c JPK 31
#1: Introduction to Algorithm Analysis ADC (214020)
If then , thus
c JPK 32
#1: Introduction to Algorithm Analysis ADC (214020)
[Andy Kaufman]
– thus
count
That’s the way, uh-huh, uh-huh
I like it, uh-huh, huh
[KC & the Sunshine band, 1977]
c JPK 33
#1: Introduction to Algorithm Analysis ADC (214020)
Overview
Introduction
– Practical relevance
Space complexity
Organisational matters
c JPK 34
#1: Introduction to Algorithm Analysis ADC (214020)
Course topics
Algorithm analysis techniques Priority queues
Elementary data structures Graph algorithms
Sorting Dynamic programming
Red-black trees Computability
+ Algorithm design strategies
– Divide-and-conquer, dynamic programming, greedy methods
– Breadth-first and depth-first searching
c JPK 35
#1: Introduction to Algorithm Analysis ADC (214020)
Course material
c JPK 36
#1: Introduction to Algorithm Analysis ADC (214020)
Course work
– presence is mandatory
– mandatory to be allowed to do the exam!
You only get acquainted with the material by actively solving exercises yourself
c JPK 37