Download presentation

Presentation is loading. Please wait.

Published byAlvaro Beecroft Modified over 2 years ago

1
Complexity Analysis (Part II ) Asymptotic Analysis. Big-O Notation: More Details. Order Arithmetic Rules. Big-O Computation: Improved Guide lines.

2
Asymptotic Analysis. The Big-O notation is typically written as: f(n) = O(g(n)). It reads: “f(n) is of the order of g(n)”or “f(n) is proportional to g(n)”. If f(n) = O(g(n)), then there exists c and n o such that: f(n) n 0 The above equation states that as n increases, the algorithm complexity grows NO faster than a constant multiple ofg(n). n0n0 Input size Require Time

3
Big-O Notation: Some Details Let's consider: f(n) = 5n + 3. A possible bounding function is: g(n) = n 4. 5n+3 = O(n 4 ) However, it is expected that the function g(n) should be of order (highest power) as small as possible. 5n+3 = O(n 3 ) 5n+3 = O(n 2 )

4
Big-O Notation: Some Examples Consider the following complexity function f(n) = 100 n. Then, choosing: c = 100, n o = 0 and g(n) = n will satisfy: f(n) < 100n Then, if we have the complexity f(n) = 5n + 3.In this case, the following choice: c = 5, n o = 0 and g(n) = n will satisfy: f(n) < 5n Let assume now that the complexity is: f(n) = 3n2 - 100. The choice of: c = 3, n o = 0 and g(n) = n2 is possible to have: f(n) < 3n 2

5
Big-O Notation: Simple Rule Simple Rule: Drop out lower terms, constant terms and constant factors. SOME EXAMPLE If f(n) = 100 n, then:g(n) = n or 100 n is O(n). If f(n) = 5 n + 3, then:g(n) = n. Or 5 n + 3 is O(n). If f(n) = 8 n2 log n + 5 n2 + n, then: g(n) = n2 log n. Or 8 n2 log n + 5 n2 + n is O( n2 log n).

6
Big-O Notation: Limitation The big-O notation has some problems. Let’s assume that we have the following complexity function: We can find so many g(n) limits that can be considered as a valid bound of the complexity f(n). So one cannot decide which g(n) will be the best for the algorithm at hand. Different choices of c and n o are possible.

7
Big-O Notation: Arithmetic Rule Multiplicative constants:O(k*f(n)) = O(f(n)) Addition rule: O(f(n)+g(n)) = O(max[f(n),g(n)]) Multiplication rule: O(f(n)*g(n)) = O(f(n)) * O(g(n)) SOME EXAMPLE O(1000n) = O(n).Use multiplicative constants rule. O(n2+3n+2) = O(n2).Use addition rule. O((n3-1) (n log n + n + 2)) = O(n4log n).How??..

8
Computing Big-O Notation: Guideline Loops such as for, while, and do-while: –The number of operations is equal to the number of iterations (e.g., n) times all the statements inside the for loop. Nested loops: –The number of statements in all the loops times the product of the sizes of all the loops. Consecutive statements: –Use the addition rule of order arithmetic: O(f(n)+g(n))=(max[f(n), g(n)]). if/else and if/else if statement: –The number of operations is equal to running time of the condition evaluation and the maximum of running time of the if and else clauses. So, the complexity is: O(Cond) + O(max[if, else])

9
Computing Big-O Notation: Guideline (Contd.) switch statements: Take the complexity of the most expensive case (with the highest number of operations). Methods call: First, evaluate the complexity of the method being called. Recursive methods: If it is a simple recursion, convert it to a for loop.

10
Computing Big-O Notation: Guideline (Contd.) Let’s look at the for loop case: 1 for(int I = 0; I < n; i++) 2sum += A[i]; 1 + (n + 1) + 2n + n = 4 n + 2 = O(n)

11
Computing Big-O Notation: Guideline (Contd.) Now Let’s consider an example of nested loops: 1 for(int I = 0; I < n; i++) 2for(int j = 0; j < n; j++) 3 sum+= B[i][j]; 4n 2 + 4n +2 = O(n 2 )

12
Computing Big-O Notation: Guideline (Contd.) An example of consecutive statements case is shown below: 1 for(int i = 0; i < n; i++) 2 a[i] = 0; 3 for(int i = 0; i < n; i++) 4 for(int j = 0; j < n; j++) { 5 sum = i + j; 6 size += 1; 7} // End of inner loop. O(n +n 2 ) = O(n 2 )

13
Computing Big-O Notation: Guideline (Contd.) An example of switch statements: 1 char key; 2 int[] X = new int[5]; 3 int[][] Y = new int[10][10]; 4........ 5 switch(key) { 6 case 'a': 7 for(int i = 0; i < X.length; i++) 8 sum += X[i]; 9 break; 10 case 'b': 11 for(int i = 0; i < Y.length; j++) 12 for(int j = 0; j < Y[0].length; j++) 13 sum += Y[i][j]; 14 break; 15 } // End of switch block o(n) o(n 2 )

14
Computing Big-O Notation: Guideline (Contd.) Look at the case of selection block (if-elseif) shown below: 1 char key; 2 int[][] A = new int[5][5]; 3 int[][] B = new int[5][5]; 4 int[][] C = new int[5][5]; 5........ 6 if(key == '+') { 7 for(int i = 0; i < n; i++) 8 for(int j = 0; j < n; j++) 9 C[i][j] = A[i][j] + B[i][j]; 10 } // End of if block 11 else if(key == 'x') 12 C = matrixMult(A, B); 13 else 14 System.out.println("Error! Enter '+' or 'x'!"); O(n 2 ) O(n 3 ) O(n) O(n 3 )

15
Sometimes ifelse statements must carefully checked: O(ifelse) = O(Condition)+ Max[O(if), O(else)] 1 int[] integers = new int[10]; 2........ 3 if(hasPrimes(integers) == true) 4 integers[0] = 20; 5 else 6integers[0] = -20; 1 public boolean hasPrimes(int[] arr) { 2 for(int i = 0; i < arr.length; i++) 3.......... 4.......... 5.......... 6 } // End of hasPrimes() O(1) O(ifelse) = O(Condition) = O(n)

16
Drill Questions 1.Consider the function f(n) = 3 n 2 - n + 4. Show that: f(n) = O(n 2 ). 2.Consider the functions f(n) = 3 n 2 - n + 4 and g(n) = n log n + 5. Show that: f(n) + g(n) = O(n 2 ). 3.Consider the functions f(n) =√ n and g(n) = log n. Show that: f(n) + g(n) =O(√n). 4.Indicate whether f(n) = O(g(n)) when we have f(n) = 10n and g(n) = n 2 - 10n. 5.Indicate whether f(n) = O(g(n)) when we have f(n) = n 3 and g(n) = n 2 log n.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google