Lecture 2: Divide and Conquer I: Merge

Lecture 4:
Divide and Conquer III:
Other Applications and Examples
Shang-Hua Teng
Integer Multiplication
• When do we need to multiply two very
large numbers?
– In Cryptography and Network Security
• message as numbers
• encryption and decryption need to multiply numbers
How to multiply 2 n-bit numbers

************
************
************
************
************
************
************
************
************
************
************
************
************
************
************************
(n ) bit operations
2
Can We do Better?
• Divide and Conquer
X=
a
b
Y=
c
d
X  a 2n / 2  b, Y  c2n / 2  d
XY  ac 2n  (ad  bc)2n / 2  bd
• MULT(X,Y)
– if |X| = |Y| = 1 then do return XY
– else return
MULT( a, c)2n  MULT (a, d )  MULT( b, c)2n / 2  MULT( b, d )
Complexity
1

T ( n)  
4T (n / 2)  (n)
if n = 1
if n > 1
• By the Master Theorem:
 
T (n)   n
2
• The Divide and Conquer Tree
Can we do better?
(Karatsuba 1962)
• Gauss Equation
ad  bc  (a  b)(c  d )  ac  bd
• MULT(X,Y)
– if |X| = |Y| = 1 then do return XY
– else
• A1 = MULT(a,c); A2 = MULT(b,d);
• A3 = MULT((a+b)(c+d));
A1 2n   A3  A1  A2 2 n / 2  A2
Complexity
1

T ( n)  
3T (n / 2)  (n)
if n = 1
if n > 1
• By the Master Theorem:

T (n)   n
log2 3
  n 
• The Divide and Conquer Tree
1.58
Geometry
•
•
•
•
•
•
•
Points in space
Dimensions
Distances
Subspaces: lines, planes, hyper-planes
Shapes: circles, spheres, cubes, ellipsoids
More complex shapes: convex hulls,…
Axiomization
Distances
• Euclidean distance
y1
x1, y1 
x2 , y2 
y2
x1
x2
x1 , y1   x2 , y2 

x1  x2    y1  y2 
2
2
Computational Geometry
• Methods and algorithm design and analysis
for geometric problems
• Applications:
–
–
–
–
–
Computer Graphics
Computer Vision
Geographic information system
Robotics
Scientific simulation and engineering design
Closest Pair Problems
• Input:
– A set of points P = {p1,…, pn} in two dimensions
• Output:
– The pair of points pi, pj that minimize the
Euclidean distance between them.
Closest Pair Problem
Closest Pair Problem

Divide and Conquer
• O(n2) time algorithm is easy
• Assumptions:
– No two points have the same x-coordinates
– No two points have the same y-coordinates
• How do we solve this problem in 1 dimensions?
– Sort the number and walk from left to right to find
minimum gap
• Can we apply divide-and-conquer directly?
Quick-Sort --- ish
 =Quick-Sort-ish-Closest-Pair(A,1,n)
– Divide
• q  RANDOM (1, n)
• t = Partition(A,1,n,q)
– Conquer
 1= Quick-Sort-ish-Closest-Pair(A,1,t)
 2= Quick-Sort-ish-Closest-Pair(A,t+1,n)
– Combine
• Return min(1, 2,min(A[t+1..n])-A[t])
Divide and Conquer
• Divide and conquer has a chance to do better
than O(n2).
• Assume that we can find the median in O(n)
time!!!
• We can first sort the point by their xcoordinates
Closest Pair Problem
Divide and Conquer for the
Closest Pair Problem
Divide by x-median
Divide
L
R
Divide by x-median
Conquer
L
R
1
2
Conquer: Recursively solve L and R
Combination I
L
R

2
Takes the smaller one of 1 , 2 :  = min(1 , 2 )
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?
L
R

Takes the smaller one of 1 , 2 :  = min(1 , 2 )
Combination II
• If the answer is “no” then we are done!!!
• If the answer is “yes” then the closest such
pair forms the closest pair for the entire set
• Why????
• How do we determine this?
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?
L
R

 
Takes the smaller one of 1 , 2 :  = min(1 , 2 )
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?
L
R
 
Need only to consider the narrow band
O(n) time
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?
L
R
 
Denote this set by S, assume Sy is sorted list
of S by y-coordinate.
Combination II
• There exists a point in L and a point in R
whose distance is less than  if and only if
there exist two points in S whose distance is
less than .
• If S is the whole thing, did we gain any thing?
• If s and t in S has the property that ||s-t|| < ,
then s and t are within 30 position of each
other in the sorted list Sy.
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?
L
R
 
There are at most one point in each box
Closest-Pair
• Closest-pair(P)
– Preprocessing:
• Construct Px and Py as sorted-list by x- and y-coordinates
– Divide
• Construct L, Lx , Ly and R, Rx , Ry
– Conquer
• Let 1= Closest-Pair(L, Lx , Ly )
• Let 2= Closest-Pair(R, Rx , Ry )
– Combination
•
•
•
•
Let  = min(1 , 2 )
Construct S and Sy
For each point in Sy, check each of its next 30 points down the list
If the distance is less than  , update the  as this smaller distance
Complexity Analysis
•
•
•
•
Preprocessing takes O(n lg n) time
Divide takes O(n) time
Conquer takes 2 T(n/2) time
Combination takes O(n) time
• So totally takes O(n lg n) time