Académique Documents
Professionnel Documents
Culture Documents
Christian Schulte
Johan Montelius
Software and Computer Systems
School of Information and Communication
Technology
KTH Royal Institute of Technology
Stockholm, Sweden
ID1218 2011
Runtime efficiency
what
Clearly
faster
is better
we have to know how fast our programs compute
we have to know how much memory our programs
require
we have to know how good a program is
Statements on Runtime
Testing is insufficient!
Runtime Guarantees
Runtime Guarantees
runtime function
where n is size of input
for example
integers
lists
argument
length of list
Runtime Guarantees
difference
by constant
c1 T(n) same as c2 T(n)
Asymptotic Complexity
Big-Oh Notation
Assume
T(n)
f(n)
some c
for some n0
(whatever computer)
(sufficiently large)
Big-Oh Notation
T(n)
cn
n0
f(n) = n, c=2
(whatever computer)
(sufficiently large)
Big-Oh Notation
= O(f(n))
T(n) O(f(n))
Examples
T(n)
T(n)
T(n)
T(n)
T(n)
=
=
=
=
=
4n + 42
is O(n)
5n + 42
is O(n)
7n + 11
is O(n)
4n + n3
is O(n3)
n100 + 2n + 8
is O(2n)
Big-Oh Notation
linear complexity
O(n2)
quadratic complexity
O(n3)
cubic complexity
O(log n) logarithmic complexity
O(2n)
exponential complexity
Summary
large input
independent of computer
numbers
abstracts away constants
captures asymptotic behavior of functions
Determining Complexity
Approach
Simplification Here
solving equation
Simple expression
values
call
recursive function call leads to recursive equation
often called: recurrence equation
Value
T(V) = cvalue
List construction
T([E1|E2]) = ccons + T(E1) + T(E2)
to ease
notation, just
forget subscript
Value
T(V) = c
List construction
T([E1|E2]) = c + T(E1) + T(E2)
Time T(E) needed for executing
expression E
how
Function Call
Function Call
T(F(E1, ..., Ek)) = ccall + T(E1) + ... +
T(Ek)
+ TF(size(IF({1, ..., k})))
input
size
Function Definition
Example: app/2
app([],Ys)
-> Ys;
app([X|Xr],Ys)
-> [X|app(Xr,Ys)].
Append: Recurrence
Equation
Analysis yields
Tapp(0) = c1
Tapp(n) = c2 + Tapp(n-1)
Solution to recurrence is
Tapp(n) = c1 + c2 n
Asymptotic complexity
Tapp(n) is of O(n) linear complexity
Recurrence Equations
defined in terms of
T(m1), , T(mk)
for m1, , mk < n
T(0),
T(1),
Possibilities
solve
Common Recurrence
Equations
T(n)
c + T(n1)
Asymptotic Complexity
O(n)
c1 + c2n + T(n1)
O(n2)
c + T(n/2)
O(log n)
c1 + c2n + T(n/2)
O(n)
c + 2T(n/2)
O(n)
c + 2T(n-1)
O(2n)
c1 + c2n + 2T(n/2)
O(n log n)
Analysis yields
Trev(0) = c1
Trev(n) = c2 + Tapp(n-1) + Trev(n-1)
= c2 + c3(n-1) + Trev(n-1)
Asymptotic complexity
Trev(n) is of O(n2) quadratic complexity
What Is to be Done?
be
necessary
Obs!
improve:
Nave reverse
O(n2)
be worse:
appending lists
reverse
O(n)
Can
O(n)
appending lists
O(n2)
Judging Asymptotic
Complexity
optimal?
Hard Problems
coloring
minimal number of colors
no connected node has same color
used in: compilation, frequency allocation,
Travelling
salesman
Hard Problems
Summary: Runtime
Efficiency
from constants
for all possible input
given in Big-Oh notation