


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































What is an Algorithm?
Four Research Areas of study in Algorithms :
1. Design of Algorithms : Algorithmic strategies ( D&C, Greedy, Dynamic Programming, Backtracking, Branch & Bound, Approximation Algorithms, Randomized algorithms, Parallel Algorithms etc). 2. Validation of Algorithms : Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs. Proof techniques (Contradiction, Mathematical Induction etc.) 3. Analysis of Algorithms : Computing Time and Storage requirements for an algorithm 4. Testing of Programs : Software Testing, Test Cases Design and Debugging
Algorithm (or Program) Specification : Example
Performance Analysis of an Algorithm : There are many criteria upon which we can judge an algorithm. For example :
Performance Evaluation : Asymptotic analysis Priori Estimates : For any instruction in a program: Total Computation Time = Time required to execute the instruction * Number of times the instruction is executed in a program (i.e. frequency count ). Time required to execute the instruction depends upon:
All above criteria differ from installation to installation. Therefore for priori estimates frequency count of each statement is most important factor while analyzing an algorithm or program. Frequency count : It is the number of times an instruction is executed in the execution of a program. Example
Performance Evaluation : Asymptotic analysis Priori Estimates : Example contd… Frequency Count of each statement: Statement # 1 : n + 1 Statement # 2 : n2 /2 + 3n/ Statement # 3 : n3/6 + n2 + 5n/ Statement # 3 : n3/6 + n2/2+ n/ Total Frequency Count = (n 3 +6n2 +11n +3)/3 = O(n 3 )
Performance Evaluation : Asymptotic analysis Asymptotic Notations : To enable us to make meaningful (but inexact) statements about the time and space complexities of an algorithm , asymptotic notations (O, o, Ω, ω, θ) are used. Big “Oh” : The function f(n) = O(g(n)), to be read as “ f of n is Big Oh of g of n”, if and only if there exist positive constants c and n0 such that, f(n) ≤ c*g(n) for all n≥n0, for example, i. f(n) = 3n + 2 ≤ 4n for all n ≥ 2. Here c = 4, g(n) = n and n0 = 2. ii. f(n) = (n3 + 6n2 + 11n +3) ≤ 2n3 for all n ≥ 8. Here c = 2, g(n) = n3 and n0 = 8. Thus f(n) = O(g(n)) states that g(n) is an upper bound on the value of f(n) for all n≥n0.
Performance Evaluation : Asymptotic analysis Theta ( θ ): The function f(n) = θ(g(n)), to be read as “ f of n is theta of g of n, if and only if there exist positive constants c1 , c2 and n0 such that, c1g(n) ≤ f(n) ≤ c2*g(n) for all n≥n0, for example, i. f(n) = 3n + 2 then 3n ≤ 3n + 2 ≤ 4n for all n≥ Here c1 = 3, c2 = 4, g(n) = n and n0 = 2. ii. f(n) = (n3 + 6n2 + 11n +3) then n3 ≤ (n3 + 6n2 + 11n +3) ≤ 2n3 for all n ≥ 8. Here c1 = 1, c2 = 2, g(n) = n3 and n0 = 8. Thus f(n) = θ(g(n)) states that g(n) is both upper & lower bound on the value of f(n) for all n≥n0.
Performance Evaluation : Asymptotic analysis Little “oh” : The function f(n) = o(g(n)), to be read as “ f of n is little oh of g of n, if and only if Lim f(n) / g(n) = 0 n → ∞ i. f(n) = 3n + 2 = o(n2) or o(nlogn) ii. f(n) = (n3 + 6n2 + 11n +3) = o(n4) or o(n3logn) Little omega : The function f(n) = ω(g(n)), to be read as “ f of n is little omega of g of n, if and only if Lim g(n) / f(n) = 0 n → ∞ i. f(n) = 3n + 2 = ω(1) ii. f(n) = (n3 + 6n2 + 11n +3) = ω(n2)
Amortized Analysis : Worst case analysis of an algorithm is sometimes pessimistic. For example let us consider the following procedure for adding 1 to counter. Let array C[1:m] represent binary number = Σ 2m-j C[j] for example [0,1,1,0,1,1] = 27 1≤ j ≤ m Algorithm count (C[1…m]) // assumption is m≥1 & C[j] = {0, 1} for 1≤j≤m { j = m + 1; repeat { j = j – 1; C[ j ] = 1 - C[ j ]; } until (C[ j ] = 1) or ( j = 1); }
Amortized Analysis : In worst case a barometric instruction j = j – 1 will be executed ‘m’ times and if we call the procedure ‘n’ times ( i.e. add 1 to count ‘n’ times) it will be executed nlog n times. Thus the worst case time complexity for ‘n’ calls = O(nlogn) but we can prove that it is O(n). 00 number of calls 01 1 10 2 11 1 100 3 Total 7 For n = 4, total calls = 7 = 24 - 1 For n = 8, total calls = 15 = 2*8 - For n = n, total calls = 2n – 1 Time Complexity = O(n)
Performance Evaluation : Performance Measurement : (Posteriori Testing) : Performance measurement is concerned with obtaining the space and time requirements for a particular algorithm. Purpose of posteriori testing may be to work out some empirical formula by executing the program for different sizes of n, which can be used to estimate the runtime for a given problem size, or the purpose may be to compare the two algorithms for their runtime. For example, for sequential search the runtime may be given by the empirical formula t = 0.002 + 0.0003067n, where t is the time in milliseconds and n is size of array. Time and Space tradeoffs in algorithms : In general, while solving the problem, for any algorithm, computation time required will be more when space required is less and vice-a-versa. Example
Best, Worst and Average case analysis of an algorithm: Best Case analysis : Best case is that input to the algorithm which takes minimum time for execution of it. Best case analysis of an algorithm is the asymptotic analysis of an algorithm for best case input. Examples :