300 likes | 427 Views
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)
E N D
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) • StupidExample(N-1); T(N-1) • END;
Analysis by substitution method Repeat until T(0) k+…+3+2+1 → 1+2+3+…+k k=N
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;
Master TheoremArithmetic case Time complexity function: Solution:
Master TheoremProof for arithmetic case Substitution:
Master TheoremProof for arithmetic case Case 1: N/c terms
Master TheoremProof for arithmetic case Case 2: Arithmetic sum
Master TheoremProof for arithmetic case Case 3: Dominating term
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;
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
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
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);
Master TheoremGeometric case Time complexity function: Solution:
Master TheoremProof of geometric case p steps Extract
Master TheoremProof of geometric case Case 1: Needs revisions!
Master TheoremProof of geometric case Case 2: =1 Geometric sum Log N terms N
Master TheoremProof of geometric case Case 3: Needs revisions! …summation…
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
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]; }
Merge sortTime complexity Since we have b=c O(N log N)
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
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:
Karatsuba-Ofman multiplicationDivide-and-Conquer revised + 6 summations and 1.5 multiplications by kn