Computational Complexity: CSD-202 Data Structure and Algorithms
Computational Complexity: CSD-202 Data Structure and Algorithms
T(n)=3n+4
It means this algorithm will take 3n+4 steps/statements for given value of n.
T(n,k)=1+1+n+1+n+n+nk+n+nk+nk+1= 3nk+4n+4
Solution?
Focus on growth rate /order of T(n), rather than it’s actual value
As n grows large
Terms with n changes more than terms which are constant
constants can be ignored for larger values of n as their contribution to T remains same
Terms with largest order of n becomes dominant than terms with lower order of n
It means lower order terms can be neglected for larger values of n to find growth order, because their
contribution to T grows at slower rate with compare to terms with higher order as n grows
Coefficients can also be neglected
They become irrelevant when comparing two functions with different order(they shouldn’t be too large
though)
3. sum++;
1. for (i=1;i<n/2; i+=1)
4. print sum
2. print i*n
2. print i
2. print i
2. for(i=1;i<=n; i*=2)
3. print i
3. print i
T(n)= 3n2+4n+4
25 Created by Saba Anwar, Revised and edited by Asmara Safdar, 02/09/2019
Computer Science Department- CIIT Lahore
Cubic
When algorithm time is directly proportional to cube of input size?
Triple nested loops
1. sum=0
2. for (i=0;i<n;++i)
3. for (j=0;j<n;++j)
4. for (k=0;k<j;++k)
5. sum++
6. print sum
T(n)= ??