CSE101: Algorithm Design and Analysis
Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones)
Week-06 Lecture 21: Divide and Conquer (Multiplication)
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 21: Divide and Conquer (Multiplication) DIVIDE AND CONQUER Divide and Conquer Break a problem into
CSE101: Algorithm Design and Analysis
Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones)
Week-06 Lecture 21: Divide and Conquer (Multiplication)
algorithms
𝑑𝑦 + 𝑒 = 𝑏𝑑𝑦! + 𝑏𝑒𝑦 + 𝑐𝑑𝑦 + 𝑐𝑒
4 multiplications. 𝑏𝑑, 𝑏𝑒, 𝑐𝑑, 𝑐𝑒
𝑑𝑦 + 𝑒 = 𝑏𝑑𝑦! + (𝑏𝑒 + 𝑐𝑑)𝑦 + 𝑐𝑒
Then we can improve this by only doing 3 multiplications: 𝑏𝑑, 𝑐𝑒, (𝑏 + 𝑐)(𝑑 + 𝑒)
not seem very impressive when calculating asymptotics.
improvement.
6*11*16 + 3*10*16 + 3*6
where n is a power of 2.
their left and right halves which are each n/2 bits long
xL xR yL yR
where n is a power of 2.
their left and right halves which are each n/2 bits long
𝑦 = 2#/!𝑦& + 𝑦% 𝑧 = 2#/!𝑧& + 𝑧%
! "𝑦& + 𝑦%
2
! "𝑧& + 𝑧%
! " 𝑦&𝑧% + 𝑦%𝑧& + 𝑦%𝑧%
respectively.
% = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦#, 𝑧#
( = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦$, 𝑧$
% ∗ 2) + 𝑄& + 𝑄' ∗ 2
! " + 𝑄
()
! " + 𝑃(𝑜)
n bits, cn time n/2 bits , cn/2 time n/2 bits , cn/2 time n/2 bits, cn/2 time
n/4 n/4
n/4
n/4
n/4
n/4
n/4
n/2 bits, cn/2 time
n/4
….
%! ∗ 4" = 𝑑𝑜 ∗ 2"
….
largest term dominates order.
Insight: replace
multiplications by (linear time) subtraction
respectively.
! " + 𝑆&)
possibilities.)
and y.
possibilities.)
multiplyKS(x,y) returns the correct product xy whenever x has 𝑙 digits and 𝑧 has 𝑙 digits for any 1 ≤ 𝑙 < 𝑜.
𝑧* + 𝑧+
𝑧" + 𝑧$ = 𝑦"𝑧" + 𝑦$𝑧$ + 𝑦"𝑧$ + 𝑦$𝑧"
! " + 𝑆%
𝑆# ∗ 2! + 𝑆$ − 𝑆# − 𝑆" ∗ 2
! " + 𝑆" =
𝑦%𝑧% ∗ 2! + (𝑦%𝑧& + 𝑦&𝑧%) ∗ 2
& ' + 𝑦&𝑧& =
𝑦% ∗ 2
! " + 𝑦&
𝑧% ∗ 2
! " + 𝑧&
= 𝑦𝑧
# ! + 𝑃(𝑜)
n bits, cn time n/2 bits , cn/2 time n/2 bits, cn/2 time n/4 n/4
n/4 n/4 n/4
n/2 bits, cn/2 time n/4
recursive calls instead of 3 reduces the size of the tree more than a constant factor.
….
!* ∗ 3/ = 𝑑𝑜 ∗ (1.5)/
….
largest term dominates order.
*+,$ = 𝑜*+,$ = 𝑜{#.01… }
𝑈 𝑜 = 𝑏𝑈 𝑜 𝑐 + 𝑃 𝑜1 We will use the master theorem.