Beruflich Dokumente
Kultur Dokumente
Algorithm Analysis
Memory - Time
It is important to be able to predict
how an algorithm behaves under a
range of possible conditions
usually different inputs
Algorithm Analysis
Running Time
The time to solve a problem increases with
the size of the input
measure the rate at which the time increases
e.g., linearly, quadratically, exponentially
Algorithm Analysis
Additional Consideration
Sometimes, simple but less efficient
algorithms are preferred over more
efficient ones
E.G.M. Petrakis
Algorithm Analysis
Bubble sort
Count the number of steps executed by
the algorithm
E.G.M. Petrakis
Algorithm Analysis
Algorithm Analysis
Growth Rate
Depending on the order of n an algorithm can beof
Linear time Complexity: T(n) = c n
Quadratic Complexity: T(n) = c n2
Exponential Complexity: T(n) = c nk
E.G.M. Petrakis
Algorithm Analysis
E.G.M. Petrakis
Algorithm Analysis
Algorithm Analysis
T(n) cg(n) n n0
n n0 , c 1
Example:
3
3
T(n) = n T(n) (n )
T(n) = 6n 2n + 7 T(n) O(n )
2
E.G.M. Petrakis
Algorithm Analysis
10
Properties of O
f (g)
O is transitive: if
f (h)
g (h)
If f O(g) f + g O(g)
f O(f )
If g O(g ) f g O(f g )
f O(f )
f + g O(f + g )
If
g O(g )
E.G.M. Petrakis
Algorithm Analysis
11
Algorithm Analysis
12
O in Practice
Among different algorithms solving
the same problem, prefer the
algorithm with the smaller rate of
growth O
it will be faster for large n
(1) < (log n) < (n) < (n log n) < (n2) < (n3)
< < (2n) < O(nn)
(n), (n log n), (n2) < (n3): polynomial
(log n): logarithmic
O(nk), O(2n), (n!), O(nn): exponential!!
E.G.M. Petrakis
Algorithm Analysis
13
n
100
10-6 sec/command
Complexity
1000n
20
.02
.5
.1
.9
.3
.6
1.5
4.5
10
100n 2
.04
.25
1.0
4.0
25
2min
100n 3
.08
1.0
10
1.0min
n logn
.4
1.1
2 n/3
.0001
0.1
2n
1.0
1000nlogn
nn
E.G.M. Petrakis
220da 125cent
21min 27hrs
5 108 cent
4
2.7hrs 3 10 cent
35yrs 3 10 4 cent
14
f(n) cg(n)
g(n) is a lower bound of the rate of
growth of f(n)
f(n) increases faster than g(n) as n
increases
e.g: T(n) = 6n2 2n + 7 =>T(n) in (n2)
E.G.M. Petrakis
Algorithm Analysis
15
Algorithm Analysis
16
Thet
If the lower and upper bounds
are the same:
f O(g)
f (g)
f (g)
Algorithm Analysis
17
Proof:
T(n) |T(n)| |nnm|+|m-1nm-1|+|1n|+|0|
nm{|m| + 1/n|m-1|++1/nm-1|1|+1/nm|0|}
nm{|m|+|m-1|++|1|+|0|}
T(n) O(nm)
E.G.M. Petrakis
Algorithm Analysis
18
Theorem (cont.)
b) T(n) = mnm + m-1nm-1 + 1n + 0 >=
cnm + cnm-1 + cn + c >= cnm
where c = min{m, m-1, 1, 0} =>
T(n) (nm)
Algorithm Analysis
19
Theorem:
(a)
(b)
T(n) = i k (n k +1 ), k 0
i =1
i =1
i =1
i =1
2 T(n) = i k + (n - i + 1) k =
n
n
k
k
(i + (n - i + 1) ) ( )
i =1
i =1 2
n
(either i
2
T(n)
E.G.M. Petrakis
or
n
(n - i + 1) )
2
1
2
k +1
k +1
n
T
(
n)
(n
)
k +1
Algorithm Analysis
20
T(n) =
T(n - 1) + n if n > 1
T(n) = T(n - 1) + n =
= T(n - 2) + (n - 1) + n =
= ......
= T(1) + 2 + .... + (n - 1) + n =
n(n + 1)
= i =
T(n) (n 2 )
2
i =1
n
E.G.M. Petrakis
Algorithm Analysis
21
Fibonacci Relation
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) if n 2
(n - 2)/2
if n even
F(n) 2
E.G.M. Petrakis
Algorithm Analysis
22
(n)
1+ 5
=
= 1.6180
2
golden ratio
E.G.M. Petrakis
Algorithm Analysis
23
Binary Search
int BSearch(int table[a..b], key, n)
{
if (a > b) return ( 1) ;
int middle = (a + b)/2 ;
if (T[middle] == key) return (middle);
else if ( key < T[middle] )
return BSearch(T[a..middle 1], key);
else return BSearch(T[middle + 1..b], key);
}
E.G.M. Petrakis
Algorithm Analysis
24
c k =0
T(n) =T(2 - 1) =
k 1
1) k > 0
d +T(2
k
Algorithm Analysis
25
E.G.M. Petrakis
Algorithm Analysis
26
T(n) =
d + max{T((n 1)/2),T( (n 1)/2)} k > 0
T(n) <= T(n+1) <= ...T(u(n)) where u(n)=2k 1
For some k: n < u(n) < 2n
Then, T(n) <= T(u(n)) = d log(u(n) + 1) + c =>
d log(2n + 1) + c => T(n) O(log n)
E.G.M. Petrakis
Algorithm Analysis
27
Merge sort
list mergesort(list L, int n)
{
if (n == 1) return (L);
L1 = lower half of L;
L2 = upper half of L;
return merge (mergesort(L1,n/2),
mergesort(L2,n/2) );
}
n: size of array L (assume L some power of 2)
merge: merges the sorted L1, L2 in a sorted array
E.G.M. Petrakis
Algorithm Analysis
28
E.G.M. Petrakis
Algorithm Analysis
29
log2n merges
[57]
[48]
[37]
[12]
[92]
[86]
[33]
[25
57]
[37
48]
[12
92]
[33
86]
[25
37
48
57]
[12
33
86
92]
[12
25
33
37
48
57
86
92]
Algorithm Analysis
30
Two ways:
recursion analysis
induction
E.G.M. Petrakis
Algorithm Analysis
31
Induction
Let T(n) = O(n log n) then T(n) can be
written as T(n) <= a n log n + b, a, b > 0
Proof: (1) for n = 1, true if b >= c1
E.G.M. Petrakis
Algorithm Analysis
32
Induction (cont.)
T(n) = a n log n a n + 2 b + c2 n
Let b = c1 and a = c1 + c2 =>
T(n) <= (c1 + c2) n log n + c1 =>
T(n) <= C n log n + B
E.G.M. Petrakis
Algorithm Analysis
33
Algorithm Analysis
34
...
x = pi xi = (n + 1)/2
comparisons
Algorithm Analysis
35
Comparison
The best-case is not representative
happens only rarely
useful when it has high probability
E.G.M. Petrakis
Algorithm Analysis
36