divide and conquer
play

Divide and Conquer Chapter 4 1 Integer Multiplication 2 Integer - PowerPoint PPT Presentation

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


  1. Divide and Conquer Chapter 4 1

  2. Integer Multiplication 2

  3. 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

  4. 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

  5. D&C Integer Multiplication 𝑦 𝑦 1 𝑦 2 𝑧 𝑧 1 𝑧 2 π‘œ π‘œ 2 𝑐𝑗𝑒𝑑 2 𝑐𝑗𝑒𝑑 5

  6. 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

  7. 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

  8. 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

  9. Matrix Multiplication Section 4.2 9

  10. Matrix Multiplication = . 𝐷 = 𝐡. 𝐢 𝑙=π‘œ 𝑏 𝑗𝑙 . 𝑐 π‘™π‘˜ , assuming A, B, and C are 𝑑 π‘—π‘˜ = Οƒ 𝑙=1 square π‘œ Γ— π‘œ matrices 10

  11. Simple Matrix Multiplication n n n π‘ˆ π‘œ = Θ π‘œ 3 11

  12. 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

  13. D&C Matrix Multiplication 13

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Linear-time Selection Section 9.3 19

  20. 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

  21. Quick-sort-like Algorithm 𝑙 𝐡 p 1 21

  22. Quick-sort-like Algorithm 𝑙 𝐡 p 1 p 2 22

  23. Quick-sort-like Algorithm 𝑙 𝐡 p 1 p 2 p 3 23

  24. Quick-sort-like Algorithm 𝑙 𝐡 p 1 p 2 p 3 p 4 24

  25. Quick-sort-like Algorithm 𝑙 𝐡 p 1 p 2 p 3 π‘ˆ π‘œ = Θ π‘œ 2 p 4 βœ“ How to choose a good pivot? 25

  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} 26

  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} 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

  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} 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

  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} 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

  30. 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

  31. 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

  32. Size of Sublist 𝑁 𝑛 βˆ— medians of fives π‘œ 5 group 32

  33. Size of Sublist 𝑁 < < 𝑛 βˆ— medians of fives π‘œ 5 group 33

  34. Size of Sublist < < < < < < < 𝑁 < < 𝑛 βˆ— medians of fives < < < < < < < π‘œ 5 group 34

  35. Size of Sublist ? 𝑛 βˆ— ? 𝑛 βˆ— A 35

  36. 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

  37. Recurrence Relation 𝑏 ; π‘œ ≀ 5 π‘ˆ π‘œ = ቐ π‘œ 7π‘œ π‘ˆ 5 + π‘ˆ 10 + π‘π‘œ ; π‘œ > 5 Can we apply the Master theorem? Let’s try recursive tree expansion 37

  38. Recurrence Tree π‘ˆ π‘œ 38

  39. Recurrence Tree π‘π‘œ π‘ˆ π‘œ π‘ˆ 7π‘œ 5 10 39

  40. Recurrence Tree π‘π‘œ π‘π‘œ 7π‘π‘œ 5 10 π‘œ π‘ˆ 7π‘œ π‘ˆ 7π‘œ π‘ˆ 49π‘œ π‘ˆ 25 50 50 100 40

  41. Recurrence Tree π‘π‘œ π‘π‘œ π‘π‘œ 7π‘π‘œ 9π‘œ 5 10 10 π‘œ π‘ˆ 7π‘œ π‘ˆ 7π‘œ π‘ˆ 49π‘œ 81π‘œ π‘ˆ 25 50 50 100 100 41

  42. 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

  43. 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

  44. 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