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 π π = ππ π π + π π ! We will use the master theorem.
Summation Lemma Consider the summation $ π ! ! !"# It behaves differently for different values of π .
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, π’βππ ! !"# !"#
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 Ο΅ π(π) !"# !"#
Summation Lemma Consider the summation $ π ! ! !"# It behaves differently for different values of π . If π > 1 then this sum is exponential with base π . $ $ π π ! < ππ $ πππ πππ π, π ! Ο΅ π π $ ππ π > 1, π’βππ ! π‘π ! π > π β 1 !"# !"#
Summation Lemma Consider the summation $ π ! ! !"# It behaves differently for different values of π . π 1 ππ π < 1 $ π ! Ο΅ 9 ! π π ππ π = 1 π π $ ππ π > 1 !"#
Master Theorem Master Theorem: If π(π) = ππ(π/π) + π(π : ) for some constants π > 0, π > 1, π β₯ 0 , Then π π : ππ π < π : π π : log π ππ π = π : π π Ο΅ π π ;<= ! > ππ π > π :
Master Theorem: Solving the recurrence π(π) = ππ(π/π) + π(π : ) Size π 1 subproblem Size π/π π subproblems Size π/π % π % subproblems Depth log & π Size 1 π '() ! $ subproblems
Master Theorem: Solving the recurrence After π levels, there are π ! subproblems, each of size π/π ! . So, during the π th level of recursion, the time complexity is * * $ π ! = π π ! $ π & " & " π ! = π π * π *
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. '() ! $ π ! π * ! π π = π π * !"#
Master Theorem: Proof &'( ! ) π # π " & π π = π π " #$% Case 1: π < π " * + " < 1 and the series converges to a constant so Then we have that π π = π π "
Master Theorem: Proof &'( ! ) π # π " & π π = π π " #$% Case 2: π = π " * + " = 1 and so each term is equal to 1 Then we have that π π = π π " log + π
Master Theorem: Proof &'( ! ) π # π " & π π = π π " #$% Case 2: π > π " Then the summation is exponential and grows proportional to its last term &'( ! ) * so + " π &'( ! ) π π = π π " = π π &'( ! * π "
Master Theorem Theorem: If π(π) = ππ(π/π) + π(π : ) for some constants π > 0, π > 1, π β₯ 0 , Then Top-heavy π π : ππ π < π : π π : log π Steady-state ππ π = π : π π Ο΅ Bottom-heavy π π ;<= ! > ππ π > π :
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.
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!!!!!!
Poll: What is the fastest known integer multiplication time? β’ π π /012 3 ) β’ π(π ππππ (log ππππ) β’ π(π ππππ 2^{log β π}) β’ π(π log π) β’ O(n)
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?
Can we do better than π !.#$ ? β’ Could any multiplication algorithm have a faster asymptotic runtime than πͺ π 5.78 ? β’ Any ideas?????
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
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 π§ < + π§ =
Multiplying trinomials β’ ππ¦ 3 + ππ¦ + π ππ¦ 3 + ππ¦ + π
Multiplying trinomials β’ ππ¦ 3 + ππ¦ + π ππ¦ 3 + ππ¦ + π = πππ¦ > + ππ + ππ π¦ 2 + ππ + ππ + ππ π¦ 3 + ππ + ππ π¦ + ππ 9 multiplications means 9 recursive calls. Each multiplication is 1/3 the size of the original.
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 + π(π)
Multiplying trinomials β’ ππ¦ G + ππ¦ + π ππ¦ G + ππ¦ + π = πππ¦ F + ππ + ππ π¦ H + ππ + ππ + ππ π¦ G + ππ + ππ π¦ + ππ π π = 9π π 3 + π(π) 9 > 3 ! a=9 π π * ππ π < π * π π = π π "#$ M % b=3 π π * log π ππ π = π * π π Ο΅ π π = π π & d=1 π π '() ! + ππ π > π *
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
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
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.)
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
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)
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