37
Asymptotic Notation (O, Ω, ) Describes the behavior of the time or space complexity for large instance characteristics Common asymptotic functions 1 (constant), log n, n (linear) n log n, n 2 , n 3 2 n ( exponential), n! where n usually refer to the number of instance of data (data 的的 的)

Asymptotic Notation (O, Ω, ) Describes the behavior of the time or space complexity for large instance characteristics Common asymptotic functions

Embed Size (px)

Citation preview

Page 1: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Asymptotic Notation (O, Ω, ) Describes the behavior of the time or

space complexity for large instance characteristics

Common asymptotic functions 1 (constant), log n, n (linear) n log n, n2 , n3

2n ( exponential), n! where n usually refer to the number of

instance of data (data的個數 )

Page 2: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Common asymptotic functions

Page 3: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Common asymptotic functions

Page 4: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Common asymptotic functions

Page 5: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Common asymptotic functions

Page 6: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Big-O notation

The big O notation provides an upper bound for the function f

Definition: f(n) = O(g(n)) iff positive constants c and n0

exist such that f(n) c g(n) for all n, nn0 e.g. f(n) = 10n2 + 4n + 2 then, f(n) = O(n2), or O(n3), O(n4), …

Page 7: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Ω(Omega) notation

The Ω notation provides a lower bound for the function f

Definition: f(n) = Ω(g(n)) iff positive constants c and n0

exist such that f(n) c g(n) for all n, nn0 e.g. f(n) = 10n2 + 4n + 2 then, f(n) = Ω(n2), or Ω(n), Ω(1)

Page 8: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

notation The notation is used when the

function f can be bounded both from above and below by the same function g

Definition: f(n) = (g(n)) iff positive constants c1and c2,

and an n0 exist such that c1g(n) f(n) c2g(n) for all n, nn0

e.g. f(n) = 10n2 + 4n + 2 then, f(n) = Ω(n2), and f(n)=O(n2),

therefore, f(n)= (n2)

Page 9: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Sorting

Rearrange a[0], a[1], …, a[n-1] into ascending order. When done, a[0] <= a[1] <= … <= a[n-1]

8, 6, 9, 4, 3 => 3, 4, 6, 8, 9

Page 10: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Sort Methods

Insertion Sort Bubble Sort Selection Sort Rank Sort Shell Sort Heap Sort Merge Sort Quick Sort

Page 11: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insert An Element

Given a sorted list/sequence, insert a new element

Given 3, 6, 9, 14 Insert 5 Result 3, 5, 6, 9, 14

Page 12: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insert an Element

3, 6, 9, 14 insert 5 Compare new element (5) and last one

(14) Shift 14 right to get 3, 6, 9, , 14 Shift 9 right to get 3, 6, , 9, 14 Shift 6 right to get 3, , 6, 9, 14 Insert 5 to get 3, 5, 6, 9, 14

Page 13: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insert An Element

// insert t into a[0:i-1]int j;for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];a[j + 1] = t;

Page 14: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insertion Sort

Start with a sequence of size 1 Repeatedly insert remaining elements

Page 15: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insertion Sort

Sort 7, 3, 5, 6, 1 Start with 7 and insert 3 => 3, 7 Insert 5 => 3, 5, 7 Insert 6 => 3, 5, 6, 7 Insert 1 => 1, 3, 5, 6, 7

Page 16: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insertion Sort

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] // code to insert comes here}

Page 17: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insertion Sort

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;}

Page 18: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Insertion Sort (C++)

Page 19: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Complexity

Space/Memory Time

Count a particular operation Count number of steps Asymptotic complexity

Page 20: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Comparison Count

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;}

Page 21: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Comparison Count

Pick an instance characteristic … n, n = a.length for insertion sort

Determine count as a function of this instance characteristic.

Page 22: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Comparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

How many comparisons are made?

Page 23: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Comparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

number of compares depends on a[]s and t as well as on n

Page 24: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Comparison Count

Worst case count = maximum count Best case count = minimum count Average count

Page 25: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Worst-Case Comparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

a = [1, 2, 3, 4] and t = 0 => 4 compares

a = [1,2,3,…,i] and t = 0 => i compares

Page 26: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Worst-Case Comparison Count

for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

total compares = 1 + 2 + 3 + … + (n-1)

= (n-1)n/2

Page 27: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Step Count

A step is an amount of computing that does not depend on the instance characteristic n

10 adds, 100 subtracts, 1000 multipliescan all be counted as a single step

n adds cannot be counted as 1 step

Page 28: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Step Count

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

for (int i = 1; i < a.length; i++) 1{// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1} 0

s/e

Page 29: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Step Count

s/e isn’t always 0 or 1

x = MyMath.sum(a, n);

where n is the instance characteristic

has a s/e count of n

Page 30: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Step Count

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

for (int i = 1; i < a.length; i++) 1{// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1} 0

s/e steps

ii+ 1

Page 31: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Step Countfor (int i = 1; i < n ; i++) { 2i + 3}

step count for for (int i = 1; i < n ; i++) is n

step count for body of for loop is2(1+2+3+…+n-1) + 3(n-1)= (n-1)n + 3(n-1)= (n-1)(n+3)

Page 32: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Asymptotic Complexity of Insertion Sort O(n2) What does this mean?

Page 33: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Complexity of Insertion Sort

Time or number of operations does not exceed c·n2 on any input of size n (n suitably large).

Actually, the worst-case time is Θ(n2) and the best-case is Θ(n)

So, the worst-case time is expected to quadruple each time n is doubled

Page 34: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Complexity of Insertion Sort

Is O(n2) too much time? Is the algorithm practical?

Page 35: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Practical Complexities

109 instructions/second

n n nlogn n2 n3

1000 1mic 10mic 1milli 1sec

10000 10mic 130mic 100milli 17min

106 1milli 20milli 17min 32years

Page 36: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Impractical Complexities

109 instructions/second

n n4 n10 2n

1000 17min 3.2 x 1013

years3.2 x 10283

years

10000 116days

??? ???

10^6 3 x 10^7years

?????? ??????

Page 37: Asymptotic Notation (O, Ω,  )  Describes the behavior of the time or space complexity for large instance characteristics  Common asymptotic functions

Faster Computer Vs Better Algorithm

Algorithmic improvement more usefulthan hardware improvement.

E.g. 2n to n3