cse101 algorithm design and analysis

CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy - PowerPoint PPT Presentation

CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones) Week-06 Lecture 22: Divide and Conquer (Master Theorem) Master Theorem How do you solve a recurrence of the form


  1. CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones) Week-06 Lecture 22: Divide and Conquer (Master Theorem)

  2. Master Theorem β€’ How do you solve a recurrence of the form π‘ˆ π‘œ = π‘π‘ˆ π‘œ 𝑐 + 𝑃 π‘œ ! We will use the master theorem.

  3. Summation Lemma Consider the summation $ 𝑠 ! ! !"# It behaves differently for different values of 𝑠 .

  4. Summation Lemma Consider the summation $ 𝑠 ! ! !"# It behaves differently for different values of 𝑠 . If 𝑠 < 1 then this sum converges. This means that the sum is bounded above by some constant 𝑑 . Therefore $ $ 𝑠 ! < 𝑑 𝑔𝑝𝑠 π‘π‘šπ‘š π‘œ 𝑑𝑝 ! 𝑠 ! Ο΅ 𝑃(1) 𝑗𝑔 𝑠 < 1, π‘’β„Žπ‘“π‘œ ! !"# !"#

  5. Summation Lemma Consider the summation $ 𝑠 ! ! !"# It behaves differently for different values of 𝑠 . If 𝑠 = 1 then this sum is just summing 1 over and over n times. Therefore $ $ 𝑠 ! = ! 𝑗𝑔 𝑠 = 1, π‘’β„Žπ‘“π‘œ ! 1 = π‘œ + 1 Ο΅ 𝑃(π‘œ) !"# !"#

  6. Summation Lemma Consider the summation $ 𝑠 ! ! !"# It behaves differently for different values of 𝑠 . If 𝑠 > 1 then this sum is exponential with base 𝑠 . $ $ 𝑠 𝑠 ! < 𝑑𝑠 $ 𝑔𝑝𝑠 π‘π‘šπ‘š π‘œ, 𝑠 ! Ο΅ 𝑃 𝑠 $ 𝑗𝑔 𝑠 > 1, π‘’β„Žπ‘“π‘œ ! 𝑑𝑝 ! 𝑑 > 𝑠 βˆ’ 1 !"# !"#

  7. Summation Lemma Consider the summation $ 𝑠 ! ! !"# It behaves differently for different values of 𝑠 . 𝑃 1 𝑗𝑔 𝑠 < 1 $ 𝑠 ! Ο΅ 9 ! 𝑃 π‘œ 𝑗𝑔 𝑠 = 1 𝑃 𝑠 $ 𝑗𝑔 𝑠 > 1 !"#

  8. Master Theorem Master Theorem: If π‘ˆ(π‘œ) = π‘π‘ˆ(π‘œ/𝑐) + 𝑃(π‘œ : ) for some constants 𝑏 > 0, 𝑐 > 1, 𝑒 β‰₯ 0 , Then 𝑃 π‘œ : 𝑗𝑔 𝑏 < 𝑐 : 𝑃 π‘œ : log π‘œ 𝑗𝑔 𝑏 = 𝑐 : π‘ˆ π‘œ Ο΅ 𝑃 π‘œ ;<= ! > 𝑗𝑔 𝑏 > 𝑐 :

  9. Master Theorem: Solving the recurrence π‘ˆ(π‘œ) = π‘π‘ˆ(π‘œ/𝑐) + 𝑃(π‘œ : ) Size π‘œ 1 subproblem Size π‘œ/𝑐 𝑏 subproblems Size π‘œ/𝑐 % 𝑏 % subproblems Depth log & π‘œ Size 1 𝑏 '() ! $ subproblems

  10. Master Theorem: Solving the recurrence After 𝑙 levels, there are 𝑏 ! subproblems, each of size π‘œ/𝑐 ! . So, during the 𝑙 th level of recursion, the time complexity is * * $ 𝑏 ! = 𝑃 𝑏 ! $ 𝑃 & " & " 𝑏 ! = 𝑃 π‘œ * 𝑐 *

  11. Master Theorem: Solving the recurrence After 𝑙 levels, there are 𝑏 ! subproblems, each of size π‘œ/𝑐 ! . * * $ 𝑏 ! = 𝑃 𝑏 ! $ So, during the 𝑙 th level, the time complexity is 𝑃 & " & " ! 𝑏 = 𝑃 π‘œ * 𝑐 * After log & π‘œ levels, the subproblem size is reduced to 1, which usually is the size of the base case. So the entire algorithm is a sum of each level. '() ! $ 𝑏 ! π‘œ * ! π‘ˆ π‘œ = 𝑃 𝑐 * !"#

  12. Master Theorem: Proof &'( ! ) 𝑏 # π‘œ " & π‘ˆ π‘œ = 𝑃 𝑐 " #$% Case 1: 𝑏 < 𝑐 " * + " < 1 and the series converges to a constant so Then we have that π‘ˆ π‘œ = 𝑃 π‘œ "

  13. Master Theorem: Proof &'( ! ) 𝑏 # π‘œ " & π‘ˆ π‘œ = 𝑃 𝑐 " #$% Case 2: 𝑏 = 𝑐 " * + " = 1 and so each term is equal to 1 Then we have that π‘ˆ π‘œ = 𝑃 π‘œ " log + π‘œ

  14. Master Theorem: Proof &'( ! ) 𝑏 # π‘œ " & π‘ˆ π‘œ = 𝑃 𝑐 " #$% Case 2: 𝑏 > 𝑐 " Then the summation is exponential and grows proportional to its last term &'( ! ) * so + " 𝑏 &'( ! ) π‘ˆ π‘œ = 𝑃 π‘œ " = 𝑃 π‘œ &'( ! * 𝑐 "

  15. Master Theorem Theorem: If π‘ˆ(π‘œ) = π‘π‘ˆ(π‘œ/𝑐) + 𝑃(π‘œ : ) for some constants 𝑏 > 0, 𝑐 > 1, 𝑒 β‰₯ 0 , Then Top-heavy 𝑃 π‘œ : 𝑗𝑔 𝑏 < 𝑐 : 𝑃 π‘œ : log π‘œ Steady-state 𝑗𝑔 𝑏 = 𝑐 : π‘ˆ π‘œ Ο΅ Bottom-heavy 𝑃 π‘œ ;<= ! > 𝑗𝑔 𝑏 > 𝑐 :

  16. Master Theorem Applied to Multiply 𝑃 π‘œ * 𝑗𝑔 𝑏 < 𝑐 * 𝑃 π‘œ * log π‘œ 𝑗𝑔 𝑏 = 𝑐 * π‘ˆ π‘œ Ο΅ The recursion for the runtime of Multiply is 𝑃 π‘œ '() ! + 𝑗𝑔 𝑏 > 𝑐 * T(n) = 4T(n/2) + cn So we have that a=4, b=2, and d=1. In this case, 𝑏 > 𝑐 : so π‘ˆ π‘œ ϡ𝑃 π‘œ ;<= , F = 𝑃 π‘œ G Not any improvement of grade-school method.

  17. Master Theorem Applied to MultiplyKS 𝑃 π‘œ * 𝑗𝑔 𝑏 < 𝑐 * 𝑃 π‘œ * log π‘œ 𝑗𝑔 𝑏 = 𝑐 * π‘ˆ π‘œ Ο΅ The recursion for the runtime of Multiply is 𝑃 π‘œ '() ! + 𝑗𝑔 𝑏 > 𝑐 * T(n) = 3T(n/2) + cn So we have that a=3, b=2, and d=1. In this case, 𝑏 > 𝑐 : so π‘ˆ π‘œ ϡ𝑃 π‘œ ;<= , H = 𝑃 π‘œ I.KL An improvement on grade-school method!!!!!!

  18. Poll: What is the fastest known integer multiplication time? β€’ 𝑃 π‘œ /012 3 ) β€’ 𝑃(π‘œ π‘šπ‘π‘•π‘œ (log π‘šπ‘π‘•π‘œ) β€’ 𝑃(π‘œ π‘šπ‘π‘•π‘œ 2^{log βˆ— π‘œ}) β€’ 𝑃(π‘œ log π‘œ) β€’ O(n)

  19. Poll: What is the fastest known integer multiplication time? All have/will be correct β€’ 𝑃 π‘œ /012 Kuratsuba β€’ 𝑃(π‘œ π‘šπ‘π‘•π‘œ log log π‘œ ) Schonhage-Strassen, 1971 β€’ 𝑃(π‘œ π‘šπ‘π‘•π‘œ 2^{𝑑 log βˆ— π‘œ}) Furer, 2007 β€’ 𝑃(π‘œ log π‘œ) Harvey and van der Hoeven, 2019 β€’ O(n), you, tomorrow?

  20. Can we do better than π‘œ !.#$ ? β€’ Could any multiplication algorithm have a faster asymptotic runtime than π›ͺ π‘œ 5.78 ? β€’ Any ideas?????

  21. Can we do better than π‘œ !.#$ ? β€’ What if instead of splitting the number in half, we split it into thirds. β€’ x= x L x M x R β€’ y= y L y M y R

  22. Can we do better than π‘œ !.#$ ? β€’ What if instead of splitting the number in half, we split it into thirds. β€’ 𝑦 = 2 39/2 𝑦 ; + 2 9/2 𝑦 < + 𝑦 = β€’ 𝑧 = 2 39/2 𝑧 ; + 2 9/2 𝑧 < + 𝑧 =

  23. Multiplying trinomials β€’ 𝑏𝑦 3 + 𝑐𝑦 + 𝑑 𝑒𝑦 3 + 𝑓𝑦 + 𝑔

  24. Multiplying trinomials β€’ 𝑏𝑦 3 + 𝑐𝑦 + 𝑑 𝑒𝑦 3 + 𝑓𝑦 + 𝑔 = 𝑏𝑒𝑦 > + 𝑏𝑓 + 𝑐𝑒 𝑦 2 + 𝑏𝑔 + 𝑐𝑓 + 𝑑𝑒 𝑦 3 + 𝑐𝑔 + 𝑑𝑓 𝑦 + 𝑑𝑔 9 multiplications means 9 recursive calls. Each multiplication is 1/3 the size of the original.

  25. Multiplying trinomials β€’ 𝑏𝑦 G + 𝑐𝑦 + 𝑑 𝑒𝑦 G + 𝑓𝑦 + 𝑔 = 𝑏𝑒𝑦 F + 𝑏𝑓 + 𝑐𝑒 𝑦 H + 𝑏𝑔 + 𝑐𝑓 + 𝑑𝑒 𝑦 G + 𝑐𝑔 + 𝑑𝑓 𝑦 + 𝑑𝑔 9 multiplications means 9 recursive calls. Each multiplication is 1/3 the size of the original. π‘ˆ π‘œ = 9π‘ˆ π‘œ 3 + 𝑃(π‘œ)

  26. Multiplying trinomials β€’ 𝑏𝑦 G + 𝑐𝑦 + 𝑑 𝑒𝑦 G + 𝑓𝑦 + 𝑔 = 𝑏𝑒𝑦 F + 𝑏𝑓 + 𝑐𝑒 𝑦 H + 𝑏𝑔 + 𝑐𝑓 + 𝑑𝑒 𝑦 G + 𝑐𝑔 + 𝑑𝑓 𝑦 + 𝑑𝑔 π‘ˆ π‘œ = 9π‘ˆ π‘œ 3 + 𝑃(π‘œ) 9 > 3 ! a=9 𝑃 π‘œ * 𝑗𝑔 𝑏 < 𝑐 * π‘ˆ π‘œ = 𝑃 π‘œ "#$ M % b=3 𝑃 π‘œ * log π‘œ 𝑗𝑔 𝑏 = 𝑐 * π‘ˆ π‘œ Ο΅ π‘ˆ π‘œ = 𝑃 π‘œ & d=1 𝑃 π‘œ '() ! + 𝑗𝑔 𝑏 > 𝑐 *

  27. Multiplying trinomials β€’ 𝑏𝑦 G + 𝑐𝑦 + 𝑑 𝑒𝑦 G + 𝑓𝑦 + 𝑔 = 𝑏𝑒𝑦 F + 𝑏𝑓 + 𝑐𝑒 𝑦 H + 𝑏𝑔 + 𝑐𝑓 + 𝑑𝑒 𝑦 G + 𝑐𝑔 + 𝑑𝑓 𝑦 + 𝑑𝑔 β€’ There is a way to reduce from 9 multiplications down to just 5!!! β€’ Then the recursion becomes β€’ π‘ˆ π‘œ = 5π‘ˆ(π‘œ/3 ) + O(n) β€’ So by the master theorem

  28. Multiplying trinomials β€’ 𝑏𝑦 G + 𝑐𝑦 + 𝑑 𝑒𝑦 G + 𝑓𝑦 + 𝑔 = 𝑏𝑒𝑦 F + 𝑏𝑓 + 𝑐𝑒 𝑦 H + 𝑏𝑔 + 𝑐𝑓 + 𝑑𝑒 𝑦 G + 𝑐𝑔 + 𝑑𝑓 𝑦 + 𝑑𝑔 β€’ There is a way to reduce from 9 multiplications down to just 5!!! β€’ Then the recursion becomes β€’ π‘ˆ π‘œ = 5π‘ˆ(π‘œ/3 ) + O(n) β€’ So by the master theorem T(n)=O( π‘œ ;<= - K ) = 𝑃 π‘œ I.FH

  29. Dividing into k subproblems β€’ What happens if we divide into k subproblems each of size n/k. β€’ (𝑏 #./ 𝑦 #./ + 𝑏 #.0 𝑦 #.0 + β‹― 𝑏 / 𝑦 + 𝑏 % )(𝑐 #./ 𝑦 #./ + 𝑐 #.0 𝑦 #.0 + β‹― 𝑐 / 𝑦 + 𝑐 % ) β€’ How many terms are there? (multiplications.)

  30. Dividing into k subproblems β€’ What happens if we divide into k subproblems each of size n/k. β€’ (𝑏 !,- 𝑦 !,- + 𝑏 !,% 𝑦 !,% + β‹― 𝑏 - 𝑦 + 𝑏 # )(𝑐 !,- 𝑦 !,- + 𝑐 !,% 𝑦 !,% + β‹― 𝑐 - 𝑦 + 𝑐 # ) β€’ How many terms are there? (multiplications.) β€’ There are 𝑙 G multiplications. The recursion is π‘ˆ π‘œ = 𝑙 G π‘ˆ π‘œ 𝑙 + 𝑃 π‘œ … … … 𝑏 = 𝑙 G , 𝑐 = 𝑙, 𝑒 = 1 π‘ˆ π‘œ = 𝑃(π‘œ ;<= 1 N , ) = 𝑃 π‘œ G

  31. Cook-Toom algorithm β€’ In fact, if you split up your number into k equally sized parts, then you can combine them with 2k-1 multiplications instead of the 𝑙 3 individual multiplications. β€’ This means that you can get an algorithm that runs in β€’ π‘ˆ π‘œ = (2𝑙 βˆ’ 1)π‘ˆ(π‘œ/𝑙 ) + O(n)

  32. Cook-Toom algorithm β€’ In fact, if you split up your number into k equally sized parts, then you can combine them with 2k-1 multiplications instead of the 𝑙 G individual multiplications. β€’ This means that you can get an algorithm that runs in β€’ π‘ˆ π‘œ = (2𝑙 βˆ’ 1)π‘ˆ(π‘œ/𝑙 ) + O(n) 234(,167) β€’ π‘ˆ π‘œ = 𝑃 π‘œ time!!!! 234 1

Recommend


More recommend