CS-E3190 Lect02 PDF
CS-E3190 Lect02 PDF
CS-E3190 Lect02 PDF
CS-E3190
Pekka Orponen
Department of Computer Science
Aalto University
Autumn 2017
Lecture 2: Algorithms and their analysis
CE-3190 PAT
Autumn 2017
2/23
2.1 Analysis of algorithms: basic notions
CE-3190 PAT
Autumn 2017
3/23
2.2 Analysis of iterative algorithms
To illustrate the basic worst-case analysis of iterative
algorithms, let us consider yet another well-known (but bad)
sorting method.
CE-3190 PAT
Autumn 2017
4/23
Analysis of insertion sort
CE-3190 PAT
Autumn 2017
5/23
Basic analysis rules
Denote T [P] = the time complexity of an algorithm segment P.
I T [x e] = constant, T [read x] = constant, T [write x] =
constant.
I T [S1 ; S2 ; . . . ; Sk ] = T [S1 ] + + T [Sk ] =
O(max{T [S1 ], . . . , T [Sk ]})
I T [if P then S1 else S2 ] =
(
T [P] + T [S1 ] if P = true
T [P] + T [S2 ] if P = false
I T [while P do S] =
T [P] + (number of times P = true) (T [S] + T [P])
In analysing nested loops, proceed from innermost out. Control
variables of outer loops enter as parameters in the analysis of
inner loops.
CE-3190 PAT
Autumn 2017
6/23
Operation costs in programming languages
I If one is using a modern high-level programming language
(such as Python), one needs to be aware of the runtime
costs of basic operations.
I If an operation addresses a structured object, the actual
cost may depend highly on the language implementation.
I E.g. if A is a Python list of length n, then the cost of
obtaining a k -element slice such as A[0:k] is O(k ).
I In fact, even the cost of inserting a single element at or
near the beginning of the list, e.g. operation
A.insert(0,x) has cost O(n)! This is because Python lists
are implemented internally as simple linear arrays.
I The costs of several basic Python operations on structured
data are listed at
https://wiki.python.org/moin/TimeComplexity.
CE-3190 PAT
Autumn 2017
7/23
2.3 Analysis of recursive algorithms
Let us review the analysis of the merge sort algorithm.
Let M ERGE (A0 , A00 , A) be a subroutine which takes two sorted arrays
A0 [1 . . . m] and A00 [1 . . . (n m)] and merges them, element by
element, into a single sorted sequence in array A[1 . . . n] in O(n)
steps.
T (n) 2T ( n2 ) + c2 n
2(2T ( n4 ) + c2 n2 ) + c2 n
4T ( n4 ) + 2c2 n
... [general pattern emerges]
k n
2 T ( 2k ) + k c2 n
2log2 n T (1) + log2 n c2 n
c1 n + c2 n log2 n = O(n log2 n).
CE-3190 PAT
Autumn 2017
9/23
2.4 Order of growth of functions
CE-3190 PAT
Autumn 2017
10/23
Definitions and notations
I Let f : N ! R+ .
I Define the order (of growth class) of f as:
O(f ) = {t : N ! R+ |9c, n0 0:n n0 ) t(n) c f (n)}.
I Thus: t 2 O(f ), if t(n) is for all large enough n at most a
constant factor bigger than f (n).
I One usually uses the somewhat imprecise, but very
convenient notation t = O(f ) instead of the formal t 2 O(f ).
I Also, one denotes functions by means of a generic
argument n. Thus one writes, e.g.,
2n2 + 3n = O(n2 )
as a shorthand for
the function f , for which f (n) = 2n2 + 3n, is in the class
O(g), where g(n) = n2 .
CE-3190 PAT
Autumn 2017
11/23
Some typical function growth rates
T(n)
1 3
n - n
2 2
3000
5 n2
2000 100n
1000
n
5 10 15 20
CE-3190 PAT
Autumn 2017
12/23
Some examples and rules in the O-calculus
1. n2 = O(n3 ) i.e., to be precise, n2 2 O(n3 )
2. 5n3 + 3n2 = O(n3 )
3. loga n = O(logb n) for all a, b > 1
P
4. log2 (n!) = ni=1 log2 i = O(n log2 n)
5.
n
X 1
= O(1) constant
2i
i=0
CE-3190 PAT
Autumn 2017
13/23
Rules of thumb for determining order of growth
CE-3190 PAT
Autumn 2017
14/23
Properties of order classes
I.e., further rules in the O-calculus.
Example:
n3 + 3n2 + n + 8 2 O(|{z}
n3 + 3n 2 3
| +{zn + 8}) O(n )
g(n) f (n)
CE-3190 PAT
Autumn 2017
15/23
Comparing orders via limits
Theorem 1 Let f , g : N ! R+ . Then:
(i) If
f (n)
lim = c > 0,
n!1 g(n)
CE-3190 PAT
Autumn 2017
16/23
LHpitals rule
Theorem 2 Let f , g : R ! R. Assume that
lim f (x) = lim g(x) = 0 or 1,
x!1 x!1
and moreover that f 0 (x) and g 0 (x) exist and g 0 (x) 6= 0 for all
large enough x. Then
f (x) f 0 (x)
lim = lim 0 ,
x!1 g(x) x!1 g (x)
Some properties:
1. g 2 (f ) , g 2 O(f ) and f 2 O(g)
2. g 2 o(f ) , g 2 O(f ) and f 2
/ O(g)
3. g 2 (f ) , g 2
/ o(f )
CE-3190 PAT
Autumn 2017
18/23
Other order-of-growth notions (2/2)
One reads:
I f 2 (g): f is of the same order as g.
I f 2 o(g): f is of lower order than g.
I f 2 (g): f is of at least the same order as g.
CE-3190 PAT
Autumn 2017
19/23
Computing with order-of-growth classes (1/3)
Examples:
1. O(n2 ) + O(n3 ) = O(n3 )
2. n + O( n1 ) , {n} + O( n1 )
3. n O( n1 ) = O(n n1 ) = O(1)
CE-3190 PAT
Autumn 2017
20/23
Computing with order-of-growth classes (2/3)
Define also: [
O(X ) = O(f ),
f 2X
Examples:
1. O(O(n2 )) = O(n2 )
2. O(n O(n2 )) = O(O(n3 )) = O(n3 )
CE-3190 PAT
Autumn 2017
21/23
Computing with order-of-growth classes (3/3)
CE-3190 PAT
Autumn 2017
22/23
Estimating order-of-growth with the O-calculus
Example: Whatpis the dominating behaviour (with an error
estimate) of n( n n 1) as n ! 1?
p
n ln n ln n ln n 2
n=e n =1+ + O(( ) )
n n
p ln n ln n 2
) n( n n 1) = n ( + O(( ) )
n n
ln n 2
= ln n + n O(( ) )
n
ln2 n
= ln n + O( )
n
p
Consequently: The value of n( n n 1) grows approximately as
2
ln n. The order of the error in this estimate is O( ln n n ), which
goes to zero as n ! 1.
CE-3190 PAT
Autumn 2017
23/23