Download presentation

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 no such that: f(n) < cg(n),n > n0 The above equation states that as n increases, the algorithm complexity grows NO faster than a constant multiple ofg(n). n0 Input size Require Time

3
**Big-O Notation: Some Details**

Let's consider: f(n) = 5n + 3. A possible bounding function is: g(n) = n4. 5n+3 = O(n4) However, it is expected that the function g(n) should be of order (highest power) as small as possible. 5n+3 = O(n3) 5n+3 = O(n2)

4
**Big-O Notation: Some Examples**

Consider the following complexity function f(n) = 100 n. Then, choosing: c = 100, no = 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, no = 0 and g(n) = n will satisfy: f(n) < 5n Let assume now that the complexity is: f(n) = 3n The choice of: c = 3, no = 0 and g(n) = n2 is possible to have: f(n) < 3n2

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 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 no 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++) 2 sum += 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++) for(int j = 0; j < n; j++) sum+= B[i][j]; 4n2+ 4n +2 = O(n2)

12
**Computing Big-O Notation: Guideline (Contd.)**

An example of consecutive statements case is shown below: 1 for(int i = 0; i < n; i++) a[i] = 0; 3 for(int i = 0; i < n; i++) 4 for(int j = 0; j < n; j++) { sum = i + j; size += 1; } // End of inner loop. O(n +n2 ) = O(n2)

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]; 5 switch(key) { 6 case 'a': for(int i = 0; i < X.length; i++) sum += X[i]; 9 break; 10 case 'b': for(int i = 0; i < Y.length; j++) for(int j = 0; j < Y[0].length; j++) sum += Y[i][j]; break; 15 } // End of switch block o(n) o(n2) o(n2)

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]; 6 if(key == '+') { 7 for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) 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(n2) O(n3) O(n3) O(n)

15
**Sometimes ifelse statements must carefully checked: **

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

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

Similar presentations

Presentation is loading. Please wait....

OK

Complexity Analysis (Part II)

Complexity Analysis (Part II)

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on supply chain management of nokia pc Download ppt on turbo generator hybrid Disaster management ppt on uttarakhand Ppt on healthy food and junk food Ppt on indian intelligence agencies Ppt on question tags quiz Seminar ppt on brain machine interface Ppt on art of living download Ppt on power line carrier communication ppt Ppt on blood stain pattern analysis terminology