algorithm analysis
play

Algorithm Analysis Part II Tyler Moore CSE 3353, SMU, Dallas, TX - PowerPoint PPT Presentation

Algorithm Analysis Part II Tyler Moore CSE 3353, SMU, Dallas, TX Lecture 4 Some slides created by or adapted from Dr. Kevin Wayne. For more information see


  1. �������������� Algorithm Analysis Part II Tyler Moore CSE 3353, SMU, Dallas, TX Lecture 4 Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some slides adapted from Dr. Steven Skiena. For more information see http://www.algorist.com 9 2 / 43 Implications of dominance Testing dominance De fi nition f ( n ) Dominance g ( n ) dominates f ( n ) i ff lim n →∞ g ( n ) = 0 De fi nition Exponential algorithms get hopeless fast. Little oh notation f ( n ) is o ( g ( n )) i ff g ( n ) dominates f ( n ). Quadratic algorithms get hopeless at or before 1,000,000. O ( n log n ) is possible to about one billion. In other words, little oh means “grows strictly slower than”. Q: is 3 n o ( n 2 )? A: Yes, since lim n →∞ 3 n n 2 = 3 n = 0 Q: is 3 n 2 o ( n 2 )? A: 3 / 43 4 / 43

  2. ������������ ������������������������������������������� f ( n ) Proposition. If , then �� � � �� is � Θ � � � � �� . Polynomials. Let � � � ����� � � ��� � �� � ������� � �� � � � with � �� ����� . Then, � � � �� is Θ � � � � . lim g ( n ) = c > 0 n →∞ a 0 + a 1 n + . . . + a d n d Pf. By definition of the limit, there exists � � such such that for all �� � ≥ �� � � Pf. lim = a d > 0 n d n →∞ 1 2 c < f ( n ) g ( n ) < 2 c no need to specify base Logarithms. Θ ���� �� � � is Θ ���� �� � � for any constants � , � ����� . (assuming it is a constant) � Thus, � � � � �� ≤ ���� � � � � � � for all �� � ≥ �� � � , which implies � � � � � is � � � � � � � . � Similarly, � � � � ��� ≥ � �� ½ � � � � � � �� for all �� � ≥ �� � � , which implies � � � � � is Ω � � � � � � . Logarithms and polynomials. For every � ����� , ���� � � is � � � � �� � . f ( n ) Proposition. If , then �� � � �� is � � � � � � �� . lim g ( n ) = 0 n →∞ Exponentials and polynomials. For every � ����� and every � ����� , � � �� is � � � � �� � . n d lim r n = 0 Pf. n →∞ 15 16 5 / 43 6 / 43 Exercises ������������������ Linear time. Running time is proportional to input size. Computing the maximum. Compute maximum of � numbers � � ����� � � . Using the limit formula and results from earlier slides, answer the following: Q: Is 5 n 2 + 3 n o ( n )? max ← a 1 A: No, since lim n →∞ 5 n 2 +3 n = lim n →∞ 5 n + 3 = ∞ for i = 2 to n { n if (a i > max) Q: is 3 n 3 + 5 Θ ( n 3 )? max ← a i A: } Q: is n log n + n 2 O ( n 3 )? A: 19 7 / 43 8 / 43

  3. ������������������ ������������������������������ Merge. Combine two sorted lists � ��� � � �� � � ����� � � ������ � ��� � � �� � � ����� � � into sorted O(n log n) time. Arises in divide-and-conquer algorithms. whole. Sorting. Mergesort and heapsort are sorting algorithms that perform � � � ����� � � compares. Largest empty interval. Given � time-stamps � � ����� � � on which copies of a i = 1, j = 1 file arrive at a server, what is largest interval when no copies of file arrive? while (both lists are nonempty) { if (a i ≤ b j ) append a i to output list and increment i O(n log n) solution. Sort the time-stamps. Scan the sorted list in order, else(a i ≤ b j )append b j to output list and increment j identifying the maximum gap between successive time-stamps. } append remainder of nonempty list to output list Claim. Merging two lists of size � takes � � � � time. Pf. After each compare, the length of output list increases by � . 20 21 9 / 43 10 / 43 �������������������� � � ���������������� � � Ex. Enumerate all pairs of elements. Cubic time. Enumerate all triples of elements. Closest pair of points. Given a list of � points in the plane � � � �� � � ������� � � �� � � � , Set disjointness. Given � sets � � ����� � � each of which is a subset of find the pair that is closest. ��������� � , is there some pair of these which are disjoint? O(n 2 ) solution. Try all pairs of points. O(n 3 ) solution. For each pair of sets, determine if they are disjoint. min ← (x 1 - x 2 ) 2 + (y 1 - y 2 ) 2 foreach set S i { for i = 1 to n { foreach other set S j { for j = i+1 to n { foreach element p of S i { d ← (x i - x j ) 2 + (y i - y j ) 2 determine whether p also belongs to S j if (d < min) } min ← d if (no element of S i belongs to S j ) } report that S i and S j are disjoint } } Remark. Ω � � � � seems inevitable, but this is just an illusion. [see Chapter 5] } 22 23 11 / 43 12 / 43

  4. ��������������������� � � ���������������� Independent set of size k. Given a graph, are there � nodes such that no Independent set. Given a graph, what is maximum cardinality of an two are joined by an edge? independent set? k is a constant O(n k ) solution. Enumerate all subsets of � nodes. O(n 2 2 n ) solution. Enumerate all subsets. foreach subset S of k nodes { S* ← φ check whether S in an independent set foreach subset S of nodes { if (S is an independent set) check whether S in an independent set report S is an independent set if (S is largest independent set seen so far) } update S* ← S } } } � Check whether � is an independent set takes � � � � � time. ≤ n k � n � = n ( n − 1)( n − 2) × · · · × ( n − k + 1) � Number of � element subsets = k k ( k − 1)( k − 2) × · · · × 1 k ! � � � � � � � � ��� � ����� � � � � � . poly-time for k=17, but not practical 24 25 13 / 43 14 / 43 Common algorithm dominance classes �������������� Search in a sorted array. Given a sorted array � of � numbers, is a given number �� in the array? Dominance class Example problem types O(log n) solution. Binary search. 1 Operations independent of input size (e.g., addition, min(x,y), etc.) lo ← 1 �� hi ← n log n Binary search while (lo ≤ hi) { n Operating on every element in an array mid ← (lo + hi) / 2 n log n Quicksort, mergesort if (x < A[mid]) hi ← mid - 1 n 2 Operating on every pair of items else if (x > A[mid]) lo ← mid + 1 else return yes n 3 Operating on every triple of items } 2 n Enumerating all subsets of n items return no n ! Enumerating all orderings of n items 26 15 / 43 16 / 43

  5. Homework 1 Selecting the Right Jobs A movie star wants to the select the maximum number of staring roles such that no two jobs require his presence at the same time. Due at the beginning of class one week from today You are encouraged to work in pairs Tarjan of the Jungle The Four Volume Problem Steiner’s Tree Process Terminated The President’s Algorist Halting State Programming Challenges Please start on the Python coding early! "Discreet" Mathematics Calculated Bets 16 18 / 43 18 / 43 Brute-force movie-scheduling pseudo-code The Movie Star Scheduling Problem Input: A set I of n intervals on the line. Output: What is the largest subset of mutually ExhaustiveScheduling(I) non-overlapping intervals which can be selected from I ? j = 0 Give an algorithm to solve the problem! Smax = {} For each of the 2^n subsets Si of intervals I If (Si is mutually non-overlapping) and (size(Si)>j) then j = size(Si) and Smax = Si Return Smax 17 19 / 43 20 / 43

  6. Earliest Job First Earliest Job First is Wrong! Start working as soon as there is work available: The first job might be so long (W ar and Peace) that it prevents us from taking any other job. EarliestJobFirst(I) Accept the earlest starting job j from I which does not overlap any previously accepted job, and repeat until no more such jobs remain. 18 19 21 / 43 22 / 43 Shortest Job First Shortest Job First is Wrong! Always take the shortest possible job, so you spend the least Taking the shortest job can prevent us from taking two longer time working (and thus unavailable). jobs which barely overlap it. ShortestJobFirst(I) While (I ≠ ∅ ) do Accept the shortest possible job j from I . Delete j , and intervals which intersect j from I . 20 21 23 / 43 24 / 43

Recommend


More recommend