1 / 29

Divide and Conquer

Pasi Fränti. Divide and Conquer. 16.9.2014. Divide and Conquer. Divide to sub-problems Solve the sub-problems Conquer the solutions. By recursion!. Stupid example. StupidExample(N) IF N=0 THEN RETURN O(1) ELSE FOR i←1 TO N DO N WRITE(‘x’); O(1)

avye-gentry
Download Presentation

Divide and Conquer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pasi Fränti Divide and Conquer 16.9.2014

  2. Divide and Conquer • Divide to sub-problems • Solve the sub-problems • Conquer the solutions By recursion!

  3. Stupid example • StupidExample(N) • IF N=0 THEN RETURN O(1) • ELSE • FOR i←1 TO N DO N • WRITE(‘x’); O(1) • StupidExample(N-1); T(N-1) • END;

  4. Analysis by substitution method Repeat until T(0) k+…+3+2+1 → 1+2+3+…+k k=N

  5. Sorting algorithmRecursive “through the bones” Sort(A[i, j]) q  FindMin(A[i, j]); Swap(A[i],A[q]) Sort(A[i+1, j]); FindMin(A[i, j]) q  FindMin(A[i+1, j]); IF A[i]<A[q] THEN RETURN i ELSE RETURN q;

  6. Master TheoremArithmetic case Time complexity function: Solution:

  7. Master TheoremProof for arithmetic case Substitution:

  8. Master TheoremProof for arithmetic case Case 1: N/c terms

  9. Master TheoremProof for arithmetic case Case 2: Arithmetic sum

  10. Master TheoremProof for arithmetic case Case 3: Dominating term

  11. Quicksort • Quicksort(A[i, j]) • IF i < j THEN O(1) • k ← Partition(A[i, j]); O(N) • Quicksort(A[i, k]); T(N/2) • Quicksort(A[k+1, j]); T(N/2) • ELSE RETURN;

  12. Partition algorithm • Choose (any) element as pivot-value p. • Arrange all x≤p to the left side of list. • Arrange all x>p to the right side. • Time complexity O(N). 7 3 11 2 20 13 6 1 10 14 5 8 p=7 x≤7 x>7 7 3 2 6 1 5 11 20 13 10 14 8

  13. Partition algorithm x≤p x>p unprocessed Partition(A[i, j]) pivot = A[j]; r = i-1; FOR k = i TO j-1 DO IF (A[k] ≤ pivot) THEN r = r + 1; SWAP(A[r], A[k]); SWAP(A[r+1], A[j]); RETURN r+1; ∙∙∙ r ∙∙∙ k FOR increases k R increaseswhen needed Swap done when more space needed to the left side

  14. Selection in linear timeFind the kth smallest Selection(A[i, j], k) IF i=j THEN RETURN A[i]; ELSE q ← Partition(A, i, j); size ← (q-i)+1; IF k ≤ size THEN Selection(A[i, q], k); ELSE Selection(A[q+1, j], k-size);

  15. Master TheoremGeometric case Time complexity function: Solution:

  16. Master TheoremProof of geometric case p steps Extract

  17. Master TheoremProof of geometric case Case 1: Needs revisions!

  18. Master TheoremProof of geometric case Case 2: =1 Geometric sum Log N terms N

  19. Master TheoremProof of geometric case Case 3: Needs revisions! …summation…

  20. Merge sortMain algorithm MergeSort(A[i, j]) IF i<j THEN k := (i+j)/2; O(1) MergeSort (A[i, k]); T(N/2) MergeSort(A[k+1, j]); T(N/2) Merge(A[i, j], k); c∙N

  21. Merge sortMerge step Merge(A[i, j], k) { l←i; m←k+1; t←i; WHILE (l≤k) OR (m ≤ j) IF l>k THEN B[t]←A[m]; m←m+1; ELSEIF m>j THEN B[t]←A[l]; l←l+1; ELSEIF A[l]<A[m] THEN B[t]←A[m]; m←m+1; ELSE B[t]←A[l]; l←l+1; t←t+1; FOR t ← 1 TO j DO A[t]←B[t]; }

  22. Merge sortTime complexity Since we have b=c O(N log N)

  23. Merge sortSubstitution method

  24. Karatsuba-Ofman multiplicationMultiplication of two n-digit numbers School book algorithm: 3 6 2 4  2 3 4 5 1 8 1 2 0 1 4 4 9 6 1 0 8 7 2 7 2 4 8 8 4 9 8 2 8 0

  25. Karatsuba-Ofman multiplicationStraightforward divide-and-conquer One n-digit number divided into two n/2-digit numbers (most and least significant) 3624=36∙102+24 2345=23∙102+45 Multiplication reformulated:

  26. Karatsuba-Ofman multiplicationTime complexity analysis

  27. Karatsuba-Ofman multiplicationDivide-and-Conquer revised + 6 summations and 1.5 multiplications by kn

  28. Working space

More Related