









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Nice well experienced teacher notes
Typology: Lecture notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










The choice of an algorithm depends upon analysis:
e.g. int func(int a) { retur n a; } 2-bytes to store variable 2-bytes required to return value 4-bytes memory is required to complete execution. This 4-byte of memory is fixed for any input value of a. So space complexity is said to be constant (i.e. O(1) ) Another e.g. int sum(int a[ ],int n) { int s=0,i; for(i=0;i<n;i++) s+=a[i];
Suppose a loop will run n number of times, so the time complexity will be n atleast and the value of n will increase the time taken will also increase. Since the algorithm performance may vary with different types of input data hence we use worst- case time complexity of an algorithm because that is the maximum time taken for any input size. Calculating Time complexity : The most common metric for calculating time complexity is Big-O notation. This removes all constant factors , so that the running time can be estimated in relation to n, as n approaches infinity. e.g. Statement; only single statement , its time complexity will be constant. The running time of the statement will not change in relation to n. e.g. for(i=0;i<n;i++) { statement; } The time complexity will be linear. The running time of loop is directly proportional to n. Asymptotic notation: Asymptotic notations are the mathematical notations used to describe the running time of an algorithm. To analyze time complexity of an algorithm , we rely on notations. There are three asymptotic notations:
Big-O notation: It is used to define the upper bound of an algorithm in terms of time complexity. It always indicates the maximum time required by an algorithm for all input values. So Big-O describes the worst case of an algorithm time complexity. It is represented as shown below: If f(n) and g(n) are two functions defined as positive integers, then f(n) = O(g(n)) as f(n) is big-O of g(n) or f(n) is on the order of g(n) if there exist constant C such that f(n)<=C.g(n). This implies that f(n) does not grow faster than g(n) or g(n) is the upper bound on the function f(n). We are calculating the growth rate of the function which eventually calculates the worst time complexity of a function i.e. how worst an algorithm can perform. e.g. f(n)=2n+3, g(n)=n we have to find f(n)=O(g(n)) It must satisfy following condition f(n)<=C.g(n) So replace f(n) with 2n+3 and g(n) with n 1). 2n+3 <=C.n lets assume C=5 and n= 21+3<=51 for n=1 condition is true C.g(n ) f(n) k
e.g. If f(n)=2n+3 and g(n)=n Is f(n)= Ωg(n)? It must satisfy the condition f(n)>=C.g(n) 2n+3>=C.n Suppose C=1 then 2n+3 >=n (this is true for any value of n starting from 1) therefor it is proved that g(n) is big omega of 2n+3 function. g(n) function is the lower bound of f(n), when C=1. So the notation fastest running time. Theta Notation (θ): The notation describes the average case scenario. It represents the realistic time complexity of an algorithm. Every time an algorithm does not perform worst or best , algorithms mainly fluctuate between worst and best case and this gives average case of an algorithm. It is a formal way to express both the upper and lower bound of an algorithm running time. If f(n) and g(n) are two functions defined as positive integers, then f(n)= θ (g(n)). The condition is satisfied only if C1.g(n) <=f(n) <= C2.g(n) The function is bounded by two limits i.e. upper and lower limit and f(n) comes in between C1.g( n) f(n) Time Input C2.g( n)
Commonly used functions:
1. Constant function f(n)=1: whatever the size of input , these function takes constant time. 2. Linear function f(n)=n: These functions grow linearly with input size n. 3. Quadratic function f(n)=n^2 : grow faster f(n)=n^3. (non linear complexity) 4. Logarithm function f(n)=log(n): These functions are slower growing than linear (It reduces the size of input data in each step, this indicates that the number of operations is not the same as input size e.g. Binary search). Time required by an algorithm falls under three cases: 5. Best case : It is the minimum time required for program execution. We calculate the lower bound on running time of an algorithm. It bounds the function below i.e. we must know the cases causes minimum number of operations to be executed. e.g. In linear search best case occurs when element found at first position , so time complexity is O(1). The number of operations are fixed. Linear Search: for(i=0;i<n;i++) { if(a[i]==item) return a[i]; } 2. Worst-case analysis : It is the maximum time required for program execution. We calculate the upper bound on running time of an algorithm. It bounds the function from above. We must know the case that causes a
Primitive Data Structure Non-Primitive Data Structure