data structures and algorithms - penn state college of ...ads22/courses/cmpsc465/s12/lec...s....

27
2/20/12 S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson Adam Smith Data Structures and Algorithms CMPSC 465 LECTURES 7-8 More Divide and Conquer Multiplication

Upload: others

Post on 19-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson

Adam Smith

Data Structures and Algorithms

CMPSC 465

LECTURES 7-8 More Divide and Conquer

• Multiplication

Page 2: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

John McCarthy (1927 –2011)

• Pioneer in artificial intelligence

– In fact, he coined the term

• Lots of work on how reasoning can be automated

– Meta-lesson: you learn a lot about something when

you teach a computer to do it.

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Page 3: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

McCarthy’s “91” function

• Why be so careful about correctness with

recursive procedures?

• This function is tricky to reason about because it

doesn’t always make recursive calls on smaller

inputs

– Programmer’s goal: code that is easy to understand

– Do not write circular programs!

2/20/12 S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson 3

M(n) ={

n − 10, if n > 100M(M(n + 11)), if n ≤ 100

Page 4: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Reminder: geometric series

1

11

2

xxx =+++ for |x| < 1

1

11

12

x

xxxx

n

n=++++

+

for x 1

Page 5: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

Review

• For each tree:

– How many leaves?

– How many nodes in total? (big-theta)

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

H

Page 6: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Multiplying large integers

• Given n-bit integers a, b (in binary), compute

c=ab

• Naive (grade-school) algorithm:

– Write a,b in binary

– Compute n intermediate

products

– Do n additions

• Running time?

– Total work: (n2)

an-1 an-2 … a0

bn-1 bn-2 … b0

n bits

n bits

n bits

2n bit output

0

0 0 0

Page 7: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Divide-and-conquer design

1. Divide the problem (instance) into subproblems.

2. Conquer the subproblems by solving them recursively.

3. Combine subproblem solutions.

Page 8: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Multiplying large integers

• Divide and Conquer (Attempt #1):

– Write a = A1 2n/2 + A0

b = B1 2n/2 + B0

– We want ab = A1B1 2n + (A1B0 + B1A0) 2

n/2 + A0B0

– Multiply n/2 –bit integers recursively

– T(n) = 4T(n/2) + (n)

– Alas! this is still (n2) . (Exercise: write out the recursion tree.)

Page 9: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

• Divide and Conquer (Attempt #1):

– Write a = A1 2n/2 + A0

b = B1 2n/2 + B0

– We want ab = A1B1 2n + (A1B0 + B1A0) 2

n/2 + A0B0

– Multiply n/2 –bit integers recursively

– T(n) = 4T(n/2) + (n)

– Alas! this is still (n2) . (Exercise: write out the recursion tree.)

Multiplying large integers

– Consider the expression (A0+A1) (B0 + B1) = A0B0 + A1B1 + (A0B1 + B1A0)

– We can get away with 3 multiplications! (in yellow)

x = A1B1 y = A0B0 z = (A0+A1)(B0+B1)

– Now we use ab = A1B1 2n + (A1B0 + B1A0) 2

n/2 + A0B0

= x 2n + (z–x–y) 2n/2 + y

Page 10: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Multiplying large integers

MULTIPLY (n, a, b)

a and b are n-bit integers

Assume n is a power of 2 for simplicity

1. If n 2 then use grade-school algorithm else

2. A1 a div 2n/2 ; B1 b div 2n/2 ;

3. A0 a mod 2n/2 ; B0 b mod 2n/2 .

4. x MULTIPLY(n/2 , A1, B1)

5. y MULTIPLY(n/2 , A0, B0)

6. z MULTIPLY(n/2 , A1+A0 , B1+B0)

7. Output x 2n + (z–x–y)2n/2 + y

Page 11: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Multiplying large integers

• The resulting recurrence

T(n) = 3T(n/2) + (n)

• We will see that T(n) = (nlog23) = (n1.59…)

• Note: There is a (n log n) algorithm for

multiplication (assumes constant-time word ops)

– Actually, (n log(n) 2log*(n)) bit-level operations

(due to Martin Fürer, 2007)

Page 12: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Reminder: recursion trees

• Technique for guessing solution to recurrences

– Write out tree of recursive calls

– Each node gets assigned the work done during that

call to the procedure (dividing and combining)

– Total work is sum of work at all nodes

Page 13: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Recursion tree for multiplication

Solve T(n) = 3T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4

cn/2 cn/2

(1)

lg2 n

cn

(3/2)cn

(9/4)cn

cn/2

cn/4

At level k: (3/2)kcn

(3/2)log ncn = c nlog2 3 At bottom level:

Page 14: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Recursion tree for multiplication

Solve T(n) = 3T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4

cn/2 cn/2

(1)

lg2 n

cn

(3/2)cn

(9/4)cn

cn/2

cn/4

)()3(3loglog

log

0

22

2

2

3ncn

n

n

k

k

====

Total work

Geometric series: When base of

exponent is constant (e.g. 3/2), the

largest term is a constant fraction

of total

Page 15: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Divide-and-conquer design

1. Divide the problem (instance) into subproblems.

2. Conquer the subproblems by solving them recursively.

3. Combine subproblem solutions.

Page 16: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Example: Find 9

3 5 7 8 9 12 15

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Page 17: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Example: Find 9

3 5 7 8 9 12 15

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Page 18: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Example: Find 9

3 5 7 8 9 12 15

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Page 19: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Example: Find 9

3 5 7 8 9 12 15

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Page 20: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Example: Find 9

3 5 7 8 9 12 15

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Page 21: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary search

Find an element in a sorted array:

1.Divide: Check middle element.

2.Conquer: Recursively search 1 subarray.

3.Combine: Trivial.

Example: Find 9

3 5 7 8 9 12 15

Page 22: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Binary Search

BINARYSEARCH(b, A[1 . . n]) find b in sorted (increasing order)

array A

1. If n=0 then return “not found”

2. If A[ n/2 ] = b then return n/2

3. If A[ n/2 ] > b then

4. return BINARYSEARCH(A[1 . . n/2 ])

5. Else

6. return n/2 + BINARYSEARCH(A[ n/2 +1 . . n])

Page 23: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

Proof of correctness

• Omitted here, similar to Merge-Sort

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Page 24: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Recurrence for binary search

T(n) = 1 T(n/2) + (1)

# subproblems

subproblem size

work dividing and combining

Page 25: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Recurrence for binary search

T(n) = 1 T(n/2) + (1)

# subproblems

subproblem size

work dividing and combining

T(n) = T(n/2) + c = T(n/4) + 2c = c log n = (lg n) .

Page 26: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

Exponentiation

Problem: Compute a b, where b N is n bits long

Question: How many multiplications?

a b =

a b/2 a

b/2 if b is even;

a (b–1)/2 a

(b–1)/2 a if b is odd.

Divide-and-conquer algorithm:

T(b) = T(b/2) + (1) T(b) = (log b) = (n) .

Naive algorithm: (b) = (2n) (exponential

in the input length!)

Page 27: Data Structures and Algorithms - Penn State College of ...ads22/courses/cmpsc465/S12/lec...S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson Multiplying

2/20/12 S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson

So far: 3 recurrences

• Merge Sort

T(n) = 2 T(n/2) + (n) = (n log n)

• Binary Search / Exponentiation

T(n) = 1 T(n/2) + (1) = (log n)

• Multiplication

T(n) = 3 T(n/2) + (n) = (nlog2 3)

Coming soon: systematic ways to solve these.