Lectut CSN 102 PDF DS - Part1
Lectut CSN 102 PDF DS - Part1
Performance Analysis
Fundamental Concepts
Two criteria are used to judge algorithms: (i) time complexity (ii) space
complexity.
Space Complexity of an algorithm is the amount of memory it needs to
run to completion.
Time Complexity of an algorithm is the amount of CPU time it needs to
run to completion.
Space Complexity
1. Algorithm Sum(a[], n)
2. {
3. s:= 0.0;
4. for i = 1 to n do
5. s := s + a[i];
6. return s;
7. }
Space Complexity: Example 2.
For most of the algorithms associated with this course, time complexity
comparisons are more interesting than space complexity comparisons
Time complexity: A measure of the amount of time required to execute an
algorithm
Time Complexity
- 32 bit
- Sequential executer
- 1 unit time for
Arithmetic and
Logical Operations
- 1 unit time for
assignment and return
Time Complexity
500 f(n) = n2
T(n) = n2/2 + n/2
400
# of mult’s
300
200
100
g(n) = n
5 10 15 20 25 30 35
n (degree of polynomial)
Time Complexity
1. Loops
2. Nested Loops
3. Consecutive Statements
4. if –else statements
Few Examples
TAdd=1+1=2 units of
time
= Constant time
Time Complexity: Example 1
Statements Total
1 Algorithm Sum(a[],n) 0
2 { 0
3 S = 0.0; 1
4 for i=1 to n do n+1
5 s = s+a[i]; n
6 return s; 1
7 } 0
2n+3
Time Complexity: Example 2
Statements Total
1 Algorithm Sum(a[],n,m) 0
2 { 0
5 s = s+a[i][j]; nm
6 return s; 1
7 } 0
2nm+2n+2
Few Examples
TSum_of_list=1+3(n+1)+2n+1
=5n+5
=cn+c’
Tsum_of_Matrices =a*n^2+b*n+c
TAdd =O(1)
TSum_of_list = O(n)
Tsum_of_Matrices =O(n2)
Asymptotic Notation
Performance Measurement
Which is better?
– T(P1) = (n+1) or T(P2) = (n2 + 5).
– T(P1) = log (n2 + 1)/n! or T(P2) = nn(nlogn)/n2.
Complex step count functions are difficult to compare.
For comparing, ‘rate of growth’ of time and space complexity functions is
easy and sufficient.
Asymptotic Notations
O, W, Q, o, w
Defined for functions over the natural numbers.
– Ex: f(n) = Q(n2).
– Describes how f(n) grows in comparison to n2.
Define a set of functions; in practice used to compare
two function sizes.
The notations describe different rate-of-growth
relations between the defining function and the
defined set of functions.
Asymptotic Notations
= n0
Big-Oh Notation (Formal Definition)
Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive
constants
c and n0 such that
f(n) cg(n) for n n0
Example: 2n + 10 is O(n)
2n + 10 cn
(c 2) n 10
n 10/(c 2)
Pick c = 3 and n0 = 10
Big O Notation
Example: f(n) = 10n2+4n+2 is O(n2) because 10n2+4n+2 <= 11n2 for all n
>=5.
Example: f(n) = 6*2n+n2 is O(2n) because 6*2n+n2 <=7*2n for all n>=4.
Algorithms can be: O(1) constant; O(log n) logrithmic; O(nlogn);
O(n) linear; O(n2) quadratic; O(n3) cubic; O(2n)
exponential.
Big O Notation
3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3)
need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n n0
this is true for c = 4 and n0 = 21
3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 1 such that 3 log n + 5 c•log n for n n0
this is true for c = 8 and n0 = 2
Big-Oh and Growth Rate
The big-Oh notation gives an upper bound on the growth rate of a function
The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than
the growth rate of g(n)
Useful to find the worst case of an algorithm
Find the output of the following code
int n=32;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps;
Example of O(log n) algorithm.
Example
W(g(n)) = {f(n) : positive constants c and n0, such
that n n0, we have 0 cg(n) f(n)}
c * log n n , n 16
Omega
Omega gives us a LOWER BOUND on a function.
10n2 - 3n = Q(n2)
What constants for n0, c1, and c2 will work?
Make c1 a little smaller than the leading coefficient, and c2 a little bigger.
To compare orders of growth, look at the leading term.
3n2 + 17
f(n)=3n+2 is o(n2)
fg ab
f (n) = O(g(n)) a b
f (n) = W(g(n)) a b
f (n) = Q(g(n)) a = b
f (n) = o(g(n)) a < b
f (n) = w (g(n)) a > b
Limits
lim
n
[f(n) / g(n)] = 0 f(n) o(g(n))
lim
n
[f(n) / g(n)] < f(n) O(g(n))
0 < lim
n
[f(n) / g(n)] < f(n) Q(g(n))
0 < lim
n
[f(n) / g(n)] f(n) W(g(n))
lim
n
[f(n) / g(n)] = f(n) w(g(n))
lim
n
[f(n) / g(n)] undefined can’t say
Time Complexity
int main()
{ int i, sum=0;
int n; Time Complexity - O(n)
cin>>n; Space complexity – O(1)
for (i=0; i<n; i++)
sum+=i;
}
Examples
int main()
{ int n;
cin>>n; Time Complexity - O(n2)
int *arr;
arr=new int [n]; Space complexity – O(n)
for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
{
some statements;
}
return 0;
}}
const int n=100; Time Complexity - O(n2)
int main()
{ Space complexity – O(n)
for (int i=0;i<n;i++)
f();
return 0;
}
void f()
{ int a[n];
for (j=0;j<n;j++)
{
some statements;
}
}
int n=64;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps; Time Complexity - O(log n)
int* A; Space complexity – O(log n)
A=new int[steps];
….
….
Programming Contest Sites
http://icpc.baylor.edu/public/worldMap/World-Finals-
2015 (ACM ICPC)
http://www.codechef.com (Online Contest)
http://www.topcoder.com
http://www.spoj.com
http://www.interviewstreet.com