Professional Documents
Culture Documents
Performance Analysis
Time Complexity
for(i=0; i < N; i++) { for(i=0; i < N; i++) {
statement; for(j=0; j < N;j++) {
} statement;
}
}
• In server machine, it
• In desktop machine, it
requires 0.1ns
requires 0.2ns
• Algorithm 1 : 5n2 + 2n + 1
• Algorithm 2 : 10n2 + 8n + 3
f(n) = O(g(n))
Asymptotic Notation: Big - Oh Notation (O)
• Example
– Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x
g(n) for all values of C > 0 and n0>= 1
– f(n) <= C g(n)
3n + 2 <= C n
f(n) = Ω(g(n))
Asymptotic Notation: Big - Omege Notation (Ω)
• Example
– Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C
g(n) for all values of C > 0 and n0>= 1
– f(n) >= C g(n)
3n + 2 >= C n
• Above condition is always TRUE for all values of C = 1 and n >= 1.
By using Big - Omega notation we can represent the time complexity as
follows...
3n + 2 = Ω(n)
Asymptotic Notation: Big - Theta Notation (Θ)
• Big - Theta notation is used to define the average bound of
an algorithm in terms of Time Complexity.
• Consider function f(n) the time complexity of an algorithm
and g(n) is the most significant term. If C1 g(n) <= f(n) >= C2
g(n) for all n >= n0, C1, C2 > 0 and n0 >= 1. Then we can
represent f(n) as Θ(g(n)).
•
f(n) = Θ(g(n))
Asymptotic Notation: Big - Theta Notation (Θ)
• Example
– Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <=
f(n) >= C2 g(n) for all values of C1, C2 > 0 and n0>= 1
– C1 g(n) <= f(n) >= C2 g(n)
C1 n <= 3n + 2 >= C2 n
Function A Function #2
n0.1 log n
vs.
n + 100n0.1 2n + 10 log n
5n5 n!
n-152n/100 1000n15
82log n 3n7 + 7n
Race 1
In this one, crossover point is very late! So, which algorithm is really better???
Race C
5n5 vs. n!
Race 5
5n5 n! O(n5)
• Reflexivity
f(n) = (f(n))
f(n) = O(f(n))
f(n) = (f(n))
Properties
• Symmetry
f(n) = (g(n)) iff g(n) = (f(n))
• Complementarity
f(n) = O(g(n)) iff g(n) = (f(n))
f(n) = o(g(n)) iff g(n) = w((f(n))
Kinds of Analysis
Distinguish
– Worst case
• Your worst enemy is choosing input
– Average case
• Assume probability distribution of inputs
– Amortized
• Average time over many runs
– Best case (not too useful)
Analyzing Code