Presentation is loading. Please wait.

Presentation is loading. Please wait.

Amortised Analysis.

Similar presentations


Presentation on theme: "Amortised Analysis."— Presentation transcript:

1 Amortised Analysis

2 What is Amortized analysis
It refers to determining the time-averaged running time for sequence of operations. It doesn't make any assumption about distribution of data values. Average case analysis make assumption that data is scattered evenly. It is worst case analysis for a sequence of operations, rather than individual operations.

3 Amortized Analysis It generally applies to a method that consists of sequence of operations, where majority of operations take less time, but some of operations take more time. If we can show that the operations that take more time rarely occurs and then time complexity is upper bound of cheap operation.

4 Example Given an array of 'N' integers we want the difference between the sum till value of two given indices. The number of time the program run is very large. Ex If the array is 1, 8, 6, 4, 9, 7, 2 Let x = 3 and y = 5 (x and y are indices) Sum till index 3 = 19 Sum till index 5 = 35 Difference = 35 – 19 = 16

5 Solution : we use an auxiliary array to store the cumulative frequency of the given array.
The cumulative frequency array becomes, Now if the index , x = 3 and y = 5 We know sum till index 3 is C[3] and sum till index 5 is C[5] Difference = C[5] – C[3] = 35 – 19 = 16 C[0] C[1] C[2] C[3] C[4] C[5] C[6] 1 9 15 19 28 35 37

6 Time Complexity = O(n), Amortized Complexity = O(1)
Program : // Calculate the cumulative frequency c[0]=a[0]; for(i=1; i<n; i++) c[i] = c[i-1] + c[i]; //Done at start, Only once. while(t--) // 't' number of test cases. { scanf(“%d%d”,&x,&y); printf(“%d\n”,abs(c[y]-c[x])); //Constant Time } Now that each test case takes constant time, the while loop complexity is O(1) Time Complexity = O(n), Amortized Complexity = O(1)

7 Analysis of Recursive Algorithms

8 Some properties of asymptotic order of growth
f(n)  O(f(n)) f(n)  O(g(n)) iff g(n) (f(n)) If f (n)  O(g (n)) and g(n)  O(h(n)) , then f(n)  O(h(n)) Note similarity with a ≤ b If f1(n)  O(g1(n)) and f2(n)  O(g2(n)) , then f1(n) + f2(n)  O(max{g1(n), g2(n)}) 8

9 Plan for Analysis of Recursive Algorithms
Decide on a parameter indicating an input’s size. Identify the algorithm’s basic operation. Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated separately.) Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is executed. Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or another method. 9

10 Find the complexity of T(n) = 3T(n-1), T(0) = 1
= = 3nT(n-n) = 3nT(0) = 3n.1 = 3n = O(3n)

11 Find the complexity of following function
int fact (int n) { if(n==0) return 1; else return n * fact (n-1) } Size : 'N' Basic Operation : Multiplication Recurrence Relation : T(n) = T(n-1) + c. c – Time taken for multiplication Base Condition : T(n) = 1, n=1

12 The recurrence relation is :
T(n) = T(n-1) + c = T(n-2) + c + c = T(n-2) + 2c = T(n-3) + c + 2c = T(n-3) +3c = = T(n-n) + nc = T(0) + nc = 1 + nc = O(n)

13 Find the complexity of the following function
void TOH(int n, chat from, char to, char aux) { if(n==1){ printf(“Move disk1 from peg %c to to peg %c”,from,to); return; } TOH(n-1,from,aux,to); printf(“Move disk %d from peg %c to peg %c”,n,from,to); TOH(n-1,aux, to, from);

14 Base Condition : T(1) = 1 Recurrence : T(n) = T(n-1) + T(n-1) + d = 2.T(n-1) + d, for n>1 T(n) = 2T(n-1) + d = 2[2T(n-2) + d] + d = 22T(n-2) + d[ ] = 22 [2T(n-3) + d] + d[ ] = 23T(n-3) + d[ ] = = 2n-1T(n-(n-1)) + d[2n-2 + 2n ] = 2n-1T(1) + d[(2n-1 – 1) / (2-1)] = 2n-1 + d[2n-1 – 1] = O(2n)

15 Find the complexity of the following function:
int Binary(int n) { if(n==1) return 1; else return(Binary(n/2) + 1); } Base Condition : T(n) = 1, n = 1 Recurrence : T(n) = T(n/2) + d. Basic Operation : Addition

16 T(n) = T(n/2) + d = [T(n/4) + d] + d = T(n/4) + 2d = [T(n/8) + d] + 2d = T(n/23) + 3d = [T(n/16) + d] + 3d = T(n/24) + 4d = T(n/2k) + kd Put n = 2k => k = logn = T(2k/2k) + (logn*)d = T(1) + (logn)*d = 1 + (logn)*d = O(logn)

17 Exercise 1 ALGORITHM S(n) //Input: A positive integer n //Output: ???
if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n What does algorith S( ) do? Analyse algorithm S( ) for its performance Write non-recursive equivalent NRS( ) of algorithm S( ) Analyse algorithm NRS( ) for its performance Compare the performances of both.

18 Solution to Exercise 1 a. The recursive algorithm computes the sum of the first n cubes: S(n) = n3. b. Analysis : 1) n, number of terms in the series indicates input’s size 2) Multiplication is the basic operation 3) Setting up of recurrence: M(n) = 0, if n = 1 M(n) = M(n-1) + 2, if n > 1 = [ M(n-2) + 2] + 2 = M(n-2) + 2*2=.... = M(n-i) + i*2= = M(n-(n-1)) + (n-1) *2 = M(1) + (n-1)*2 = (n-1) * 2 ≈ 2n  O (n) 4) It is a linear order complexity algorithm

19 Exercise 2 a. What does this algorithm compute?
ALGORITHM WhoAmI( TREENODE root) //Input: root node of a tree if root = null return 0 else return ( 1 + WhoAmI( Lchild(root) ) + WhoAmI( Rchild(root) ) ) a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it. c. Write non-recursive equivalent of WhoAmI and compare the performances of both.

20 Recurrence : T(n) = T(n/2) + T(n/2) + d
= 2.T(n/2) + d, n>=1 Base Condition : T(n) = 1, n=0 T(n) = 2T(n/2) + d = 2[2T(n/4)+d]+d = 22T(n/4) + d[ ] = 22[2T(n/8)+d] + d[ ] = 23T(n/23) + d[ ] = = 2kT(n/2k) + d[2k-1 + 2k ] Put n=2k => k = logn = 2lognT(2k/2k) + d[2k-1] = n + d[2logn – 1] = n + d[n-1] = O(n)

21 Find the complexity of the following function
T(n) = 2T(n-1) – 1, n>=1 T(n) = 1, n=0 T(n) = 2T(n-1) – 1 = 2[2T(n-2) – 1] -1 = 22T(n-2) – [ ] = = 2nT(n-n) – [2n-1 + 2n-2 + 2n ] = 2nT(0) – [2n – 1] = 2n – 2n + 1 = O(1)

22 Find the complexity of the following function
void function(int n) { if(n==1) return; for(i=1;i<=n;i++) for(j=1;j<=n;j++) printf(“Hello world\n”); function(n-3); } Recurrence : T(n) = 1 , n = 1 T(n) = T(n-3) + n2 , n>=2 Basic Operation : printf(“hello world”);

23 T(n) = T(n-3) + n2 = n2 + T(n-6) + (n-3)2 = n2 + n2 – 6n T(n-6) = 2n2 – 6n T(n-6) = 2n2 – 6n T(n-9) + (n-6)2 = 3n2 – 18n T(n-9) = = n/3 * n2 – 2n(n) + 5(n) +T(0) = n3 / 3 – 2n2 + 5n + 1 = O(n3)

24 Solve the following recurrence
T(n) = 1, if n=1 T(n) = T(n-1) + n(n-1), if n>=2 T(n) = T(n-1) + n(n-1) = [T(n-2) + (n-1)(n-2)] + n(n-1) = T(n-2) + 2(n-1)2 = T(n-3) + 3n2 – 32n + 8 = T(n-4) + 4n2 – 42n + 20 = T(n-4) + 4n(n-4) + 20 = = T(n-(n-1)) + (n-1)*n *(n-(n-1))+K = T(1) + (n-1) * n +K = 1 + (n2 – n) +K = O(n2)

25 Recurrence : T(n) = d, n<=1 T(n) = T(n-1) + T(n-2) + d
Find the complexity of the following function int fib(int n) { if(n==0) return 0; else if(n==1) return 1; else return fib(n-1) + fib(n-2) } Recurrence : T(n) = d, n<=1 T(n) = T(n-1) + T(n-2) + d

26 T(n) = T(n-1) + T(n-2) + d = T(n-2) + T(n-3) + d+T(n-3) + T(n-4) + d +d = T(n-2) + 2.T(n-3) + T(n-4) + 3d = T(n-3) + T(n-4) + d + 2[T(n-4) + T(n-5) + d]+T(n-5) + T(n-6) + d = T(n-3) + 3T(n-4) + 3T(n-5) +T(n-6) + 7d = = T(n-3) + 3T(n-4) + 3T(n-5) +T(n-6) +(23 – 1)d = T(n-(n-3)) + 3T(n-(n-2)) + 3T(n-(n-1))+T(n-n)+d(2n-3 – 1) = T(3) + 3T(2) + 3T(1) + T(0) + d(2n-3 – 1) = T(3) + 3T(2) +3d+d + d(2n-3 – 1) = T(3) +3T(2) +4d + d(2n-3 – 1) = O(2n)

27 Recurrence : T(n) = 3T(n/3) + d, if n>1 T(n) = d, if n<=1
Find the complexity of the following function void function(int n) { if(n<=1) return; for(i=1;i<=3;i++) function(n/3); } Recurrence : T(n) = 3T(n/3) + d, if n>1 T(n) = d, if n<=1

28 T(n) = 3T(n/3) + d = 3[3T(n/9) + d] + d = 32T(n/32) + d[ ] = 32[3T(n/33) + d] + d[ ] = 33T(n/33) + d[ ] = = 3kT(n/3k) + d[3k-1+3k ] Put n = 3k, => k = log3n = 3log3n T(1) + d[(3K-1)/2) = n + d[(n-1)/2] =O(n)

29 Find the time complexity of the following function
void function(int n) { if(n<=1) return; for(i=1;i<=3;i++) function(n-1); } Recurrence : T(n) = d, if n<=1 T(n) = 3T(n-1) + d, otherwise

30 T(n) = 3T(n-1) + d = 3[3T(n-2) + d] + d = 32T(n-2) + d[31+30] = 32[3T(n-3) + d] + d[31+30] = 33T(n-3) + d[ ] = = 3nT(n-n) + d[3n-1 + 3n ] = 3nT(0) + d[(3n – 1) /2] = 3n.d + d[(3n – 1) /2] = O(3n)

31 Recurrence : T(n) = n + T(0.8n), if n>1 T(n) = 1, if n<=1
Find the complexity of the following function void function(int n) { if(n<=1) return; for(i=1;i<=n;i++) printf(“Hello world”); Function(0.8n); } Recurrence : T(n) = n + T(0.8n), if n>1 T(n) = 1, if n<=1

32 T(n) = n + T(0.8n) = T(4/5n) + n = T[(4/5)2 * n] + (4/5) + n = T[(4/5)3 * n] + (4/5)2 + (4/5) + n = = T[(4/5)n * n] + (4/5)n-1 + (4/5)n (4/5) + n = T[0*n] + (4/5)/(1-0.8) + n = T(0) + K + n = 1 + K + n = O(n)

33 Find the time complexity of the following recurrence:
T(n) = 2T(n/2) + n, n>1 T(0) = T(1) = 1

34 T(n) = 2T(n/2) + n = 2[2T(n/22) + n/2] + n = 22T(2/22) + 2n = 22[2T(2/23) + n/4] + 2n = 23 T(2/23) + 3n = = 2kT(n/2k) + kn Put n = 2k => k = logn = 2lognT(1) + logn* n = n + nlogn = O(nlogn)

35 Find the complexity of the following function
void function(int n) { if(n<2) return; else counter = 0; for(i=0;i<8;i++) function(n/2); for(i=1;i<=n3;i++) Counter++; } Recurrence : T(n) = 1, if n<2 T(n) = 8T(n/2) + n3 + 1, otherwise

36 T(n) = 8T(n/2) + n3 + 1 = 8[8T(n/4) + (n/2)3 + 1] + n3 + 1 = 82 T(n/4) + 2n3 + [ ] = 83 T(n/23) + 3n3 + [ ] = = 8kT(n/2k) + kn3 + [8k-1+8k ] Put n = 2k , k = logn = 8lognT(1) + n3 * logn + [8logn – 1] /7 = nlog8 + n3 * logn + [nlog8 – 1] / 7 = n3 + n3 * logn + [nlog8 – 1] / 7 = O(n3 * logn)

37 Generate all strings of length n drawn from 0......k-1
char a[100]; void k_string(int n, int k) { if(n<1) printf(“%s”,a); else for(j=0;j<k;j++) a[n-1]=j; k_string(n-1,k); } Recurrence : T(n) = c, if n<0 T(n) = kT(n-1) + d, otherwise

38 T(n) = K T(n-1) + d = K[K.T(n-2) + d] +d = K2 T(n-2) + d[K1 + K0] = K2[K.T(n-3) + d] + d[K1 + K0] = K3T(n-3)+(K2+K1+K0)d = = Kn .T(n-n) + (Kn-1 + Kn K1 +K0)d = Kn.T(0) + (Kn-1) / (K-1) = Kn + (Kn-1) / (K-1) = O(Kn)

39 Find the time complexity of the following function
void Binary(int n) { if(n<1) printf(“%s”,A); //Assume A is global else A[n-1] = 0; Binary(n-1); A[n-1] = 1; } Recurrence : T(n)=c, if n<1 T(n) = 2T(n-1) + d, otherwise

40 T(n) = 2T(n-1) + d = 2[2T(n-2) +d] + d = 22T(n-2) + 3d = 22[2T(n-3) + d] + 3d = 23T(n-3) + d[ ] = = 2n.T(n-n) + d[2n-1 + 2n ] = 2nT(0) + d[2n – 1] = 2n*c + d[2n – 1] = O(2n)


Download ppt "Amortised Analysis."

Similar presentations


Ads by Google