lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 ·...

21
CS 401 Closest Points / Integer Multiplication Xiaorui Sun 1

Transcript of lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 ·...

Page 1: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

CS 401

Closest Points / Integer MultiplicationXiaorui Sun

1

Page 2: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

StuffMidterm review: Oct 14

Midterm exam via gradescope: October 16 (Friday)• The exam will be available on Oct 16 0am• You must submit no later than Oct 16 11:59pm• Usually can be finished in 2 hour• Open textbook, but no copy from internet and between each other• For Yes/No questions, just submit via gradescope• For algorithm design problems, you can either type your solution or

upload the pic of your handwriting to gradescope• For algorithm design problems, describe your algorithm clearly (give the

pseudocode is a good idea)• If you use the algorithm from class in a blackbox way, then you do not

need to rewrite the code. • No class on October 16, I will answer exam questions on blackboard

from 1pm to 3pm.

Page 3: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Finding the Closest Pair of Points

Page 4: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Recap: Closest Pair of Points (1-dim)Given ! points, find the closest pair.

Brute force: Check all "# pairwise distances

1-dim case: 11, 2, 4, 19, 4.8, 7, 8.2, 16, 11.5, 13, 1

find the closest pair

Fact: Closest pair is adjacent in ordered listSo, first sort, then scan adjacent pairs.

Time $(! log !) to sort, if needed, Plus $(!) to scan adjacent pairs

1 2 4 4.8 7 8.2 11 11.5 13 16 19

Page 5: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Divide & ConquerDivide: draw vertical line ! with ≈ "/2 points on each side.

Conquer: find closest pair on each side, recursively.Combine to find closest pair overall

Return best solutions

12

218

L

How ?

Page 6: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Key ObservationSuppose ! is the minimum distance of all pairs in left/right of ".

! = min 12,21 = 12.Key Observation: suffices to consider points within ! of line ".Almost the one-D problem again: Sort points in 2!-strip by their +

coordinate.

12

21

L

d=12

7

1

2

3

45

6

Page 7: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Almost 1D ProblemPartition each side of ! into "# ×

"# squares

Claim: No two points lie in the same "# ×"# box.

Proof: Such points would be within

"##+ "

##= ' (

# ≈ 0.7' < '

Let ./ have the 012 smallest 3-coordinate among points in the 2'-width-strip.

Claim: If 0 − 6 > 11, then the distance between ./ and .9 is > '.Proof: only 11 boxes within ' of 3(./).

d

2930

31

28

26

25

d

½d

½d

49

i

j

27

29

>'

Page 8: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair (2 dimension)Closest-Pair(!", !$,⋯ , !&) {

if(& ≤ $) return |!" − !$|

Compute separation line * such that half the pointsare on one side and half on the other side.

+" = Closest-Pair(left half)+$ = Closest-Pair(right half)+ = min(+", +$)

Delete all points further than d from separation line L

Sort remaining points p[1]…p[m] by y-coordinate.

for , = ", $,⋯ ,.for k = ", $,⋯ , ""if , + 0 ≤ .

d = min(d, distance(p[i], p[i+k]));

return d.}

Page 9: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair AnalysisLet !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm

Page 10: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair (2 dimension)Closest-Pair(!", !$,⋯ , !&) {

if(& ≤ $) return distance(!", !$)

Compute separation line ) such that half the pointsare on one side and half on the other side.

*" = Closest-Pair(left half)*$ = Closest-Pair(right half)* = min(*", *$)

Delete all points further than d from separation line L

Sort remaining points p[1]…p[m] by y-coordinate.

for + = ", $,⋯ ,-for k = ", $,⋯ , ""if + + / ≤ -

d = min(d, distance(p[i], p[i+k]));

return d.}

111 ≤ 112distance calculations

Page 11: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair AnalysisLet !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm

! # ≤ &1 if # ≤ 22! #

2 + 11 # o.w. ⇒ ! # = O(#log #)

BUT, that’s only the number of distance calculationsWhat if we counted running time?

Page 12: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair (2 dimension)Closest-Pair(!", !$,⋯ , !&) {

if(& ≤ $) return distance(!", !$)

Compute separation line ) such that half the pointsare on one side and half on the other side.

*" = Closest-Pair(left half)*$ = Closest-Pair(right half)* = min(*", *$)

Delete all points further than d from separation line L

Sort remaining points p[1]…p[m] by y-coordinate.

for + = ", $,⋯ ,-for k = ", $,⋯ , ""if + + / ≤ -

d = min(d, distance(p[i], p[i+k]));

return d.}

0(2 log 2)

0(2)0(1)

0(2 log 2)

0(2)

Page 13: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair AnalysisLet !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm

! # ≤ &1 if # ≤ 22! #

2 + 11 # o.w. ⇒ ! # = O(#log #)

BUT, that’s only the number of distance calculationsWhat if we counted running time?

4 # ≤ &1 if # ≤ 224 #

2 + 5(# log #) o.w. ⇒ 4 # = O(#log6 #)

Can we do better?

Page 14: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Closest Pair (2 dimension) ImprovedClosest-Pair(!", !$,⋯ , !&) {

if(& ≤ $) return distance(!", !$)

Compute separation line ) such that half the pointsare on one side and half on the other side.

(*", +") = Closest-Pair(left half)(*$, +$) = Closest-Pair(right half)* = min(*", *$)+,-./01 = merge(+",+$) (merge sort it by y-coordinate)

Let 2 be points (ordered as +,-./01) that is * from line L.

for 3 = ", $,⋯ ,5for k = ", $,⋯ , ""if 3 + 7 ≤ 5

d = min(d, distance(S[i], S[i+k]));

return d and +,-./01.} 8 9 ≤ :

1 if 9 = 128 9

2 + ? 9 o.w.⇒ 8 9 = ?(9 log 9)

Input sorted by y-coordinate

Assume: input sorted by x-coordinate(O(n log n) overhead initially)

?(1)

?(9)?(1)

?(9)

?(9)

Page 15: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

SummaryClosest pair in 2-dimension: Given ! points in the plane, find a pair with smallest Euclidean distance between them.

Brute Force: Check all pairs of points in Θ(!$) time.

Divde and Conquer:

• Divide: draw vertical line & with ≈ !/2 points on each side.

• Conquer: find closest pair on each side, recursively.• Combine to find closest pair overall

Page 16: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Integer Multiplication

Page 17: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Integer ArithmeticAdd: Given two !-bit integers " and #, compute " + #.

Multiply: Given two !-bit integers " and #, compute "×#.The “grade school” method:

17

1

011 1

110 1+

010 1

111

010 1

011 1

100 0

10111

Add

1

1

0

0

1

1

1

0

0

1

1

1

1

0

0

1

1

1

1

0

1

0

1

0000000

1010101

1010101

1010101

1010101

1010101

100000000001011

1

0

1

1

1

1

1

0

*

Multiply

00000000

&(!) bit operations.

&(!)) bit operations.

Page 18: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Divide and ConquerLet !, # be two $-bit integersWrite ! = 2'/)!* + !, and # = 2'/)#* + #,

where !,, !*, #,, #* are all $/2-bit integers.

Therefore, - $ = 4- $

2 + Θ($)So,

- $ = Θ $) .

! = 2'/) ⋅ !* + !,# = 2'/) ⋅ #* + #,!# = 2'/) ⋅ !* +!, 2'/) ⋅ #* + #,

= 2' ⋅ !*#* + 2 ⁄' ) ⋅ !*#, + !,#* + !,#,We only need 3 values!*#*, !,#,, !*#, + !,#*

Can we find all 3 by only3 multiplication?

Page 19: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Key Trick: 4 multiplies at the price of 3

! = 2$/& ⋅ !( + !*+ = 2$/& ⋅ +( + +*!+ = 2$/& ⋅ !( +!* 2$/& ⋅ +( + +*

= 2$ ⋅ !(+( + 2 ⁄$ & ⋅ !(+* + !*+( + !*+*

- = !( + !*. = +( + +*-. = !( + !* +( + +*

= !(+( + !(+* + !*+( + !*+*!(+* + !*+( = -. − !(+( − !*+*

Page 20: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Key Trick: 4 multiplies at the price of 3Theorem [Karatsuba-Ofman, 1962] Can multiply two n-digit integers in O(n1.585…) bit operations.

To multiply two n-bit integers:Add two !/2 bit integers.Multiply three !/2-bit integers.Add, subtract, and shift !/2-bit integers to obtain result.

$ ! = 3$ !2 + ( ! ⇒ $ ! = ( !*+,- . = ((!0.232…)

6 = 27/8 ⋅ 60 + 6: ⇒ ; = 60 + 6:< = 27/8 ⋅ <0 + <: ⇒ = = <0 + <:6< = 27/8 ⋅ 60 +6: 27/8 ⋅ <0 + <:

= 27 ⋅ 60<0 + 2 ⁄7 8 ⋅ 60<: + 6:<0 + 6:<:A B;= − @ − A

Page 21: lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 · Fast Fourier Transform: Applications Applications. Optics, acoustics, quantum physics,

Integer Multiplication (Summary)• Amusing exercise: generalize Karatsuba to do 5 size !/3 subproblems

This gives Θ !%.'(… time algorithm

Still open problem.