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

cse101 algorithm design and analysis
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSE101: Algorithm Design and Analysis

Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones)

Week-06 Lecture 21: Divide and Conquer (Multiplication)

slide-2
SLIDE 2

DIVIDE AND CONQUER

slide-3
SLIDE 3

Divide and Conquer

  • Break a problem into similar subproblems
  • Solve each subproblem recursively
  • Combine
slide-4
SLIDE 4

Above its weight class

  • Divide and conquer is a very simple idea
  • But it has far more than its share of the miraculous

algorithms

  • Examples
  • Strassen Matrix Multiplication
  • Karatsuba multiplication
  • Fast Fourier Transform
  • Linear time select
slide-5
SLIDE 5

Multiplying Binomials

  • if you want to multiply two binomials
  • 𝑏𝑦 + 𝑐

𝑑𝑦 + 𝑒 = 𝑏𝑑𝑦! + 𝑏𝑒𝑦 + 𝑐𝑑𝑦 + 𝑐𝑒

  • It

4 multiplications. 𝑏𝑑, 𝑏𝑒, 𝑐𝑑, 𝑐𝑒

slide-6
SLIDE 6

Multiplying Binomials

  • if you want to multiply two binomials
  • 𝑏𝑦 + 𝑐

𝑑𝑦 + 𝑒 = 𝑏𝑑𝑦! + (𝑏𝑒 + 𝑐𝑑)𝑦 + 𝑐𝑒

  • It requires 4 multiplications. 𝑏𝑑, 𝑏𝑒, 𝑐𝑑, 𝑐𝑒
  • If we assume that addition is cheap (has short runtime.)

Then we can improve this by only doing 3 multiplications: 𝑏𝑑, 𝑐𝑒, (𝑏 + 𝑐)(𝑑 + 𝑒)

slide-7
SLIDE 7

Multiplying Binomials

  • Reducing the number of multiplications from 4 to 3 may

not seem very impressive when calculating asymptotics.

  • If this was only a part of a bigger algorithm, it may be an

improvement.

slide-8
SLIDE 8

Multiplying Binary numbers

slide-9
SLIDE 9

Divide and conquer multiply

  • Say we want to multiply 10100110 and 10110011
  • How can we divide the problem into sub-problems?
  • Remember, we want much smaller sub-problems
slide-10
SLIDE 10

Multiplying large binary numbers

  • 10100110 = 166= 1010 * 2" + 0110 = 10*16 + 6
  • 10110011 = 179= 1011*2" + 0011 = 11*16 + 3
  • 10100110*10110011 = (10*16+6)(11*16+3)= 110*256 +

6*11*16 + 3*10*16 + 3*6

slide-11
SLIDE 11

Multiplying Binary numbers (DC)

  • Suppose we want to multiply two n-bit numbers together

where n is a power of 2.

  • One way we can do this is by splitting each number into

their left and right halves which are each n/2 bits long

  • x=
  • y=

xL xR yL yR

slide-12
SLIDE 12

Multiplying Binary numbers (DC)

  • Suppose we want to multiply two n-bit numbers together

where n is a power of 2.

  • One way we can do this is by splitting each number into

their left and right halves which are each n/2 bits long

  • 𝑦 = 2#/!𝑦𝑀 + 𝑦%
  • 𝑧 = 2#/!𝑧𝑀 + 𝑧%
slide-13
SLIDE 13

Multiplying Binary numbers (DC)

𝑦 = 2#/!𝑦& + 𝑦% 𝑧 = 2#/!𝑧& + 𝑧%

  • 𝑦𝑧 = 2

! "𝑦& + 𝑦%

2

! "𝑧& + 𝑧%

  • 𝑦𝑧 = 2#𝑦&𝑧& + 2

! " 𝑦&𝑧% + 𝑦%𝑧& + 𝑦%𝑧%

slide-14
SLIDE 14

Algorithm multiply

  • function multiply (x,y):
  • Input: n-bit integers x and y
  • Output: the product xy
  • If n=1: return xy
  • 𝑦#, 𝑦$ and 𝑧#, 𝑧$ are the left-most and right-most n/2 bits of x and y,

respectively.

  • 𝑄

% = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦#, 𝑧#

  • 𝑄& = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦#, 𝑧$
  • 𝑄' = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦$, 𝑧#
  • 𝑄

( = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳 𝑦$, 𝑧$

  • return (𝑄

% ∗ 2) + 𝑄& + 𝑄' ∗ 2

! " + 𝑄

()

slide-15
SLIDE 15

Algorithm

  • Runtime analysis:
  • Let T(n) be the runtime of the multiply algorithm.
  • Then 𝑈 𝑜 = 4𝑈

! " + 𝑃(𝑜)

slide-16
SLIDE 16

Total time

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

slide-17
SLIDE 17

Total

  • One top level : cn
  • 4 depth 1: cn/2 *4
  • 16 depth 2: cn/4 * 16
  • 64 depth 3: cn/8 * 64

….

  • 4" 𝑒𝑓𝑞𝑢ℎ 𝑢 ∶ #$

%! ∗ 4" = 𝑑𝑜 ∗ 2"

….

  • Max level : t= log n, (cn/2log n)*4&'($ = 𝑑 ∗ 2&'($ ∗ 2&'($ = 𝑑𝑜%
slide-18
SLIDE 18

Total time

  • cn ( 1+ 2 +4 +8+…2'()#) = 𝑃 𝑑𝑜!
  • Because in a geometric series with ratio other than 1,

largest term dominates order.

slide-19
SLIDE 19

Multiplication

Insight: replace

  • ne (of the 4)

multiplications by (linear time) subtraction

slide-20
SLIDE 20

Algorithm multiply KS

  • function multiplyKS (x,y)
  • Input: n-bit integers x and y
  • Output: the product xy
  • If n=1: return xy
  • 𝑦#, 𝑦$ and 𝑧#, 𝑧$ are the left-most and right-most n/2 bits of x and y,

respectively.

  • 𝑆% = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳𝐋𝐓 𝑦#, 𝑧#
  • 𝑆& = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳𝐋𝐓 𝑦$, 𝑧$
  • 𝑆' = 𝐧𝐯𝐦𝐮𝐣𝐪𝐦𝐳𝐋𝐓 (𝑦#+𝑦$), 𝑧# + 𝑧$
  • return (𝑆% ∗ 2) + 𝑆' − 𝑆% − 𝑆& ∗ 2

! " + 𝑆&)

slide-21
SLIDE 21

Correctness multiply KS

  • Correctness: by strong induction on 𝑜, the number of bits
  • f x and y.
  • Base Case: 𝑜 = 1 then return xy (could make a table of

possibilities.)

  • Inductive hypothesis:
slide-22
SLIDE 22

Correctness multiply KS

  • Correctness: by strong induction on 𝑜, the number of bits of x

and y.

  • Base Case: 𝑜 = 1 then return xy (could make a table of

possibilities.)

  • Inductive hypothesis: For some 𝑜 > 1, assume that

multiplyKS(x,y) returns the correct product xy whenever x has 𝑙 digits and 𝑧 has 𝑙 digits for any 1 ≤ 𝑙 < 𝑜.

  • Then by the IH: 𝑆) = 𝑦*𝑧*, 𝑆% = 𝑦+𝑧+, 𝑆, = 𝑦* + 𝑦+

𝑧* + 𝑧+

slide-23
SLIDE 23

Correctness multiply KS

  • Then by the IH:
  • 𝑆! = 𝑦"𝑧", 𝑆# = 𝑦$𝑧$, 𝑆% = 𝑦" + 𝑦$

𝑧" + 𝑧$ = 𝑦"𝑧" + 𝑦$𝑧$ + 𝑦"𝑧$ + 𝑦$𝑧"

  • And the algorithm returns:𝑆" ∗ 2# + 𝑆$ − 𝑆" − 𝑆% ∗ 2

! " + 𝑆%

𝑆# ∗ 2! + 𝑆$ − 𝑆# − 𝑆" ∗ 2

! " + 𝑆" =

𝑦%𝑧% ∗ 2! + (𝑦%𝑧& + 𝑦&𝑧%) ∗ 2

& ' + 𝑦&𝑧& =

𝑦% ∗ 2

! " + 𝑦&

𝑧% ∗ 2

! " + 𝑧&

= 𝑦𝑧

slide-24
SLIDE 24

Algorithm multiplyKS

  • Runtime
  • Let T(n) be the runtime of the multiply algorithm.
  • Then
  • 𝑈 𝑜 = 3𝑈

# ! + 𝑃(𝑜)

slide-25
SLIDE 25

Total time

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

slide-26
SLIDE 26

3 vs 4

  • Since we are pruning the tree recursively, replacing 4

recursive calls instead of 3 reduces the size of the tree more than a constant factor.

slide-27
SLIDE 27

Total

  • One top level : cn
  • 4 depth 1: cn/2 *3
  • 16 depth 2: cn/4 * 9
  • 64 depth 3: cn/8 * 27

….

  • 4/ 𝑒𝑓𝑞𝑢ℎ 𝑢 ∶ 0#

!* ∗ 3/ = 𝑑𝑜 ∗ (1.5)/

….

  • Max level : t= log n
slide-28
SLIDE 28

Total time

  • cn ( 1+ 1.5 +2.25 +…(1.5)'()#) = 𝑃 3'()#
  • Because in a geometric series with ratio other than 1,

largest term dominates order.

  • But what is 3'()#?
slide-29
SLIDE 29

Simplifying

  • 3'() ! = 2*+,$ *+,! = 2 *+,!∗*+,$ = 2 *+,!

*+,$ = 𝑜*+,$ = 𝑜{#.01… }

  • So total time is 𝑃 𝑜*+,$
slide-30
SLIDE 30

Master Theorem

  • How do you solve a recurrence of the form

𝑈 𝑜 = 𝑏𝑈 𝑜 𝑐 + 𝑃 𝑜1 We will use the master theorem.