Loading in 5 sec....

Complexity Analysis (Part II )PowerPoint Presentation

Complexity Analysis (Part II )

- By
**janna** - Follow User

- 92 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Complexity Analysis (Part II )' - janna

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Complexity Analysis (Part II)

- Asymptotic Analysis.
- Big-O Notation: More Details.
- Order Arithmetic Rules.
- Big-O Computation: Improved Guide lines.

n0

Input size

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).

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)

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) = 3n2 - 100.
- The choice of: c = 3, no = 0 and g(n) = n2 is possible to have:
f(n) < 3n2

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).

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.

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??..

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.

- The number of operations is equal to the number of iterations
- 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)]).

- Use the addition rule of order arithmetic:
- 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])

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.

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)

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)

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;

- } // End of inner loop.

O(n +n2 ) = O(n2)

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(n2)

o(n2)

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(n2)

O(n3)

O(n3)

O(n)

- 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

- integers[0] = -20;
1 public boolean hasPrimes(int[] arr) {

2 for(int i = 0; i < arr.length; i++)

3 ..........

4 ..........

5 ..........

6 } // End of hasPrimes()

- integers[0] = -20;

O(1)

O(ifelse) = O(Condition) = O(n)

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.

Download Presentation

Connecting to Server..