lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 ·...
Transcript of lecture-20 - University of Illinois at Chicagoxiaorui/cs401/slides/lecture-20.pdf · 2019-12-06 ·...
CS 401
Closest Points / Integer MultiplicationXiaorui Sun
1
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.
Finding the Closest Pair of Points
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
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 ?
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
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
>'
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.}
Closest Pair AnalysisLet !(#) be the number of pairwise distance calculations in the Closest-Pair Algorithm
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
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?
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)
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?
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)
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
Integer Multiplication
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.
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?
Key Trick: 4 multiplies at the price of 3
! = 2$/& ⋅ !( + !*+ = 2$/& ⋅ +( + +*!+ = 2$/& ⋅ !( +!* 2$/& ⋅ +( + +*
= 2$ ⋅ !(+( + 2 ⁄$ & ⋅ !(+* + !*+( + !*+*
- = !( + !*. = +( + +*-. = !( + !* +( + +*
= !(+( + !(+* + !*+( + !*+*!(+* + !*+( = -. − !(+( − !*+*
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
Integer Multiplication (Summary)• Amusing exercise: generalize Karatsuba to do 5 size !/3 subproblems
This gives Θ !%.'(… time algorithm
Still open problem.