��������������������������� Divide-Conquer-Glue Algorithms Divide-and-conquer. Mergesort and Counting Inversions � Divide up problem into several subproblems. � Solve each subproblem recursively. � Combine solutions to subproblems into overall solution. Tyler Moore Most common usage. CSE 3353, SMU, Dallas, TX � Divide problem of size � into two subproblems of size � � � � � in linear time. � Solve two subproblems recursively. Lecture 10 � Combine two solutions into overall solution in linear time. Consequence. � Brute force: Θ � � � � . � Divide-and-conquer: Θ � �� ��� �� � . Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused or adapted from Python Algorithms by Magnus Lie Hetland. ��������������������������� 2 2 / 22 ��������������� Problem. Given a list of � elements from a totally-ordered universe, 5. D IVIDE AND C ONQUER rearrange them in ascending order. ‣ mergesort ‣ counting inversions ‣ closest pair of points ‣ randomized quicksort ‣ median and selection � ������� ��� 4 3 / 22 4 / 22
�������������������� ��������� Obvious applications. � Recursively sort left half. � Organize an MP3 library. � Recursively sort right half. � Display Google PageRank results. � Merge two halves to make sorted whole. � List RSS news items in reverse chronological order. ����� Some problems become easier once elements are sorted. � Identify statistical outliers. A L G O R I T H M S � Binary search in a database. �������������� � Remove duplicates in a mailing list. A G L O R I T H M S Non-obvious applications. ��������������� � Convex hull. A G L O R H I M S T � Closest pair of points. � Interval scheduling / interval partitioning. ������������� � Minimum spanning trees (Kruskal's algorithm). A G H I L M O R S T � Scheduling to minimize maximum lateness or average completion time. � ... 5 6 5 / 22 6 / 22 Canonical Divide-Conquer-Glue Algorithm ������� Goal. Combine two sorted lists � and � � into a sorted whole � . � Scan � and � from left to right. � Compare � � and � � . � If � � �� ≤ ��� � , append � � to ��� (no larger than any remaining element in � ). � If � � ���� �� � , append � � to �� (smaller than every remaining element in � ). def d i v i d e a n d c o n q u e r (S , divide , glue ) : l e n (S) == 1: return S i f L , R = d i v i d e (S) A = d i v i d e a n d c o n q u e r (L , divide , glue ) ������������� ������������� B = d i v i d e a n d c o n q u e r (R, divide , glue ) � � � � 3 7 10 18 2 11 17 23 return glue (A, B) 5 2 ��������������������������� 2 3 7 10 11 7 7 / 22 8 / 22
Mergesort in Python How can we measure the time complexity of recursive algorithms? mergesort ( seq ) : 1 def mid = l e n ( seq )/2 #Midpoint f o r d i v i s i o n 2 Measuring the time complexity of iterative algorithms is usually l f t , r g t = seq [ : mid ] , seq [ mid : ] 3 l e n ( l f t ) > 1 : l f t = mergesort ( l f t ) #Sort i f by h a l v e s 4 straightforward: count the inputs, check for loops, etc. l e n ( r g t ) > 1 : r g t = mergesort ( r g t ) i f 5 We know that certain operations can take linear time, constant time, r e s = [ ] #Merge s o r t e d h a l v e s 6 while l f t and r g t : #N e i t h e r h a l f i s empty 7 logarithmic time, etc. i f l f t [ − 1] > = r g t [ − 1]: #l f t has g r e a t e s t l a s t v a l u e 8 r e s . append ( l f t . pop ( ) ) #Append i t Running those operation in a loop n times produces a multiplicative 9 e l s e : #r g t has g r e a t e s t l a s t v a l u e 10 factor r e s . append ( r g t . pop ( ) ) #Append i t 11 r e s . r e v e r s e () #R e s u l t i s backward 12 But how can we do this for recursive algorithms? With recurrence ( l f t r g t ) + r e s return or #Also add the remainder 13 relations 9 / 22 10 / 22 Recurrence Relations ���������������������������� Def. � � � � � = max number of compares to mergesort a list of size ≤ � . Note. � � � � � is monotone nondecreasing. Mergesort recurrence. Recurrence relations specify the cost of executing recursive functions. Consider mergesort � ��� � ���� � � � ��� ≤ Linear-time cost to divide the lists 1 �� � � ⎡ � ���� ⎤ � ������� �� � � ⎣ � ���� ⎦ � ������ � ��������� Two recursive calls are made, each given half the original input 2 Linear-time cost to merge the resulting lists together 3 Recurrence: T ( n ) = 2 T ( n 2 ) + Θ ( n ) Solution. � � � � � � is � � � � ���� � � � � . Great, but how does this help us estimate the running time? Assorted proofs. We describe several ways to prove this recurrence. Initially we assume � is a power of � and replace ≤ with � . 8 11 / 22 12 / 22
������������������������������������������������������� ������������������ Proposition. If � � � � � � satisfies the following recurrence, then � � � � � � � � ���� � � � . Proposition. If � � � � � � satisfies the following recurrence, then � � � � � � � � ���� � � � . assuming n assuming n � ��� � ���� � ��� � ���� is a power of 2 is a power of 2 � � � ���� � � � ���� �� ��� � � ���������� � ��������� �� ��� � � ���������� � ��������� Pf 1. Pf 2. [by induction on � ] � � � � � � ��� � � Base case: when � ���� , � ������� . � Inductive hypothesis: assume � � � ����� � ���� � � � . � � � �� ���� � � � �� ���� ��� � ��� ��� � � Goal: show that � �� � ������ � ���� � ��� � � . ��� � ��� ��� � � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � �� � �� � �� � � � � � ������ � ��� ��� � � �� � � �� ��� �� � ����� � � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � � � �� ���� � � � � ��� ��� � � �� � � �� ���� �� � �� ����������� � ⋮ � �� � � �� ��� �� � �� ������ ▪ ⋮ � � � � ����� �� �� 9 10 13 / 22 14 / 22 ������������������� Music site tries to match your song preferences with others. 5. D IVIDE AND C ONQUER � You rank � songs. � Music site consults database to find people with similar tastes. ‣ mergesort Similarity metric: number of inversions between two rankings. ‣ counting inversions � My rank: ��������� � . ‣ closest pair of points � Your rank: � � �� � � ����� � � . ‣ randomized quicksort � Songs � and � are inverted if � ����� � , but � �� ���� � � . ‣ median and selection A B C D E me 1 2 3 4 5 � ������� ��� you 1 3 4 2 5 ����������������������� Brute force: check all Θ � � � � pairs. 13 15 / 22 16 / 22
Recommend
More recommend