Algorithm Analysis
Algorithm Analysis
Algorithm Analysis
Algorithm Analysis
Three Parts,
Designing the algorithm Proving the correctness of the algorithm Analysis the algorithm
Method 1
int a1[n]; int a2[n]; for(int i=0;i<n;i++) { a2[i]=a1[(n-1)-i]; }
Analysis:
Size of the input array is n Size of the output array is n So the total space requirement is n+n=2n Time complexity is based on the number of assignment statements in the algorithm.
So the time complexity is n
Method 2
int a1[n]; int k=floor(n/2); for( int i=0; i<k; i++) { swap(&a1[i],&a1[(n-1)-i]); } swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
Method 2 Contd
Analysis Space Complexity
Size of the input array is n One temporary variable called temp So total space requirement is n+1
Time Complexity
Three assignments per swapping Totally n/2 swapping So time complexity is 3n/2.
Asymptotic Notations
Three standard notations are used,
Big Oh notaion (O) Big Omega notation (;) Big Theta notation (5
Big Oh notation
Big Oh notation: (Asymptotic upper bound)
f (n) = O (g (n)) If f (n) <= c*g (n) for all n n0, where c & n0 are constants > 0 O (g (n)): class of functions f (n) that grow no faster than g (n)
c*g(n) f(n)
n0
Example: T(n) = 2n + 5 is O(n). Why? 2n+5 <= 3n, for all n >= 5 T(n) = 5*n2 + 3*n + 15 is O(n2). Why? 5*n2 + 3*n + 15 <= 6*n2, for all n >= 6
n0
Example: T(n) = 2n + 5 is ;(n). Why? 2n+5 >= 2n, for all n > 0 T(n) = 5*n2 - 3*n is ;(n2). Why? 5*n2 - 3*n >= 4*n2, for all n >= 4
n0
Example: T(n) = 2n + 5 is 5(n). Why? 2n <= 2n+5 <= 3n, for all n >= 5 T(n) = 5*n2 - 3*n is 5(n2). Why? 4*n2 <= 5*n2 - 3*n <= 5*n2, for all n >= 4
Recurrence Relation
Types, Homogeneous Non Homogeneous Let f(n) is a time complexity of our algorithm for the input of size n. Then f(n) is recursively defined as,
Recurrence Relation
Solving Homogeneous Linear Recurrence Relation:
i. Write down the characteristic equation for the recurrence relation,
iv.
v. vi.
If a1 = a2 and others are distinct, then the general solution is, S(k) = (b1+b2k)ak + b3a3k+ + bnank Substitute the initial condition in the general solution and find b1, b2, bn Substitute b1, b2, bn in the general solution to get the required solution.
Example: 1) Solve the recurrence relation S(k) + S(k-1) - 6S(k-2) = 0 and S(0) = -1, S(1)=8. 2) Solve the recurrence relation S(k) 8S(k-1) + 16S(k-2) = 0 initial conditions are, S(2) = 16 & S(3) = 80 3) Solve the recurrence relation S(k) 5S(k-1) + 6S(k-2) = 0, k 2 with initial conditions S(0) = 1, S(1) = 3
iii. Step 2
Obtain a particular solution of the recurrence relation by taking an educational guess at the form of particular solution.
Form of a particular solution 1. Constant q 2.Linear function of q and k q0 + q1k 3. An nth order polynomial q0 + q1k + q2k2 + + qnkn 4. An exponential function qak
Particular solution A constant d A linear form of d0 + d1k An nth degree polynomial d0 + d1k + d2k2 + + dnkn An exponential function dak
iv. Step 3 Substitute the guess from step 2 into the recurrence relation. If the guess is correct, it should help us to determine the unknown co efficient. If it is wrong, it should be apparent from the result of the substitution. Go to the step 2. v. Step 4 The general solution of the recurrence relation is the sum of homogeneous and particular solution. If no initial conditions are given, then the solution gets over. If n initial conditions are given, obtain n linear equations and solve the linear equations if possible to get required solution.
Example 1. Solve S(k) S(k-1) 6S(k-2) = -30 with initial conditions S(0) = 20, S(1) = -5 2. Solve S(k) 2S(k-1) + S(k-2) = 2 with initial conditions S(0) = 25, S(1) = 16 3. Solve the recurrence relation S(k) 3S(k-1) 4S(k-2) = 4k