30
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng

Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

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

************ ************

************ ************ ************ ************ ************ ************ ************ ************ ************ ************ ************ ************

************************

operationsbit )( 2n

Can We do Better?• Divide and Conquer

c d

a bX =

Y =

bdbcadacXY

dcYbaXnn

nn

2/

2/2/

2)(2

2 ,2

• MULT(X,Y)– if |X| = |Y| = 1 then do return XY– else return

),MULT( 2),MULT( ),(MULT 2),MULT( 2/ dbcbdaca nn

Complexity

• By the Master Theorem:

• The Divide and Conquer Tree

)()2/(4

1)(

nnTnT

if n = 1

if n > 1

2)( nnT

Can we do better?(Karatsuba 1962)

• Gauss Equationbdacdcbabcad ))((

• 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));

22/

2131 2 2 AAAAA nn

Complexity

• By the Master Theorem:

• The Divide and Conquer Tree

)()2/(3

1)(

nnTnT

if n = 1

if n > 1

58.13log2)( nnnT

Geometry

• Points in space

• Dimensions

• Distances

• Subspaces: lines, planes, hyperplanes

• Shapes: circles, spheres, cubes, ellipsoids

• More complex shapes: convex hulls,…

• Axiomization

Distances• Euclidean distance

1x 2x

2y

1y 11, yx

22 , yx

221

2212211 ,, yyxxyxyx

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

• • t = Partition(A,1,n,q)

– Conquer1= 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])

),1( nRANDOMq

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 x-coordinates

Closest Pair Problem

Divide and Conquer for the Closest Pair Problem

Divide by x-median

Divide

Divide by x-median

L R

Conquer

Conquer: Recursively solve L and R

L R

1

2

Combination I

Takes the smaller one of 1 , 2 : = min(1 , 2 )

L R

2

Combination IIIs there a point in L and a point in R whose distance

is smaller than ?

Takes the smaller one of 1 , 2 : = min(1 , 2 )

L R

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 IIIs there a point in L and a point in R whose distance

is smaller than ?

Takes the smaller one of 1 , 2 : = min(1 , 2 )

L R

Combination IIIs there a point in L and a point in R whose distance

is smaller than ?

Need only to consider the narrow bandO(n) time

L R

Combination IIIs there a point in L and a point in R whose distance

is smaller than ?

Denote this set by S, assume Sy is sorted list of S by y-coordinate.

L R

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 15 position of each other in the sorted list Sy.

Combination IIIs 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 15 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