review big o notation
play

Review: Big O Notation Let T(n) be a function that defines the - PowerPoint PPT Presentation

1 Review: Big O Notation Let T(n) be a function that defines the worst-case running time of an algorithm. T(n) is O(f(n)) if T(n) c f(n), where c 0 for all n n 0 Example: Let T(n) = 3n + 2 T(n) is O(n) because T(n)


  1. 1 Review: Big O Notation Let T(n) be a function that defines the worst-case running time of an algorithm. T(n) is O(f(n)) if 
 T(n) ≤ c ∙ f(n), where c ≥ 0 for all n ≥ n 0 Example: 
 Let T(n) = 3n + 2 
 T(n) is O(n) because 
 T(n) ≤ 4n for all n ≥ 2 O(n) is the asymptotic upper bound of T(n). 2 Some Asymptotic Orderings Logarithms: 
 log a n is O(n d ), for all bases a and all degrees d 
 ➔ All logarithms grow slower than all 
 polynomials 100 y = x 2 75 50 25 y = log 2 x 0 1 2 3 4 5 6 7 8 9 10 3 Some Asymptotic Orderings Exponential functions: 
 n d is O(r n ) when r > 1 
 ➔ Polynomials grow no more quickly than 
 exponential functions. 1200 y = 2 x 900 600 300 y = x 2 0 1 2 3 4 5 6 7 8 9 10 Slides04 O() and Graphs.key - February 4, 2019

  2. 4 Ordering of Common O() Functions O(1) O(log n) O(n) O(n log n) O(n d ) O(r n ) 5-1 Linear Time - O(n) Linear time. Running time is at most a constant factor times the size of the input. Computing the maximum. Compute maximum of n numbers a 1 , …, a n . 5-2 Linear Time - O(n) Linear time. Running time is at most a constant factor times the size of the input. Computing the maximum. Compute maximum of n numbers a 1 , …, a n . max = a 1 for i = 2 to n { if (a i > max) max = a i } Slides04 O() and Graphs.key - February 4, 2019

  3. 6 Cost? Merge. Combine two sorted lists A = a 1 ,a 2 , …,a n with B = b 1 ,b 2 ,…,b n into sorted whole. 7-1 Logarithmic time - O(log n) Logarithmic time. Do a constant amount of work to discard a constant fraction of the input (often 1/2 ) Binary search: Search a sorted collection 7-2 Logarithmic time - O(log n) Logarithmic time. Do a constant amount of work to discard a constant fraction of the input (often 1/2 ) Binary search: Search a sorted collection low = 0; high = a.length - 1; while( low <= high ) { middle = ( low + high ) / 2; if( key == a[ middle ] ) return middle; else if( key < a[ middle ] ) high = middle - 1; else low = middle + 1; } return -1; Slides04 O() and Graphs.key - February 4, 2019

  4. 8 Linearithmic time - O(n log n) Linearithmic time. Divide-and-conquer Repeatedly divide a problem in half (gives the log n part) Solve the problem in constant time Combine the results of the divided problems in linear time Mergesort: Repeatedly divide collection in half Sort when collections have size 2 Merge resulting lists 9 Mergesort 13 17 6 3 9 2 16 1 Divide 13 17 6 3 9 2 16 1 13 17 6 3 9 2 16 1 13 17 3 6 2 9 1 16 Sort 3 6 13 17 1 2 9 16 Conquer 1 2 3 6 9 13 16 17 10-1 Quadratic Time - O(n 2 ) Quadratic time. Examine all pairs of input Typically involves nested loops Closest pair of points in a plane. Given a list of n points in the plane (x 1 , y 1 ), …, (x n , y n ), find the pair that is closest. Slides04 O() and Graphs.key - February 4, 2019

  5. 10-2 Quadratic Time - O(n 2 ) Quadratic time. Examine all pairs of input Typically involves nested loops Closest pair of points in a plane. Given a list of n points in the plane (x 1 , y 1 ), …, (x n , y n ), find the pair that is closest. min = (x 1 - x 2 ) 2 + (y 1 - y 2 ) 2 for i = 1 to n { for j = i+1 to n { d = (x i - x j ) 2 + (y i - y j ) 2 if (d < min) min = d } } Beyond Polynomial Time 11-1 O(N!). Consider all permutations and pick the best. Traveling salesperson. Given n cities with the distances between each pair of cities. Find the shortest path that visits each city exactly once. Beyond Polynomial Time 11-2 O(N!). Consider all permutations and pick the best. Traveling salesperson. Given n cities with the distances between each pair of cities. Find the shortest path that visits each city exactly once. min = sum of distances visiting cities in order c1, c2, ... cn shortest_path = c1, c2, ... cn for every other permutation p { cost = sum of distances of that permutation if (cost < min) { min = cost shortest_path = p } } Slides04 O() and Graphs.key - February 4, 2019

  6. 12 Running Times as Functions of Input Size 13-1 Asymptotic Analysis of Gale-Shapley Initialize each college and student to be free. while (some college is free and hasn't accepted every student) { Choose such a college c s = 1 st student on c’s list that c has not yet accepted if (s is free) assign c and s to each other else if (s prefers c to her current college c’) assign s and c to each other, and c' to be free else s rejects c } 13-2 Asymptotic Analysis of Gale-Shapley Cost 2n Initialize each college and student to be free. while (some college is free and hasn't accepted 1 every student) { Choose such a college c 1 s = 1 st student on c’s list that c has not 1 yet accepted if (s is free) assign c and s to each other 1 else if (s prefers c to her current college 1 c’) assign s and c to each other, and c' to 1 be free else 1 s rejects c } Slides04 O() and Graphs.key - February 4, 2019

  7. 13-3 Asymptotic Analysis of Gale-Shapley Cost 2n Initialize each college and student to be free. while (some college is free and hasn't accepted 1 every student) { Choose such a college c 1 s = 1 st student on c’s list that c has not 1 yet accepted 1 if (s is free) assign c and s to each other 1 else if (s prefers c to her current college 1 c’) assign s and c to each other, and c' to 1 be free else 1 s rejects c } 14-1 Asymptotic Analysis of Gale-Shapley Initialize each college and student to be free. while (some college is free and hasn't accepted every student) { Choose such a college c s = 1 st student on c’s list that c has not yet accepted if (s is free) assign c and s to each other else if (s prefers c to her current college c’) assign s and c to each other, and c' to be free else s rejects c } 14-2 Asymptotic Analysis of Gale-Shapley Cost Reps 2n 1 Initialize each college and student to be free. while (some college is free and hasn't accepted 1 ≤ n 2 every student) { Choose such a college c 1 ≤ n 2 s = 1 st student on c’s list that c has not 1 ≤ n 2 yet accepted if (s is free) 1 ≤ n 2 assign c and s to each other 1 ≤ n 2 else if (s prefers c to her current college 1 ≤ n 2 c’) assign s and c to each other, and c' to 1 ≤ n 2 be free else 1 ≤ n 2 s rejects c } Slides04 O() and Graphs.key - February 4, 2019

  8. 14-3 Asymptotic Analysis of Gale-Shapley Cost Reps 2n 1 Initialize each college and student to be free. while (some college is free and hasn't accepted 1 ≤ n 2 every student) { 1 ≤ n 2 Choose such a college c s = 1 st student on c’s list that c has not 1 ≤ n 2 yet accepted 1 if (s is free) 1 ≤ n 2 assign c and s to each other 1 ≤ n 2 else if (s prefers c to her current college 1 ≤ n 2 c’) assign s and c to each other, and c' to 1 ≤ n 2 be free else 1 ≤ n 2 s rejects c } 15 Undirected Graph Undirected graph. G = (V , E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = |V|, m = |E|. V = {1, 2, 3, 4, 5} 1 2 E = {(1,2), (1,4), (1,5), (2,3), (2,4), 3 (3,5)} n=5 5 4 m=6 16 Food “chain” http:// www.twingroves.district96.k 12.il.us/Wetlands/ Salamander/SalGraphics/ salfoodweb.gif Slides04 O() and Graphs.key - February 4, 2019

Recommend


More recommend