Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
JAVA Programmer
Asymptotic Analysis
Kinds of Analysis
Sohail IMRAN
m a
Asymptotic Notations
Asymptotic Notations , O, , o,
We use to mean order exactly,
Big-O order at most:
O (f(N) ) is bounded from above at most!
Big-Omega order at least:
(f(N) ) is bounded from below at least!
Little-o tight upper bound:
o (f(N) ) is bounded from above smaller than!
Little-omega tight lower bound:
(f(N) ) is bounded from below greater than!
Theta: (f(N) ) bounded, above & below equal to!
Define a set of functions: which is in practice used to compare
two function sizes.
Sohail IMRAN
m a
Complexity
Example:
T(n) = 2n+4
m a
n
n3
m a
Sohail IMRAN
m a
Asymptotic Complexity
Example:
T(n) = 2n3
T(n) = 1000n2
Which one is better?
For large values of n or
small values of n
Sohail IMRAN
m a
General Rules
Rule One: Loops
The running time of a loop is at most the running time of the statements inside the
loop, multiplied by the number of iterations.
Example:
for (i = 0; i < n; i++)
// n iterations
A[i] = (1-t)*X[i] + t*Y[i]; // 12 time units
// per iteration
(Retrieving X[i] requires one addition and one memory access, as does retrieving
Y[i]; the calculation involves a subtraction, two multiplications, and an addition;
assigning A[i] requires one addition and one memory access; and each loop
iteration requires a comparison and either an assignment or an increment, thus
totals twelve primitive operations.)
Sohail IMRAN
m a
Example:
for (i = 0; i < n; i++)
C[i,j] = j*A[i] + i*B[j];
(2 for retrieving A[i], 2 for retrieving B[j], 3 for the RHS arithmetic, 3 for assigning C[i,j].)
Sohail IMRAN
m a
10
m a
Example:
for (i = 0; i < n; i++) {
A[i] = (1-t)*X[i] + t*Y[i];
B[i] = (1-s)*X[i] + s*Y[i];
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
C[i,j] = j*A[i] + i*B[j];
Total running time: 12n2+24n time units, i.e., this code is O(n2).
Sohail IMRAN
11
m a
12
m a
13
m a
Sohail IMRAN
14
m a
Example
Begin
Initialize n
1
For i=0 to n
n+2
For j=0 to m (n+1)(m+2) = nm+n+m+2
Print I
(n+1)(m+1)(1) = nm+n+m+1
End For
End For
End
Total
T(nm) = 2nm + 3n + 2m + 6
T(n) = 2n2 + 5n + 6 (Let nm)
O(n) = n2
Sohail IMRAN
15
m a
N2 / 2 3N = O(N2)
1 + 4N = O(N)
7N2 + 10N + 3 = O(N2) = O(N3)
log10 N = log2 N / log2 10 = O(log2 N) = O(log N)
sin N = O(1); 10 = O(1), 1010 = O(1)
i 1
i 1
i N N O( N 2 )
i 2 N N 2 O( N 3 )
log N + N = O(N)
logk N = O(N) for any constant k
N = O(2N), but 2N is not O(N)
210N is not O(2N)
Sohail IMRAN
16
m a