o: order of magnitude look at the loops and to see whether the loops are nested. ◦ one single...

15
Big O notation

Upload: jalen-arrasmith

Post on 15-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

Big O notation

Page 2: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

O: order of magnitude Look at the loops and to see whether the

loops are nested.◦ One single loop: O(n)◦ A nested loop: O(n2)◦ A nested loop in a loop: O(n3)

Big O Notation (informal definition)

Page 3: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

for (int i=0; i<n; i++)for (int j=0; j< n; j++)

simple statement

for(int k=0; k< n; k++) {simple statement 1simple statement 2simple statement 3simple statement 4simple statement 5

}Simple statement 6……Simple statement 30

Formal definition of Big O

n2

5n

25

Page 4: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

T(n) = n2 + 5n + 25 Or T(n) = O(f(n)) These exists two constants, n0 and c

(>0) and a function f(n) such that all n>n0, cf(n) T(n).

Translate as: If n gets sufficiently large, there is some

constants c for which processing time will always be less than or equal to cf(n).

cf(n) is an upper bound on the performance.

Formal definition of Big O

Page 5: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

The growth rate of f(n) will be determined by the growth rate of the fastest growing term

It’s safe to ignore all constants and drop the lower order terms when determining the Big O for an algorithm

Formal definition of Big O

Page 6: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

for (int i=0; i< n-1; i++) {for (int j=i+1; j<n; j++) {

Simple statement 1Simple statement 2Simple statement 3}

}

Example

T(n) = 3(n-1) +3(n-2) + 3(n-3)+…..+3= 3(n-1+n-2+n-3+….+1) = 3n(n-1)/2 = 1.5n2 -1.5n

n0=1, c = 1.51.5n2 1.5n2-1.5n

Page 7: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

Example

Page 8: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

for (i=0; i< x.length; i *=2) {// print out i}

- The loop body will execute k-1 times with i: 1,2,4,8,16… until 2k > x.length

- 2k-1 <= x.length < 2k

- K-1 <= log2(x.length) < k

- So this loop has O(log2n)

Example

Page 9: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

1. for (int i=0; i<n; i++) for (int j=0; j<n; j++)

System.out.println(i+” “+j);

2. for (int i=0; i<n; i++) for (int j=0; j<2; j++)

System.out.println(i+” “+j);

Example

Page 10: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

3. for (int i=0; i<n; i++) for (int j=n-1; j>=i; j--)

System.out.println(i+” “+j);

4. for (int i=0; i<n; i++) for (int j=0; j<i; j++)

if (j %i == 0)System.out.println(i+” “+j);

Example

Page 11: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

for (int i=1; i<=n; i++)for (int j=1; j<=n; j++)

for (int k=n; k>=1 ; k--) {Int sum = i+j+k;}

}}

Example

Page 12: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

If T(n) is the form of a polynomial of degree d (d is the highest exponent), then it is O(nd).

O(1) represents a constant growth rate. This value doesn’t change with the number of inputs. Any finite number of O(1) steps is still O(1)

Common rule for polynomials

Page 13: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

public static boolean areUnique(int[] x) {for(int i=0; i< x.length; i++) { for (int j=0; j<x.length; j++) { if (i != j && x[i] == x[j]) return false;

}}return true;

}

Example

Page 14: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

Big O Name

O(1) Constant

O(log n) Logarithmic

O(n) Linear

O(n log n) Log-linear

O(n2) Quadratic

O(n3) Cubic

O(2n) Exponential

O(n!) Factorial

Comparing Performance

Page 15: O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop

15

Efficiency of Algorithms