cs4102 algorithms
play

CS4102 Algorithms Fall 2020 Warm up 2 What is ? =0 Finite - PowerPoint PPT Presentation

CS4102 Algorithms Fall 2020 Warm up 2 What is ? =0 Finite Geometric Series =0 If > 1 = The series The next term The series The first term multiplied by in the series 1 + + 2


  1. CS4102 Algorithms Fall 2020 Warm up 𝑙 2 𝑗 What is ? 𝑗=0

  2. 𝑀 𝑏 𝑗 Finite Geometric Series 𝑗=0 If 𝑏 > 1 βˆ’ βˆ’ = The series The next term The series The first term multiplied by 𝑏 in the series 1 + 𝑏 + 𝑏 2 + β‹― + 𝑏 𝑀 𝑏 1 + 𝑏 + 𝑏 2 + β‹― + 𝑏 𝑀 1 𝑏 𝑀+1 1 2

  3. 𝑀 𝑏 𝑗 Finite Geometric Series 𝑗=0 If 𝑏 < 1 βˆ’ βˆ’ = The series The next term The series The first term multiplied by 𝑏 in the series 1 + 𝑏 + 𝑏 2 + β‹― + 𝑏 𝑀 𝑏 1 + 𝑏 + 𝑏 2 + β‹― + 𝑏 𝑀 1 𝑏 𝑀+1 1 3 Solve for the series

  4. CS4102 Algorithms Fall 2020 Another Warm up 1) Rewrite log 10 π‘œ so the log on π‘œ has base 2 2) Rewrite 𝑏 log 𝑐 π‘œ so that π‘œ is not in the exponent

  5. 1) Rewrite log 10 π‘œ so the log on π‘œ has base 2 Log Rules Rewriting 1. log 𝑐 𝑦 + log 𝑐 𝑧 = log 𝑐 𝑦 β‹… 𝑧 1. log 𝑐 π‘œ 2. y β‹… log 𝑐 𝑦 = log 𝑐 𝑦 𝑧 2. log 𝑐 a log 𝑏 π‘œ (rule 3) 3. 𝑐 log 𝑐 𝑦 = 𝑦 3. log 𝑏 π‘œ β‹… log 𝑐 𝑏 (rule 2) 4. log 𝑐 𝑐 = 1 5. log 𝑐 1 = 0 log 𝑐 𝑏 is a constant! (doesn’t depend on π‘œ ) log 𝑐 π‘œ = Θ(log 𝑏 π‘œ)

  6. 2) Rewrite 𝑏 log 𝑐 π‘œ so that π‘œ is not in the exponent Log Rules Rewriting 1. 𝑏 log 𝑐 π‘œ 1. log 𝑐 𝑦 + log 𝑐 𝑧 = log 𝑐 𝑦 β‹… 𝑧 2. y β‹… log 𝑐 𝑦 = log 𝑐 𝑦 𝑧 2. 𝑏 log 𝑐 𝑏 log𝑏 π‘œ (rule 3) 3. 𝑐 log 𝑐 𝑦 = 𝑦 3. 𝑏 (log 𝑏 n)β‹…(log 𝑐 𝑏) (rule 2) 4. log 𝑐 𝑐 = 1 𝑏 log 𝑏 π‘œ (log 𝑐 𝑏) 4. (exponentiation rule) 5. log 𝑐 1 = 0 5. π‘œ log 𝑐 𝑏 (rule 3)

  7. Divide and Conquer β€’ Divide: – Break the problem into multiple subproblems, each smaller instances of the original β€’ Conquer: – If the suproblems are β€œlarge”: β€’ Solve each subproblem recursively – If the subproblems are β€œsmall”: β€’ Solve them directly (base case) β€’ Combine: – Merge together solutions to subproblems

  8. Analyzing Divide and Conquer 1. Break into smaller subproblems 2. Use recurrence relation to express recursive running time 3. Use asymptotic notation to simplify β€’ Divide: 𝐸(π‘œ) time, β€’ Conquer: recurse on small problems, size 𝑑 β€’ Combine: C(π‘œ) time β€’ Recurrence: – π‘ˆ π‘œ = 𝐸 π‘œ + π‘ˆ(𝑑) + 𝐷(π‘œ)

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

  10. Analyzing Merge Sort 1. Break into smaller subproblems 2. Use recurrence relation to express recursive running time 3. Use asymptotic notation to simplify β€’ Divide: 0 comparisons β€’ Conquer: recursively solve 2 small subproblems, size π‘œ 2 β€’ Combine: π‘œ comparisons β€’ Recurrence: π‘œ – π‘ˆ π‘œ = 2 π‘ˆ 2 + π‘œ 10

  11. Recurrence Solving Techniques 1. Draw the recursive structure 2. Label work done within each recursive call Tree 3. Sum work done per recursive depth 4. Find pattern ? 5. Sum work done total Guess/Check β€œ Cookbook ” 11

  12. Tree method π‘ˆ π‘œ = 2π‘ˆ π‘œ Size of the list 2 + π‘œ β€œnon - recursive” work done π‘œ A call to π‘œ mergesort οƒžο€  π‘œ total / level Recursive Calls π‘œ π‘œ π‘œ 2 π‘œ 2 2 2 log 2 π‘œ levels π‘œ π‘œ π‘œ π‘œ π‘œ 4 π‘œ 4 π‘œ 4 π‘œ 4 4 4 4 4 of recursion … … … … … 1 1 1 1 1 1 1 1 1 1 1 1 log 2 π‘œ π‘ˆ π‘œ = π‘œ = π‘œ log 2 π‘œ Base Cases! 𝑗=1 12

  13. Multiplication β€’ Want to multiply large numbers together 4 1 0 2 π‘œ -digit numbers Γ— 1 8 1 9 β€’ What makes a β€œgood” algorithm? β€’ How do we measure input size? β€’ What do we β€œcount” for run time? 13

  14. β€œSchoolbook” Method How many total 4 1 0 2 multiplications? π‘œ -digit numbers Γ— 1 8 1 9 3 6 9 1 8 π‘œ mults 4 1 0 2 π‘œ mults π‘œ levels 3 2 8 1 6 π‘œ mults β‡’ Θ(π‘œ 2 ) + 4 1 0 2 π‘œ mults 7 4 6 1 5 3 8 14

  15. Divide and Conquer method 1. Break into smaller subproblems π‘œ = 10 4 1 0 2 2 + a b a b π‘œ = 10 Γ— 1 8 1 9 2 c d + c d 10 π‘œ Γ— ( ) + a c π‘œ 10 Γ— Γ— 2 + ( ) + a d b c Γ— ( ) b d 15

  16. D&C Multiplication Pseudocode def dc_mult(x, y): What’s missing?! n = length of larger of x,y a = first n/2 digits of x b = last n/2 digits of x c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) ad = dc_mult(a, d) bc = dc_mult(b, c) bd = dc_mult(b, d) return ac*10^n + (ad + bc)*10^(n/2) + bd

  17. D&C Multiplication Pseudocode def dc_mult(x, y): n = length of larger of x,y if n == 1: return x*y a = first n/2 digits of x b = last n/2 digits of x Divide c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) ad = dc_mult(a, d) Conquer bc = dc_mult(b, c) bd = dc_mult(b, d) return ac*10^n + (ad + bc)*10^(n/2) + bd Combine

  18. Divide and Conquer Multiplication β€’ Divide: – Break π‘œ -digit numbers into four numbers of π‘œ 2 digits each (call them 𝑏 , 𝑐 , 𝑑 , 𝑒 ) β€’ Conquer: – If π‘œ > 1 : β€’ Recursively compute 𝑏𝑑 , 𝑏𝑒 , 𝑐𝑑 , 𝑐𝑒 – If π‘œ = 1 : (i.e. one digit each) β€’ Compute 𝑏𝑑 , 𝑏𝑒 , 𝑐𝑑 , 𝑐𝑒 directly (base case) β€’ Combine: π‘œ 10 π‘œ 𝑏𝑑 + 10 2 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 18

  19. Divide and Conquer method How much total arithmetic? 2. Use recurrence relation to express recursive running time π‘œ 10 π‘œ 𝑏𝑑 + 10 2 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 Recursively solve π‘ˆ π‘œ = 4π‘ˆ π‘œ 2 + 5π‘œ 19

  20. Divide and Conquer method log 2 π‘œ π‘ˆ π‘œ = 5π‘œ 2 𝑗 3. Use asymptotic notation to simplify π‘ˆ π‘œ = 4π‘ˆ π‘œ 2 + 5π‘œ 𝑗=0 5π‘œ 5π‘œ π‘œ π‘œ π‘œ π‘œ π‘œ 4 5π‘œ 5π‘œ 5π‘œ 5π‘œ 2 β‹… 5π‘œ 2 2 2 2 2 2 2 2 π‘œ π‘œ π‘œ π‘œ π‘œ π‘œ π‘œ π‘œ 16 … 5π‘œ 5π‘œ 5π‘œ 5π‘œ 5π‘œ 5π‘œ 5π‘œ 5π‘œ 4 β‹… 5π‘œ 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 … … … … … … … … … 2 log 2 π‘œ β‹… 5π‘œ … 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20

  21. Divide and Conquer method 3. Use asymptotic notation to simplify π‘ˆ π‘œ = 4π‘ˆ π‘œ 2 + 5π‘œ log 2 π‘œ π‘ˆ π‘œ = 5π‘œ 2 𝑗 𝑗=0 π‘ˆ π‘œ = 5π‘œ 2 log 2 π‘œ+1 βˆ’ 1 2 βˆ’ 1 π‘ˆ π‘œ = 5π‘œ(2π‘œ βˆ’ 1) = Θ(π‘œ 2 ) 21

  22. Karatsuba 1. Break into smaller subproblems π‘œ = 10 4 1 0 2 2 + a b a b π‘œ = 10 Γ— 1 8 1 9 2 c d + c d 10 π‘œ Γ— ( ) + a c π‘œ 10 Γ— Γ— 2 + ( ) + a d b c Γ— ( ) b d 22

  23. a b Karatsuba Γ— c d π‘œ 10 π‘œ 𝑏𝑑 + 10 2 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 This can be Can’t avoid these simplified 𝑏 + 𝑐 𝑑 + 𝑒 = 𝑏𝑑 + 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 𝑏𝑒 + 𝑐𝑑 = 𝑏 + 𝑐 𝑑 + 𝑒 βˆ’ 𝑏𝑑 βˆ’ 𝑐𝑒 Two One multiplication multiplications 23

  24. Karatsuba Pseudocode def dc_mult(x, y): n = length of larger of x,y if n == 1: return x*y a = first n/2 digits of x b = last n/2 digits of x Divide c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) bd = dc_mult(b, d) Conquer adbc = dc_mult(a+b, c+d) – ac – bd return ac*10^n + (adbc)*10^(n/2) + bd Combine

  25. a b How much total Karatsuba Γ— c d arithmetic? 2. Use recurrence relation to express recursive running time π‘œ 10 π‘œ 𝑏𝑑 + 10 𝑏 + 𝑐 𝑑 + 𝑒 βˆ’ 𝑏𝑑 βˆ’ 𝑐𝑒 + 𝑐𝑒 2 Recursively solve π‘ˆ π‘œ = 3π‘ˆ π‘œ 2 + 8π‘œ 25

  26. Karatsuba β€’ Divide: – Break π‘œ -digit numbers into four numbers of π‘œ 2 digits each (call them 𝑏 , 𝑐 , 𝑑 , 𝑒 ) β€’ Conquer: – If π‘œ > 1 : β€’ Recursively compute 𝑏𝑑 , 𝑐𝑒 , 𝑏 + 𝑐 𝑑 + 𝑒 – If π‘œ = 1 : β€’ Compute 𝑏𝑑 , 𝑐𝑒 , 𝑏 + 𝑐 𝑑 + 𝑒 directly (base case) β€’ Combine: π‘œ – 10 π‘œ 𝑏𝑑 + 10 𝑏 + 𝑐 𝑑 + 𝑒 βˆ’ 𝑏𝑑 βˆ’ 𝑐𝑒 + 𝑐𝑒 2 26

  27. a b Karatsuba Algorithm Γ— c d 1.Recursively compute: 𝑏𝑑 , 𝑐𝑒 , (𝑏 + 𝑐)(𝑑 + 𝑒) 2. 𝑏𝑒 + 𝑐𝑑 = 𝑏 + 𝑐 𝑑 + 𝑒 βˆ’ 𝑏𝑑 βˆ’ 𝑐𝑒 π‘œ 3. Return 10 π‘œ 𝑏𝑑 + 10 2 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 π‘ˆ π‘œ = 3π‘ˆ π‘œ 2 + 8π‘œ Pseudocode 1. 𝑦 ← Karatsuba(𝑏, 𝑑) 2. 𝑧 ← Karatsuba(𝑐, 𝑒) 3. 𝑨 ← Karatsuba(𝑏 + 𝑐, 𝑑 + 𝑒) βˆ’ 𝑦 βˆ’ 𝑧 4. Return 10 π‘œ 𝑦 + 10 π‘œ 2 𝑨 + 𝑧 27

  28. Karatsuba log 2 π‘œ π‘ˆ π‘œ = 8π‘œ (3 2 ) 𝑗 3. Use asymptotic notation to simplify π‘ˆ π‘œ = 3π‘ˆ π‘œ 2 + 8π‘œ 𝑗=0 8π‘œ π‘œ 8π‘œ β‹… 1 π‘œ π‘œ π‘œ 8π‘œ β‹… 3 8π‘œ 8π‘œ 8π‘œ 2 2 2 2 2 2 2 π‘œ π‘œ π‘œ π‘œ π‘œ π‘œ 8π‘œ β‹… 9 … 8π‘œ 8π‘œ 8π‘œ 8π‘œ 8π‘œ 8π‘œ 4 4 4 4 4 4 4 4 4 4 4 4 4 … … … … … … … 8π‘œ β‹… 3 log 2 π‘œ … 8 8 8 8 8 8 8 8 8 8 1 1 1 1 1 1 2 log 2 π‘œ 1 1 1 1 28

  29. Karatsuba 3. Use asymptotic notation to simplify π‘ˆ π‘œ = 3π‘ˆ π‘œ 2 + 8π‘œ log 2 π‘œ π‘ˆ π‘œ = 8π‘œ (3 2 ) 𝑗 𝑗=0 (3 2 ) log 2 π‘œ+1 βˆ’1 π‘ˆ π‘œ = 8π‘œ 3 2 βˆ’ 1 Lots of arithmetic (see pdf) π‘ˆ π‘œ = 24 π‘œ log 2 3 βˆ’ 16π‘œ = Θ(π‘œ log 2 3 ) β‰ˆ Θ(π‘œ 1.585 ) 29

  30. π‘œ 2 π‘œ 1.585 30

  31. Recurrence Solving Techniques Tree ? Guess/Check (induction) 1. Use Tree method to make a guess at the asymptotic running time β€œ Cookbook ” 2. Select a specific function with that asymptotic running time 3. Use induction to show the specific function is an upper/lower bound on the actual running time 31

Recommend


More recommend