Complexity of an algorithm CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 1 Sometimes, there are more than one way to solve a problem. We need to learn how to compare the performance of different algorithms and choose the best one to solve a particular problem. While analyzing an algorithm, we mostly consider time complexity and space complexity. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input. Time & space complexity depends on lots of things like hardware, operating system, processors, etc. However, we don't consider any of these factors while analyzing the algorithm. We will only consider the execution time of an algorithm.
Unit 1 : Calculating Time Complexity CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 2 Q: What are the number of elements, If first element is at 1 and last element is at 10 ? Q: What are the no. of elements, if first element is at 527 and last element is at 9346 ? Length of array : UB – LB + 1
Unit 1 : Time space trade off Calculate Time Complexity of Algorithm Time Complexity is most commonly estimated by counting the number of elementary functions performed by the algorithm. Since the algorithm's performance may vary with different types of input data, Hence for an algorithm we usually use the worst-case Time complexity of an algorithm because that is the maximum time taken for any input size. CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 3
Unit 1 : Time space trade off Execution Time Cases There are three cases which are usually used to compare various data structure's execution time in a relative manner. Worst Case − This is the scenario where a particular data structure operation takes maximum time it can take. If an operation's worst case time is ƒ(n) then this operation will not take more than ƒ(n) time where ƒ(n) represents function of n. Average Case − This is the scenario depicting the average execution time of an operation of a data structure. If an operation takes ƒ(n) time in execution, then m operations will take mƒ (n) time. Best Case − This is the scenario depicting the least possible execution time of an operation of a data structure. If an operation takes ƒ(n) time in execution, then the actual operation may take time as the random number which would be maximum as ƒ(n). CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 4
Case of : Insertion sort & related questions CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 5 Q1: What are the number of comparison required to insert 40? // fig : 1 Hint : j = 4 , i = 3 Answer : 2 Q2: How many times the while loop execute? Answer : 2 // for i = 3 condition is true // for i =2 condition is false Q3 : What are the number of comparison required to insert 25 ? // fig :2 Hint : j = 5, i = 4 Answer : 4 Q4: How many times the while loop execute? Answer: 4 //for i =4,3,2 condition is true and for i = 1 condition is false Q5 : What are the number of comparison required to insert 10 ? //fig : 2 Hint : j = 5, i =4 Answer : 4 Q6: How many times the while loop execute? Answer : 5 // for i = 4,3,2,1 condition is true & for i =0 condition is false 20 30 50 1 2 3 20 30 40 50 1 2 3 4 Algorithm IS ( a,n ) { for ( j=2 to n) { // j -> the position of element which we want to insert in sorted // i -> the no. of elements already in sorted array // i -> max no of comparisons Key = a[j] ; i = j – 1; while ( i > 0 && key < a[ i ]) { Shift a[ i ] to +1 position & decrement i ; } Place key at its correct position } }
Complexity : Insertion sort CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 6 Algorithm IS ( a,n ) { Line 1. // this algo will sort the elements of array a[], having n elements Line 2. for ( j=2 to n) { Line 3. Key = a[j] ; Line 4. i = j – 1; Line 5. while ( i > 0 && key <a[ i ]) { Line 6. a[i+1] = a[ i ]; Line 7. i = i -1 ; } Line 8. a[i+1] = key; } } 1. Comment Line No How to find complexity Time taken 2 No of time the for loop execute (N-2+1) + 1= n-1+1=n 3. Loop -1 N-1 4. Loop -1 N-1 5. While loop depends on comparison of key, key is dependent on J, i.e a2, a3, a4, a5 also loop depend on I, ie no of sorted elements, so loop vary from 2 to n . We assume that the while loop execute times , where j is from 2 to 5 ie . T 2 + T 3 + T 4 +T 5 5. 6. While Loop -1 6. While Loop -1 7. While Loop -1 7. While Loop -1 8. For loop -1 N-1 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn : 1
Analysis of Insertion Sort Elements are in increasing order Case 1 : When the elements are in increasing order // t j => is the number of times the while loop execute j =2 , key 30, i =1 While(1>0 & key < a1) // condition false => t 2 = 1 J=3, key = 40, i = 2 While(2>0 & key< a2) // condition false => t 3 = 1 J=4, key = 50, i =3 While (3>0 & key <a3) // condition false => t 4 = 1 J=5, key =60, I = 4 While(4>0 & key <a4) // condition false => t 5 = 1 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 7 20 30 40 50 60 1 2 3 4 5 Algorithm IS ( a,n ) { // this algo will sort the elements of array a[], having n elements for ( j=2 to n) { Key = a[j] ; i = j – 1; while ( i > 0 && key < a[ i ]) { a[i+1] = a[ i ]; i = i -1 ; } a[i+1] = key; } }
Analysis of Insertion Sort Elements are in increasing order = 1 + 1 + 1 + 1 …………(n-2+1) = 1 + 1 + …….(n-1) = n-1 // (n-1) – (n-1)= 0 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 8 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn : 1 T(n) = n + (n-1) + (n-1) + (n-1) + 0 + 0 + (n-1) T(n) = An + B where A and B are constants T(n) = Ɵ (n) // theta(n) Complexity when elements are in increasing order
Analysis of Insertion Sort Elements are in decreasing order Case 2 : When the elements are in decreasing order // t j => is the number of times the while loop execute j =2 , key 50, i =1 While(1>0 & key < a1) // condition true Shift 60 to second position, decrement i While (0>0 // condition false But again while loop execute, hence we can say that for j= 2 while loop execute twice J=3, key = 40, i = 2 While(2>0 & key< a2) // condition true Shift 60 to 3 rd position and decrement i While ( 1>0 & key <a1) //condition true Shift 50 to 2 nd position, decrement i While( 0 > 0 // condition false Hence for j= 3 while loop executes thrice Similarly for j=4 while loop executes 4 times And so on CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 9 60 50 40 30 20 1 2 3 4 5 Algorithm IS ( a,n ) { // this algo will sort the elements of array a[], having n elements for ( j=2 to n) { Key = a[j] ; i = j – 1; while ( i > 0 && key < a[ i ]) { a[i+1] = a[ i ]; i = i -1 ; } a[i+1] = key; } } 50 60 1 2 40 50 60 1 2 3
Analysis of Insertion Sort Elements are in decreasing order = 2 +3 + 4 + ………… + n = { n(n+1)/2 } - 1 // sum of n natural numbers is n(n+1)/2 [{ n(n+1)/2 } - 1 ] – (n-1) = n(n-1)/2 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 10 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn : 1 T(n) = n + (n-1) + (n-1) + [{n(n+1)/2} – 1] + n (n-1)/2 + n(n-1)/2 + (n-1) T(n) = An 2 + Bn +C where A,B and C are constants T(n) = O (n 2 ) // Big-oh(n 2 ) Complexity when elements are in decreasing order
Complexity of Insertion sort T(n) = O (n 2 ) // Big-oh(n 2 ) Complexity when elements are in decreasing order CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 11 Total complexity T(n) = n + (n-1) + (n-1) + + + + n-1 T(n) = O (n 2 ) // Big-oh(n 2 ) Complexity when elements are in decreasing order Worst Case T(n) = Ɵ (n) // theta(n) Complexity when elements are in increasing order Best Case General Equation for Insertion Sort