DS_CS01 Algorithm Analysis
DS_CS01 Algorithm Analysis
Implementation is a must.
Recursive Functions
void Test(int n)
{
if (n>0)
{ printf (“%d”,n);
Test (n-1);
}
}
T(n) =
Space
Space
Running Time
80
Now focus on how fast a function grows with the input size.
We call this the rate of growth of the running time.
Space
Space
Space
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Time
Space
Sum(a+b)
{
Return a+b
}
Tsum of matrix
Y
Tsum of list
Tsum
X (n-input size)
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
• On a graph, as
you go to the
right, a faster fA(n)=30n+8
Value of function
growing
function
eventually fB(n)=n2+1
becomes
larger...
Increasing n
More Examples …
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
O(n)
Computing running time (cont.)
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 2x N x (N+1) + c3 x N x N
O(n )
Thank You!!