Lecture 5 Analysis of Running time of algorithms
- Slides: 17
Lecture 5. Analysis of Running time of algorithms (Best, Average and Worst cases) 1
Recap RAM model is used to measure the run time of an algorithm by counting the number of steps. l Space complexity of an algorithm refer to the amount of memory required to run. l Time complexity of an algorithm refer to its running time, which depends on input size. l Primitive operations refer to the unit of operation that can be identified in the pseudo-code l 2
Counting Primitive Operations Algorithm Array. Max(A, n) {An array A storing N integers and find the largest element in A. } n-1 times current. Max = A[0] 2 steps + 1 to initialize i for (i=1; i<=n-1; i++) 2 step each time (compare i to n, inc i) if (current. Max < A[i]) 2 steps How often done? ? current. Max = A[i] 2 steps return current. Max 1 step Between 4(n-1) and 6(n-1) in the loop 3 It depends on the order the numbers appear in in A[]
Run Time Analysis l Pseudo code to find product of two numbers int method() { Run Time Complexity will be = int a, b, c; ----- c 1+c 2 + c 3 + c 4 a=2; ----- c 2 = c 1 + 2 c 2 + c 3 + c 4 (as all constant) b=3; ----- c 2 =C c= a*b; ----- c 3 printf(“Product of a and b is %d”, c); return 0; ------ c 4 } 4
Run Time Analysis (Cont !!!) 5 int method() { int a, b, large; ----- c 1 Run Time Complexity will be = a=2; ----- c 2 c 1+c 2 + c 3 +c 2 + c 4 b=3; ----- c 2 = c 1 + 4 c 2 + c 3 +c 4 (as all constants) if(a>b) ---- c 3 =C large=a; ---- c 2 Note: - =You can say there should be else 3 c 2 instead of 4 c 2 (Coefficient have large = b; ------- c 2 no impact) printf(“Large number is %d”, large); Return 0; ----- c 4
Run Time Analysis (Cont !!!) 6 int method() { int I, codd, ceven, a[5]={5, 4, 3, 2, 1}; ----- c 1 ceven=0; codd=0; ------ c 2 Run Time Complexity will be = c 1+c 2 for(i=0; i<=4; i++) ------ c 3 + n* (c 3 +c 4 + c 5)+ c 6 if(a[i]%2==0) ---- c 4 = c 1 + c 2 + n*c +c 6 ceven++; ---- c 5 = n*c + c else =n codd++; ------- c 5 printf(“Total evens %d and Total Odd %d”, ceven, codd); Return 0; ----- c 6
Run Time Analysis (Cont !!!) N N N 7 int method() { int I, a[5]; ----- c 1 for(i=0; i<=4; i++) ------ c 2 Scanf(“%d”, &a[i]); for(i=0; i<=4; i++) A[i]=a[i]+5; ------ c 3 for(i=0; i<=4; i++) print(“%d”, a[i]); return 0; ----- c 4 } Run Time Complexity will be = c 1 + n * c 2 + n*(c 1+c 3) + n * c 2 + c 4 = c 1 + 2 n*c 2 + n * c = c 1 + n * c = 2 n*c +c 1 = 2 n =n
Run Time Analysis (Cont !!!) int method() Note: { • C 3 refer to array scripts use int r, c, a[][]= { {1, 2}, {1, 3}}; ----- c 1 • C 4 refer to arithmetic op + • C 5 refer to assignment for(c=0; c<=1; c++) ------ c 2 for(r=0; r<=1; r++) N*m=n 2 a[r][c]=a[r][c]+5; ------ c 3+c 4+c 5 Run Time Complexity will be = c 1 + n * m * for(c=0; c<=1; c++) N*m=n 2 (c 2+c 3+c 4+c 5) + n * m * c 4 + c 6 for(r=0; r<=1; r++) = c 1 +c 6 + n*m*c + printf(“%d”, a[r][c]); = 2*n*m*c + c return 0; ----- c 6 = n*m*c = n*m = n 2 } 8
Run Time Analysis (Cont !!!) N times N*m=n 2 9 int method() Note: { • C 4 refer to array scripts use int r, c, , s, a[][]= { {1, 2}, {1, 3}}; ----- c 1 • C 5 refer to arithmetic op + • C 2 refer to assignment S=0; ------- c 2 for(c=0; c<=1; c++) ------ c 3 Run Time Complexity will be = c 1 + n * { for(r=0; r<=1; r++) (c 2+c 3+c 4+c 5) + n (c 2+c 3+c 4+c 5 )+c 6 a[r][c]=a[r][c]+5; ---- c 4+c 5 +c 2 = c 1+c 6 + n*m*c + n*c s=s+a[r][0]; ------- c 4+c 5 +c 2 = n*m + n + c = n 2 + n } Printf(“Sum of element of 1 st row %d”, s); return 0; ----- c 6
Types of Algorithm Complexity l Worst Case Complexity: – l Best Case Complexity: – l the function defined by the minimum number of steps taken on any instance of size n Average Case Complexity: – 10 the function defined by the maximum number of steps taken on any instance of size n the function defined by the average number of steps taken on any instance of size n
Types of Algorithm Complexity (Example: Linear Search) 5 l l l 11 7 2 6 9 12 Worst Case Complexity: – You want to search 1 in above array which is at location N – You need N steps Best Case Complexity: – You want to search 5 in above array which is at location 1 – You need 1 steps Average Case Complexity: – You want to search 2 or 9 etc in above array – You need 3 steps for 2 and 5 steps for 9 1
Best, Worst, and Average Case Complexity Number of steps Worst Case Complexity Average Case Complexity Best Case Complexity N (input size) 12
Relationship between complexity types and running time of Algorithms l l l Worst case – Provides an upper bound on running time – An absolute guarantee that the algorithm would not run longer, no matter what the inputs are Best case – Provides a lower bound on running time – Input is the one for which the algorithm runs the fastest Average case – Provides a prediction about the running time – Assumes that the input is random 13
Running Time l l Most algorithms transform input objects into output objects. The running time of an algorithm typically grows with the input size. Average case time is often difficult to determine. We focus on the worst case running time. – – Easier to analyze Crucial to applications such as games, finance and robotics 14
Home Work l Exercise-1. – l Exercise-2. – 15 Write a pseudo code which find the sum of two 3*3 matrics and then calculate its running time. Write a pseudo code which read a number N and print whether it is prime or not. After that, calculate the run time complexity
Summary Number of steps you need to find the complexity of an algorithm l Run time complexity vary due to use fo different constructs such as simple, nested and consecutive loop, selection and assignment statement. l Types of algorithms complexity are best, average and worst which also depend on number of steps or comparisons l 16
In Next Lecturer l 17 In next lecture, we will talk about the asymptotic analysis fro comparisons of algorithms.
- Once upon a time there was a house with a man,
- Running running running
- Analysis of algorithms lecture notes
- Introduction to algorithms lecture notes
- Running time analysis
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Undecidable problems and unreasonable time algorithms.
- Time complexity of algorithms
- Undecidable problems and unreasonable time algorithms
- Time sort
- Expected running time
- Recurrence relation cheat sheet
- Running time
- Running time adalah
- O(n^2) time complexity
- Magentasum.com
- Its time to start running
- 1001 design