Algorithm Analysis
Algorithm Analysis
return sum; 1
}
Time complexity: T(N) = 4*N + 4
Asymptotic Analysis
N 50 N 2 31N 3 24 N 15 3 N 2 N 21 4 3 N
1 120 37
2 511 71
3 1374 159
4 2895 397
5 5260 1073
6 8655 3051
7 13266 8923
8 19279 26465
9 26880 79005
10 36255 236527
What Happened?
N 3 N 2 N 21 4 3 N 4 3N %ofTotal
1 37 12 32.4
2 71 36 50.7
3 159 108 67.9
4 397 324 81.6
5 1073 972 90.6
6 3051 2916 95.6
7 8923 8748 98.0
8 26465 26244 99.2
9 79005 78732 99.7
10 236527 236196 99.9
50 N 2 31N 3 24 N 15
3 N 2 N 21 4 3 N
Ordering:
1 log 2 N N N log 2 N N N 2 3
2 3 N N
Order of Magnitude Analysis (cont)
Furthermore, simply ignore any constants in
front of term and simply report general class
of the term:
31N 3 4 3N 15 N log 2 N
3
50 N 2 31N 3 24 N 15 grows proportionally to N
N
3 N N 21 4 3
2 N
grows proportionally to 3
When comparing algorithms, determine
formulas to count operation(s) of interest,
then compare dominant terms of formulas
Obtaining Asymptotic Bounds
• Eliminate low order terms
– 4n + 5 4n
– 0.5 n log n - 2n + 7 0.5 n log n
– 2n + n3 + 3n 2n
• Eliminate coefficients
– 4n n
– 0.5 n log n n log n
– n log n2 = 2 n log n n log n
Big O Notation
• Algorithm A requires time proportional to f(N) - algorithm is said
to be of order f(N) or O(f(N))
• Definition: an algorithm is said to take time proportional to
O(f(N)) if there is some constant C such that for all but a finite
number of values of N, the time taken by the algorithm is less
than C*f(N)
• T(n) = O(f(n)): growth rate of T(n) that of f(n)
– constants c and n0 s.t. T(n) c f(n) n n0
– Or if limnT(n)/f(n) exists and is finite, then T(n) is O(f(n))
Examples:
50 N 2 31N 3 24 N 15 is O( N 3 )
3 N 2 N 21 4 3 N is O(3N )
Big O Notation(2)
• If an algorithm is O(f(N)), f(N) is said to be
the growth-rate function of the algorithm.
T(n)= ?
Ex2. for i = 1 to n do
for j = i to n do
for k= i to j do
sum = sum + 1
T(n)= ?
Quiz
Conditions
• Worst-case running time: the test, plus
either the then part or the else part
(whichever is the larger).
• Conditional
if C then S1 else S2
T(n) <= time of C + MAX (S1, S2)
<= time of C + S1 + S2
• Ex: If (N==0) return 0;
else {
for (i=0; i<N; i++) sum++;
}
Consecutive statements
T(N)= ?
Logarithms in running time
• Example 1:
for (i = N; i>=1;)
i=i/2;
long gcd( int m, int n )
T(N)= O(logN) {
while( n != 0 )
• Example 2: {
Greatest common divisor int rem = m % n;
m = n;
n = rem;
}
return m;
}
Recursion
• Recursion
– Function calls its self
– Some cases are very difficult to analyze
• Example: Factorial
• Normal case O(N)
• Recursion:
fac(n)
if n = 0 return 1
else return n * fac(n - 1)
T(0) = 1
T(n) c + T(n - 1) if n > 0
Example: Factorial
Analysis by substitution method
T(n) c + c + T(n - 2)
(by substitution)
T(n) c + c + c + T(n - 3)
(by substitution, again)
T(n) kc + T(n - k)
(extrapolating 0 < k n)
T(n) nc + T(0) = nc + b
(setting k = n)
• T(N) = 2T(N/2) + 1
• T(N) = 2T(N/2) + n
Master Theorem for Divide and
Conquer
T(n) = aT(n/b) + (nklogpn)
a>=1, b> 1, k>= 0 and p is a real number
Case (1): a bk then T(n) = (nlogba )
Case(2): a = bk :
if p > -1 then T(n) = (nlogbalogp+1n)
if p = -1 then T(n) = (nlogbaloglogn)
if p < -1 then T(n) = (nlogba )
Case(3): a < bk :
if p >= 0 then T(n) = (nklogpn)
if p = 0 then T(n) = O(nk)
Examples
Admissible equations:
Inadmissible equations:
Inner loop:
j=iN-1 (j-i + 1) = (N – i + 1)(N-i)/2
Outer Loop:
i=0N-1 (N – i + 1)(N-i)/2 = (N3 + 3N2 + 2N)/6
Overall: O(N3)
Algorithm 2
Divide and Conquer
Proof:
= 2.cn/2 + 4T(n/4) + cn
= 4T(n/4) + 2cn
= 8T(n/8) + 3cn
=…………..
= 2iT(n/2i) + icn
=………………… (reach a point when n = 2i i=log n)
= n.T(1) + cnlog n = O(nlogn)
Algorithm 4
O(n) complexity
Summary
• Describe running time of an algorithm as a
mathematical function of input size.
• Terminologies: O(•), (•), (•).
• How to analyze a program
• Example: Maximum Subsequence Problem
• Next week: Sorting