Divide and Conquer Pasi Frnti 2 11 2016
- Slides: 29
Divide and Conquer Pasi Fränti 2. 11. 2016
Divide and Conquer 1. Divide to sub-problems 2. Solve the sub-problems 3. Conquer the solutions By recursion! 2
Stupid example Stupid. Example(N) IF N=0 THEN RETURN ELSE FOR i← 1 TO N DO WRITE(‘x’); Stupid. Example(N-1); END; O(1) N O(1) T(N-1) 3
Repeat until T(0) Analysis by substitution method k+…+3+2+1 → 1+2+3+…+k k =N
Sorting algorithm Recursive “through the bones” Sort(A[i, j]) q Find. Min(A[i, j]); Swap(A[i], A[q]) Sort(A[i+1, j]); Find. Min(A[i, j]) q Find. Min(A[i+1, j]); IF A[i]<A[q] THEN RETURN i ELSE RETURN q;
Master Theorem Arithmetic case Time complexity function: Solution:
Master Theorem Proof for arithmetic case Substitution:
Master Theorem Proof for arithmetic case Case 1: N/c terms
Master Theorem Proof for arithmetic case Case 2: Arithmetic sum
Master Theorem Proof for arithmetic case Case 3: Dominating term
Quicksort(A[i, j]) IF i < j THEN k ← Partition(A[i, j]); Quicksort(A[i, k]); Quicksort(A[k+1, j]); ELSE RETURN; O(1) O(N) T(N/2)
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 7 3 2 6 x>7 1 5 11 20 13 10 14 8
Partition algorithm x≤p ∙∙∙ x>p r k FOR increases k Partition(A[i, j]) pivot = A[j]; r = i-1; ∙∙∙ unprocessed R increases when needed 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; Swa mor p don e wh e sp a to th ce ne en e lef eded t sid e
Selection in linear time Find 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 Theorem Geometric case Time complexity function: Solution:
Master Theorem p steps Proof of geometric case Extract
Master Theorem Proof of geometric case Case 1: Geometric sum a>1
Master Theorem Proof of geometric case Case 2: =1 N Log N terms Geometric sum
Master Theorem Proof of geometric case Case 3: <1 …summation…
Merge sort Main algorithm Merge. Sort(A[i, j]) IF i<j THEN k : = (i+j)/2; Merge. Sort (A[i, k]); Merge. Sort(A[k+1, j]); Merge(A[i, j], k); O(1) T(N/2) c∙N
Merge sort Merge 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 ← i TO j DO A[t]←B[t]; }
Merge sort Time complexity Since we have b=c O(N log N)
Merge sort Substitution method
Karatsuba-Ofman multiplication Multiplication of two n-digit numbers School book algorithm: 3624 2345 18120 14496 10872 7248 8498280
Karatsuba-Ofman multiplication Straightforward 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 multiplication Time complexity analysis
Karatsuba-Ofman multiplication Divide-and-Conquer revised + 6 summations and 1. 5 multiplications by kn
Wor ki ng s pace
- Divide and conquer advantages and disadvantages
- Dynamic programming vs divide and conquer
- Voronoi diagram
- Delaunay triangulation divide and conquer algorithm
- Counting inversions divide and conquer
- Metoda divide et impera
- Dynamic programming bottom up
- Dynamic programming vs divide and conquer
- Divide and conquer
- Strassen's matrix multiplication algorithm
- Divide and conquer
- Cse 202
- How to divide in pseudocode
- Subtract and conquer
- Tuliskan algoritma divide and conquer
- Defective chessboard problem divide and conquer
- Divide and conquer algorithm
- Divide and conquer algoritma
- Knapsack problem divide and conquer
- Dawned rink hand arrive
- Powering a number divide and conquer
- Is shell sort divide and conquer
- Let's divide and conquer
- Divide and conquer complexity
- Algorithm couple
- Nnn sort
- Conquer adalah
- Divide & conquer
- Muster theorem
- Divide and conquer