cs4102 algorithms
play

CS4102 Algorithms Fall 2020 Warm up Simplify: 1 + 2 + 3 + + ( 1) - PowerPoint PPT Presentation

CS4102 Algorithms Fall 2020 Warm up Simplify: 1 + 2 + 3 + + ( 1) + = 1 + 1 1 + 2 + 3 + + ( 1) + = 2 + 1 2 Matrix Multiplication 1 2 3 2 4 6 4 5 6 8 10 12 7 8 9


  1. CS4102 Algorithms Fall 2020 Warm up Simplify: 1 + 2 + 3 + β‹― + (π‘œ βˆ’ 1) + π‘œ = 1

  2. π‘œ π‘œ + 1 1 + 2 + 3 + β‹― + (π‘œ βˆ’ 1) + π‘œ = 2 π‘œ π‘œ + 1 2

  3. Matrix Multiplication π‘œ 1 2 3 2 4 6 π‘œ Γ— 4 5 6 8 10 12 7 8 9 14 16 18 2 + 16 + 42 4 + 20 + 48 6 + 24 + 54 = β‹… β‹… β‹… β‹… β‹… β‹… 60 72 84 = 132 162 192 204 252 300 Run time? 𝑃(π‘œ 3 ) 3

  4. Matrix Multiplication D&C Multiply π‘œ Γ— π‘œ matrices ( 𝐡 and 𝐢 ) Divide: 𝑏 1 𝑏 2 𝑏 3 𝑏 4 𝑐 1 𝑐 2 𝑐 3 𝑐 4 𝑏 5 𝑏 6 𝑏 7 𝑏 8 𝑐 5 𝑐 6 𝑐 7 𝑐 8 𝐡 = 𝐢 = 𝑏 9 𝑏 10 𝑏 11 𝑏 12 𝑐 9 𝑐 10 𝑐 11 𝑐 12 𝑏 13 𝑏 14 𝑏 15 𝑏 16 𝑐 13 𝑐 14 𝑐 15 𝑐 16 4

  5. Matrix Multiplication D&C Multiply π‘œ Γ— π‘œ matrices ( 𝐡 and 𝐢 ) 𝑏 1 𝑏 2 𝑏 3 𝑏 4 𝑐 1 𝑐 2 𝑐 3 𝑐 4 𝐡 1,1 𝐡 1,2 𝐢 1,1 𝐢 1,2 𝑏 5 𝑏 6 𝑏 7 𝑏 8 𝑐 5 𝑐 6 𝑐 7 𝑐 8 𝐡 = 𝐢 = 𝑏 9 𝑏 10 𝑏 11 𝑏 12 𝑐 9 𝑐 10 𝑐 11 𝑐 12 𝐡 2,1 𝐡 2,2 𝐢 2,1 𝐢 2,2 𝑏 13 𝑏 14 𝑏 15 𝑏 16 𝑐 13 𝑐 14 𝑐 15 𝑐 16 Combine: 𝐡𝐢 = 𝐡 1,1 𝐢 1,1 + 𝐡 1,2 𝐢 2,1 𝐡 1,1 𝐢 1,2 + 𝐡 1,2 𝐢 2,2 𝐡 2,1 𝐢 1,2 + 𝐡 2,2 𝐢 2,2 𝐡 2,1 𝐢 1,1 + 𝐡 2,2 𝐢 2,1 π‘ˆ π‘œ = 8π‘ˆ π‘œ 2 + 4 π‘œ 2 Cost of Run time? 2 additions 5

  6. Matrix Multiplication D&C π‘ˆ π‘œ = 8π‘ˆ π‘œ 2 + 4 π‘œ 2 2 π‘ˆ π‘œ = 8π‘ˆ π‘œ 2 + Θ(π‘œ 2 ) 𝑏 = 8, 𝑐 = 2, 𝑔 π‘œ = π‘œ 2 Case 1! π‘œ log 𝑐 𝑏 = π‘œ log 2 8 = π‘œ 3 π‘ˆ π‘œ = Θ(π‘œ 3 ) We can do better… 6

  7. Matrix Multiplication D&C Multiply π‘œ Γ— π‘œ matrices ( 𝐡 and 𝐢 ) 𝑏 1 𝑏 2 𝑏 3 𝑏 4 𝑐 1 𝑐 2 𝑐 3 𝑐 4 𝐡 1,1 𝐡 1,2 𝐢 1,1 𝐢 1,2 𝑏 5 𝑏 6 𝑏 7 𝑏 8 𝑐 5 𝑐 6 𝑐 7 𝑐 8 𝐡 = 𝐢 = 𝑏 9 𝑏 10 𝑏 11 𝑏 12 𝑐 9 𝑐 10 𝑐 11 𝑐 12 𝐡 2,1 𝐡 2,2 𝐢 2,1 𝐢 2,2 𝑏 13 𝑏 14 𝑏 15 𝑏 16 𝑐 13 𝑐 14 𝑐 15 𝑐 16 𝐡𝐢 = 𝐡 1,1 𝐢 1,1 + 𝐡 1,2 𝐢 2,1 𝐡 1,1 𝐢 1,2 + 𝐡 1,2 𝐢 2,2 𝐡 2,1 𝐢 1,2 + 𝐡 2,2 𝐢 2,2 𝐡 2,1 𝐢 1,1 + 𝐡 2,2 𝐢 2,1 Idea: Use a Karatsuba-like technique on this 7

  8. Strassen’s Algorithm Multiply π‘œ Γ— π‘œ matrices ( 𝐡 and 𝐢 ) 𝑏 1 𝑏 2 𝑏 3 𝑏 4 𝑐 1 𝑐 2 𝑐 3 𝑐 4 𝐡 1,1 𝐡 1,2 𝐢 1,1 𝐢 1,2 𝑏 5 𝑏 6 𝑏 7 𝑏 8 𝑐 5 𝑐 6 𝑐 7 𝑐 8 𝐡 = 𝐢 = 𝑏 9 𝑏 10 𝑏 11 𝑏 12 𝑐 9 𝑐 10 𝑐 11 𝑐 12 𝐡 2,1 𝐡 2,2 𝐢 2,1 𝐢 2,2 𝑏 13 𝑏 14 𝑏 15 𝑏 16 𝑐 13 𝑐 14 𝑐 15 𝑐 16 Find 𝐡𝐢 : Calculate: 𝑅 1 + 𝑅 4 βˆ’ 𝑅 5 + 𝑅 7 𝑅 3 + 𝑅 5 𝑅 1 = 𝐡 1,1 + 𝐡 2,2 (𝐢 1,1 + 𝐢 2,2 ) 𝑅 1 βˆ’ 𝑅 2 + 𝑅 3 + 𝑅 6 𝑅 2 + 𝑅 4 𝑅 2 = 𝐡 2,1 + 𝐡 2,2 𝐢 1,1 𝑅 3 = 𝐡 1,1 (𝐢 1,2 βˆ’ 𝐢 2,2 ) 𝐡 1,1 𝐢 1,1 + 𝐡 1,2 𝐢 2,1 𝐡 1,1 𝐢 1,2 + 𝐡 1,2 𝐢 2,2 𝑅 4 = 𝐡 2,2 (𝐢 2,1 βˆ’ 𝐢 1,1 ) 𝐡 2,1 𝐢 1,2 + 𝐡 2,2 𝐢 2,2 𝐡 2,1 𝐢 1,1 + 𝐡 2,2 𝐢 2,1 𝑅 5 = 𝐡 1,1 + 𝐡 1,2 𝐢 2,2 𝑅 6 = 𝐡 2,1 βˆ’ 𝐡 1,1 (𝐢 1,1 + 𝐢 1,2 ) Number Mults.: 7 Number Adds.: 18 π‘ˆ π‘œ = 7π‘ˆ π‘œ 2 + 9 𝑅 7 = 𝐡 1,2 βˆ’ 𝐡 2,2 (𝐢 2,1 + 𝐢 2,2 ) 2 π‘œ 2 8

  9. Strassen’s Algorithm π‘ˆ π‘œ = 7π‘ˆ π‘œ 2 + 9 2 π‘œ 2 𝑏 = 7, 𝑐 = 2, 𝑔 π‘œ = 9 2 π‘œ 2 Case 1! π‘œ log 𝑐 𝑏 = π‘œ log 2 7 β‰ˆ π‘œ 2.807 π‘ˆ π‘œ = Θ π‘œ log 2 7 β‰ˆ Θ(π‘œ 2.807 ) 9

  10. Strassen’s Algorithm π‘œ 3 π‘œ log 2 7 10

  11. Is this the fastest? Best possible is unknown May not even exist! 11

  12. Recurrence Solving Techniques Tree ? Guess/Check β€œ Cookbook ” Substitution 12

  13. Observation β€’ Divide: 𝐸(π‘œ) time, β€’ Conquer: recurse on small problems, size 𝑑 β€’ Combine: C(π‘œ) time β€’ Recurrence: π‘ˆ π‘œ = 𝐸 π‘œ + π‘ˆ(𝑑) + 𝐷(π‘œ) β€’ Many D&C recurrences are of form: π‘ˆ π‘œ = π‘π‘ˆ π‘œ 𝑐 + 𝑔(π‘œ) 13

  14. Master Theorem π‘ˆ π‘œ = π‘π‘ˆ π‘œ 𝑐 + 𝑔(π‘œ) β€’ Case 1: if 𝑔 π‘œ = 𝑃(π‘œ log 𝑐 𝑏 βˆ’πœ ) for some constant 𝜁 > 0 , then π‘ˆ π‘œ = Θ(π‘œ log 𝑐 𝑏 ) β€’ Case 2: if 𝑔 π‘œ = Θ(π‘œ log 𝑐 𝑏 ) , then π‘ˆ π‘œ = Θ(π‘œ log 𝑐 𝑏 log π‘œ) β€’ Case 3: if 𝑔 π‘œ = Ξ©(π‘œ log 𝑐 𝑏+𝜁 ) for some constant 𝜁 > 0 , and if π‘œ 𝑏𝑔 ≀ 𝑑𝑔(π‘œ) for some constant 𝑑 < 1 and all sufficiently large 𝑐 π‘œ , then π‘ˆ π‘œ = Θ(𝑔 π‘œ ) 14

  15. Substitution Method β€’ Idea: take a β€œdifficult” recurrence, re -express it such that one of our other methods applies. β€’ Example: π‘ˆ π‘œ = 2π‘ˆ π‘œ + log 2 π‘œ 15

  16. Tree method log 2 π‘œ 1/2 = 1 2 log 2 π‘œ π‘ˆ π‘œ = 2π‘ˆ( π‘œ) + log 2 π‘œ log 2 π‘œ log 2 π‘œ π‘œ 1 1 log 2 π‘œ π‘œ π‘œ + 2 log 2 π‘œ 2 log 2 π‘œ 1 log 2 log 2 π‘œ 1 1 1 4 log 2 π‘œ 4 log 2 π‘œ 4 log 2 π‘œ 4 log 2 π‘œ log 2 π‘œ + + + π‘œ π‘œ π‘œ π‘œ … … … … 2 … 1 1 1 1 1 1 log 2 π‘œ 2 2 2 2 2 + + + + + π‘ˆ π‘œ = 𝑃(log 2 π‘œ β‹… log 2 log 2 π‘œ) 16

  17. Substitution Method π‘ˆ π‘œ = 2π‘ˆ π‘œ + log 2 π‘œ I don’t like the Β½ in π‘ˆ π‘œ = 2π‘ˆ π‘œ 1/2 + log 2 π‘œ the exponent Let π‘œ = 2 𝑛 , i.e. 𝑛 = log 2 π‘œ Now the variable is in the exponent on both sides! 𝑛 π‘ˆ 2 𝑛 = 2π‘ˆ 2 + 𝑛 Rewrite in terms of exponent! 2 𝑛 Let 𝑇 𝑛 = 2𝑇 + 𝑛 S will operate exactly as T, just Case 2! 2 redefined in terms of the exponent Let 𝑇 𝑛 = Θ(𝑛 log 𝑛) Substitute Back 𝑇 𝑛 = π‘ˆ(2 𝑛 ) Let T π‘œ = Θ(log π‘œ log log π‘œ) 17

  18. Tree method 𝑛 π‘ˆ 2 𝑛 = 2π‘ˆ 2 π‘ˆ π‘œ = 2π‘ˆ( π‘œ) + log 2 π‘œ π‘œ = 2 𝑛 + 𝑛 2 log 2 π‘œ log 2 π‘œ π‘œ 2 𝑛 1 1 log 2 π‘œ π‘œ π‘œ + 2 log 2 π‘œ 2 𝑛/2 2 log 2 π‘œ 2 𝑛/2 1 1 1 1 4 log 2 π‘œ 4 log 2 π‘œ 4 log 2 π‘œ log 2 log 2 π‘œ 4 log 2 π‘œ log 2 π‘œ + + + 2 𝑛/4 2 𝑛/4 2 𝑛/4 2 𝑛/4 π‘œ π‘œ π‘œ π‘œ … … … … 2 … 1 1 1 1 1 1 log 2 π‘œ 2 2 2 2 2 + + + + + 18

  19. Tree method π‘œ = 2 𝑛 π‘ˆ 2 𝑛 = 2π‘ˆ(2 𝑛/2 ) + 𝑛 𝑛 𝑛 2 𝑛 𝑛 𝑛 2 𝑛/2 2 𝑛/2 𝑛 + 2 2 log 2 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 + + 2 𝑛/4 4 2 𝑛/4 4 2 𝑛/4 4 2 𝑛/4 4 + … … … … 2 1 … 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 𝑛 + + + + + 19

  20. Tree method 𝑇 𝑛 = 2𝑇 𝑛 π‘œ = 2 𝑛 + 𝑛 π‘ˆ 2 𝑛 = 𝑇(𝑛) 2 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛/2 𝑛/2 + 2 2 log 2 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 + + 4 4 4 4 + 𝑛/4 𝑛/4 𝑛/4 𝑛/4 … … … … 1 … 1 1 1 1 1 1 𝑛 1 1 1 1 1 + + + + + π‘ˆ π‘œ = 𝑃(𝑛 β‹… log 2 𝑛) = 𝑃(log 2 π‘œ β‹… log 2 log 2 π‘œ) 20

Recommend


More recommend