divide and conquer
play

Divide and Conquer Algorithm Theory WS 2012/13 Fabian Kuhn Divide - PowerPoint PPT Presentation

Chapter 1 Divide and Conquer Algorithm Theory WS 2012/13 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search can be considered


  1. Chapter 1 Divide and Conquer Algorithm Theory WS 2012/13 Fabian Kuhn

  2. Divide ‐ And ‐ Conquer Principle • Important algorithm design method • Examples from Informatik 2: • Sorting: Mergesort, Quicksort • Binary search can be considered as a divide and conquer algorithm • Further examples • Median • Comparison orders • Delaunay triangulation / Voronoi diagram • Closest pairs • Line intersections • Integer factorization / FFT • ... Algorithm Theory, WS 2012/13 Fabian Kuhn 2

  3. Example 1: Quicksort � � � ℓ � � � � � � � function Quick ( � : sequence): sequence; {returns the sorted sequence � } begin if #� � 1 then return � else { choose pivot element � in � ; partition � into � ℓ with elements � � , and � � with elements � � Quick( � ℓ ) � Quick( � � ) return end ; Algorithm Theory, WS 2012/13 Fabian Kuhn 3

  4. Example 2: Mergesort � � ℓ � � sort recursively sort recursively � ℓ � ℓ merge � Algorithm Theory, WS 2012/13 Fabian Kuhn 4

  5. Formulation of the D&C principle Divide ‐ and ‐ conquer method for solving a problem instance of size � : 1. Divide � � � : Solve the problem directly. � � � : Divide the problem into � subproblems of sizes � 1 , … , � � � � ( � � 2 ). 2. Conquer Solve the � subproblems in the same way (recursively). 3. Combine Combine the partial solutions to generate a solution for the original instance. Algorithm Theory, WS 2012/13 Fabian Kuhn 5

  6. Analysis Recurrence relation: • ���� : max. number of steps necessary for solving an instance of size � �� � � � • ���� � � � � � � ⋯ � � � � �� � � � � ���� ��� ������ ��� ������� Special case: � � �, � � � � � � � � ⁄ • cost for divide and combine: DC � • ��1� � � • ���� � 2���/2� � DC��� Algorithm Theory, WS 2012/13 Fabian Kuhn 6

  7. Analysis, Example Recurrence relation: � �� � , ⁄ � � � 2 ⋅ � � 2 � 1 � � Guess the solution by repeated substitution: Algorithm Theory, WS 2012/13 Fabian Kuhn 7

  8. Analysis, Example Recurrence relation: � �� � , ⁄ � � � 2 ⋅ � � 2 � 1 � � Verify by induction: Algorithm Theory, WS 2012/13 Fabian Kuhn 8

  9. Comparing Orders • Many web systems maintain user preferences / rankings on things like books, movies, restaurants, … • Collaborative filtering: – Predict user taste by comparing rankings of different users. – If the system finds users with similar tastes, it can make recommendations (e.g., Amazon) • Core issue: Compare two rankings – Intuitively, two rankings (of movies) are more similar, the more pairs are ordered in the same way – Label the first user’s movies from 1 to n according to ranking – Order labels according to second user’s ranking – How far is this from the ascending order (of the first user)? Algorithm Theory, WS 2012/13 Fabian Kuhn 9

  10. Number of Inversions Formal problem: • Given : array � � �� � , � � , � � , … , � � � of distinct element s • Objective : Compute number of inversions � � ≔ 0 � � � � � � � � � � � • Example : � � � 4 , 1 , 5 , 2 , 7 , 10 , 6 � • Naive solution : Algorithm Theory, WS 2012/13 Fabian Kuhn 10

  11. Divide and conquer � � ℓ � � 1. Divide array into 2 equal parts � ℓ and � � 2. Recursively compute #inversions in � ℓ and � � 3. Combine: add #pairs � � ∈ � ℓ , � � ∈ � � such that � � � � � � � � � � ℓ � � Algorithm Theory, WS 2012/13 Fabian Kuhn 11

  12. Combine Step • Assume � ℓ and � � are sorted � � � � � ℓ � � � � • Pointers � and � , initially pointing to first elements of � ℓ and � � • If � � � � � : – � � is smallest among the remaining elements – No inversion of � � and one of the remaining elements – Do not change count • If � � � � � : – � � is smallest among the remaining elements – � � is smaller than all remaining elements in � ℓ – Add number of remaining elements in � ℓ to count • Increment point, pointing to smaller element Algorithm Theory, WS 2012/13 Fabian Kuhn 12

  13. Combine Step • Need sub ‐ sequences in sorted order • Then, combine step is like merging in merge sort • Idea : Solve sorting and #inversions at the same time! Partition � into two equal parts � ℓ and � � 1. Recursively compute #inversions and sort � ℓ and � � 2. Merge � ℓ and � � to sorted sequence, at the same time, compute 3. number of inversions between elements � � in � ℓ and � � in � � Algorithm Theory, WS 2012/13 Fabian Kuhn 13

  14. Analysis, Example Recurrence relation: ⁄ � � � 2 ⋅ � � 2 � � ⋅ �, � 1 � � Repeated substitution: Algorithm Theory, WS 2012/13 Fabian Kuhn 14

  15. Analysis, Example Recurrence relation: ⁄ � � � 2 ⋅ � � 2 � � ⋅ �, � 1 � � Verify by induction: Algorithm Theory, WS 2012/13 Fabian Kuhn 15

  16. Geometric divide ‐ and ‐ conquer Closest Pair Problem : Given a set � of � points, find a pair of points with the smallest distance. Naive solution: Algorithm Theory, WS 2012/13 Fabian Kuhn 16

  17. Divide ‐ and ‐ conquer solution Divide � into two equal sized sets � ℓ und � � . 1. Divide: 2. Conquer: � ℓ � mindist�� ℓ � � � � mindist��� � 3. Combine: � ℓ� � min � � ℓ , � � | � ℓ ∈ � ℓ , � � ∈ � � return min �� ℓ , � � , � ℓ� � � � � � ℓ� � ℓ � ℓ � � Algorithm Theory, WS 2012/13 Fabian Kuhn 17

  18. Divide ‐ and ‐ conquer solution Divide � into two equal sized sets � ℓ und � � . 1. Divide: 2. Conquer: � ℓ � mindist�� ℓ � � � � mindist��� � 3. Combine: � ℓ� � min � � ℓ , � � | � ℓ ∈ � ℓ , � � ∈ � � return min � ℓ , � � , � ℓ� Computation of � ℓ� : � � � min�� ℓ , � � � � � � ℓ � � Algorithm Theory, WS 2012/13 Fabian Kuhn 18

  19. Merge step 1. Consider only points within distance � of the bisection line, in the order of increasing � ‐ coordinates. 2. For each point � consider all points � within � ‐ distance at most � 3. There are at most 7 such points. Algorithm Theory, WS 2012/13 Fabian Kuhn 19

  20. Combine step � 4 � � 3 � 2 � 1 � � � ℓ � � � � � � � min �� ℓ , ��� Algorithm Theory, WS 2012/13 Fabian Kuhn 20

  21. Implementation • Initially sort the points in � in order of increasing � ‐ coordinates • While computing closest pair, also sort � according to � ‐ coord. – Partition � into � ℓ and � � , solve and sort sub ‐ problems recursively – Merge to get sorted � according to � ‐ coordinates – Center points: points within � ‐ distance � � min � ℓ , � � of center – Go through center points in � in order of incr. � ‐ coordinates Algorithm Theory, WS 2012/13 Fabian Kuhn 21

  22. Running Time Recurrence relation: ⁄ � � � 2 ⋅ � � 2 � � ⋅ �, � 1 � � Solution: • Same as for computing number of number of inversions, merge sort (and many others…) � � � ��� ⋅ log �� Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend