Divide and Conquer Chapter 4 1
Integer Multiplication 2
Integer Multiplication π¨ = π¦ Γ π§ Each with n bits x= 1 0 1 1 y= 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 Z= 1 0 0 0 0 1 0 0 3
Integer Multiplication NaΓ―ve_IM(x, y) 1. z = 0 2. while y > 0 3. if (y is odd) then z += x 4. n bit operations x *= 2 5. n iterations y /= 2 6. end 7. return z 8. π π = Ξ π 2 4
D&C Integer Multiplication π¦ π¦ 1 π¦ 2 π§ π§ 1 π§ 2 π π 2 πππ’π‘ 2 πππ’π‘ 5
D&C Integer Multiplication π¦ π¦ 1 π¦ 2 π§ π§ 1 π§ 2 π¦ 1 Γ π§ 2 π¦ 2 Γ π§ 2 π¦ 1 Γ π§ 1 π¦ 2 Γ π§ 1 π π 2 + π¦ 2 2 + π§ 2 π¨ = π¦ 1 2 π§ 1 2 π z = π¦ 1 π§ 1 2 π + π¦ 1 π§ 2 + π¦ 2 π§ 1 2 2 + π¦ 2 π§ 2 π π = 4π π π π = Ξ π 2 2 + π 6
Karatsubaβs Algorithm π¦ π¦ 1 π¦ 2 π§ π§ 1 π§ 2 π¨ = π¦π§ π z = π¦ 1 π§ 1 2 π + π¦ 1 π§ 2 + π¦ 2 π§ 1 2 2 + π¦ 2 π§ 2 π π¨ = π¦ 1 π§ 1 2 π + 2 + π¦ 2 π§ 2 π¦ 1 β π¦ 2 π§ 2 β π§ 1 + π¦ 1 π§ 1 + π¦ 2 π§ 2 2 π π = 3π π π π = Ξ π lg 3 2 + π 7
Example π¦ = 34 π¦ 1 = 3 π¦ 2 = 4 π§ = 53 π§ 1 = 5 π§ 2 = 3 π = 2 π¨ = π¦π§ z = π¦ 1 π§ 1 10 2 + ΰ΅« π¦ 1 β π¦ 2 π§ 2 β π§ 1 + 2 2 + π¦ 2 π§ 2 π¦ 1 π§ 1 + π¦ 2 π§ 2 10 ΰ΅― π¦ = 5 β 3 100 + β1 β β2 + 3 β 5 + 4 β 3 10 + 4 β 3 = 1500 + 2 + 15 + 12 β 10 + 12 = 1802 8
Matrix Multiplication Section 4.2 9
Matrix Multiplication = . π· = π΅. πΆ π=π π ππ . π ππ , assuming A, B, and C are π ππ = Ο π=1 square π Γ π matrices 10
Simple Matrix Multiplication n n n π π = Ξ π 3 11
D&C Matrix Multiplication π΅ = π΅ 11 π΅ 12 π΅ 22 , πΆ = πΆ 11 πΆ 12 πΆ 22 , π΅ 21 πΆ 21 π· = π· 11 π· 12 π· 21 π· 22 π· 11 π· 22 = π΅ 11 π· 12 π΅ 22 . πΆ 11 π΅ 12 πΆ 12 π· 21 π΅ 21 πΆ 21 πΆ 22 π· 11 = π΅ 11 . πΆ 11 + π΅ 12 . πΆ 21 π· 12 = π΅ 11 . πΆ 12 + π΅ 12 . πΆ 22 π· 21 = π΅ 21 . πΆ 11 + π΅ 22 . πΆ 21 π· 22 = π΅ 21 . πΆ 12 + π΅ 22 . πΆ 22 12
D&C Matrix Multiplication 13
Analysis of the D&C Algorithm π 2 + Ξ π 2 Ξ€ π π = 8π By applying the Master Theorem π = 8 , π = 2 , π π = Ξ π 2 π log π π = π 3 π π = Ξ π 2 = π π 3β1 Case 1 applies π π = Ξ π log π π = Ξ π 3 14
Strassenβs Algorithm π΅ = π΅ 11 π΅ 12 π΅ 22 , πΆ = πΆ 11 πΆ 12 πΆ 22 , π΅ 21 πΆ 21 π· = π· 11 π· 12 π· 21 π· 22 π 1 = πΆ 12 β πΆ 22 π 6 = πΆ 11 + πΆ 22 π 2 = π΅ 11 + π΅ 12 π 7 = π΅ 12 β π΅ 22 π 3 = π΅ 21 + π΅ 22 π 8 = πΆ 21 + πΆ 22 π 4 = πΆ 21 β πΆ 11 π 9 = π΅ 11 β π΅ 21 π 5 = π΅ 11 + π΅ 22 π 10 = πΆ 11 + πΆ 12 15
Strassenβs Algorithm π 1 = π΅ 11 . π 1 π 2 = π 2 . πΆ 22 π 3 = π 3 . πΆ 11 π 4 = π΅ 22 . π 4 π 5 = π 5 . π 6 π 6 = π 7 . π 8 π 7 = π 9 . π 10 π· 11 = π 5 + π 4 β π 2 + π6 π· 12 = π 1 + π 2 π· 21 = π 3 + π 4 π· 22 = π 5 + π 1 β π 3 β π 7 16
Strassenβs Algo Sketch 1. Strassen(A, B, n) 2. Split A, B into quadrants 3. Compute S1, β¦, S10 (Matrix add/sub) 4. Compute P1, β¦, P7 recursively 5. Compute C1, β¦, C4 (Matrix add/sub) 6. Return C π 2 + Ξ π 2 Ξ€ π π = 7π 17
Analysis of Strassenβs Algorithm π 2 + Ξ π 2 Ξ€ π π = 7π By applying the Master Theorem π = 7 , π = 2 , π π = Ξ π 2 π log π π = π log 2 7 = π 2.80735β¦ β π 2.8 π π = Ξ π 2 = π π lg 7β0.8 Case 1 applies π π = Ξ π log π π = π π 2.81 18
Linear-time Selection Section 9.3 19
Linear-time Selection Given an array π΅ of π elements and an integer 1 β€ π β€ π , find the π π’β smallest element in π΅ NaΓ―ve algorithm, sort π΅ and pick the π π’β element in the sorted array β Ξ πππ π Select and remove the smallest element k times β Ξ ππ Another quick-sort-like algorithm Pick the first element (pivot) Place it in its position in the array Recursively process one subarray 20
Quick-sort-like Algorithm π π΅ p 1 21
Quick-sort-like Algorithm π π΅ p 1 p 2 22
Quick-sort-like Algorithm π π΅ p 1 p 2 p 3 23
Quick-sort-like Algorithm π π΅ p 1 p 2 p 3 p 4 24
Quick-sort-like Algorithm π π΅ p 1 p 2 p 3 π π = Ξ π 2 p 4 β How to choose a good pivot? 25
Median of Fives A = {94, 82, 88, 12, 23, 61, 11, 13, 70, 37, 28, 31, 64, 6, 19, 32, 27, 38, 35, 21, 50, 91, 69, 57, 24, 93, 22, 43, 30, 67, 90, 48, 42, 65, 45} 26
Median of Fives A = {94, 82, 88, 12, 23, 61, 11, 13, 70, 37, 28, 31, 64, 6, 19, 32, 27, 38, 35, 21, 50, 91, 69, 57, 24, 93, 22, 43, 30, 67, 90, 48, 42, 65, 45} 1. Partition into groups of 5 94 61 28 32 50 93 90 82 11 31 27 91 22 48 88 13 64 38 69 43 42 12 70 6 35 57 30 65 23 37 19 21 24 67 45 27
Median of Fives A = {94, 82, 88, 12, 23, 61, 11, 13, 70, 37, 28, 31, 64, 6, 19, 32, 27, 38, 35, 21, 50, 91, 69, 57, 24, 93, 22, 43, 30, 67, 90, 48, 42, 65, 45} 2. Sort each sublist 12 11 6 21 24 22 42 23 13 19 27 50 30 45 82 37 28 32 57 43 48 88 61 31 35 69 67 65 94 70 64 38 91 93 90 28
Median of Fives A = {94, 82, 88, 12, 23, 61, 11, 13, 70, 37, 28, 31, 64, 6, 19, 32, 27, 38, 35, 21, 50, 91, 69, 57, 24, 93, 22, 43, 30, 67, 90, 48, 42, 65, 45} 3. Find the median of each sublist 12 11 6 21 24 22 42 23 13 19 27 50 30 45 82 37 28 32 57 43 48 π 88 61 31 35 69 67 65 94 70 64 38 91 93 90 29
Median of Fives A = {94, 82, 88, 12, 23, 61, 11, 13, 70, 37, 28, 31, 64, 6, 19, 32, 27, 38, 35, 21, 50, 91, 69, 57, 24, 93, 22, 43, 30, 67, 90, 48, 42, 65, 45} 4. Recursively find the median of the medians π = 82,37,28,32,57,43,48 Median of medians (m*) = 43 Partition A around m* and recursively process one side 30
Algorithm Pseudo-code S ELECT (π΅, π, π) 1. if (π β€ 5) then sort π΅ and return π π’β element 2. Partition π΅ into groups of 5 3. π ο§ Find the median of each group 4. π π π β = S ELECT (π, 5 , 10 ) 5. Partition π΅ around π β , let it be at position π 6. if (π = π) then return π β 7. if (π > π) then return S ELECT (π΅[1, π β 1], π β 1, π) 8. if (π < π) then return S ELECT (π΅[π + 1, π], π β π, π β π) 9. 31
Size of Sublist π π β medians of fives π 5 group 32
Size of Sublist π < < π β medians of fives π 5 group 33
Size of Sublist < < < < < < < π < < π β medians of fives < < < < < < < π 5 group 34
Size of Sublist ? π β ? π β A 35
Size of Sublist π 5 groups each of size 5 π β is larger than half of them π β is larger than π 10 groups π β is larger than at least 3 elements in each group Similarly, π β is less than at least 3 elements in each group Size of each of the two sublists is [ 3 10 π, 7 10 π] Worst-case scenario, we prune 3n/10 elements and recursively process 7n/10 36
Recurrence Relation π ; π β€ 5 π π = α π 7π π 5 + π 10 + ππ ; π > 5 Can we apply the Master theorem? Letβs try recursive tree expansion 37
Recurrence Tree π π 38
Recurrence Tree ππ π π π 7π 5 10 39
Recurrence Tree ππ ππ 7ππ 5 10 π π 7π π 7π π 49π π 25 50 50 100 40
Recurrence Tree ππ ππ ππ 7ππ 9π 5 10 10 π π 7π π 7π π 49π 81π π 25 50 50 100 100 41
Running Time π π 9 9 π=π π=π π π = Ο π=0 π = π Ο π=0 10 10 We do not know the depth π of the tree π 9 1 π=β π π β€ π Ο π=0 β€ π β€ 10π 1β 9 10 10 π π = Ξ π 42
Proof by Induction We want to prove that π π = π π π π β€ ππ , for π > 0 and π β₯ π 0 Base case: π π = π for π β€ 5 Setting π β₯ π satisfies the base case Assume that π π β€ ππ is true for all π β€ π We want to prove that it is true for π = π + 1 π+1 7 π+1 π π + 1 = π + π + π π + 1 5 10 π+1 7 π+1 π π + 1 β€ π + π + π π + 1 5 10 43
Proof by Induction We want to prove that π π + 1 + π 7 π + 1 + π π + 1 β€ π π + 1 5 10 5 + 7π π 10 + π β€ π π 10 β₯ π π β₯ 10π By setting π β₯ 10π , the inequality π π + 1 β€ π π + 1 will be true π π = π π 44
Recommend
More recommend