lecture 4: divide and conquer iii: other applications and examples

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

Upload: priscilla-shaw

Post on 30-Dec-2015

27 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 4: Divide and Conquer III: Other Applications and Examples

Lecture 4:Divide and Conquer III:Other Applications and Examples

Shang-Hua Teng

Page 2: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 3: Lecture 4: Divide and Conquer III: Other Applications and Examples

How to multiply 2 n-bit numbers

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

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

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

operationsbit )( 2n

Page 4: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 5: Lecture 4: Divide and Conquer III: Other Applications and Examples

Complexity

• By the Master Theorem:

• The Divide and Conquer Tree

)()2/(4

1)(

nnTnT

if n = 1

if n > 1

2)( nnT

Page 6: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 7: Lecture 4: Divide and Conquer III: Other Applications and Examples

Complexity

• By the Master Theorem:

• The Divide and Conquer Tree

)()2/(3

1)(

nnTnT

if n = 1

if n > 1

58.13log2)( nnnT

Page 8: Lecture 4: Divide and Conquer III: Other Applications and Examples

Geometry

• Points in space

• Dimensions

• Distances

• Subspaces: lines, planes, hyper-planes

• Shapes: circles, spheres, cubes, ellipsoids

• More complex shapes: convex hulls,…

• Axiomization

Page 9: Lecture 4: Divide and Conquer III: Other Applications and Examples

Distances• Euclidean distance

1x 2x

2y

1y 11, yx

22 , yx

221

2212211 ,, yyxxyxyx

Page 10: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 11: Lecture 4: Divide and Conquer III: Other Applications and Examples

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.

Page 12: Lecture 4: Divide and Conquer III: Other Applications and Examples

Closest Pair Problem

Page 13: Lecture 4: Divide and Conquer III: Other Applications and Examples

Closest Pair Problem

Page 14: Lecture 4: Divide and Conquer III: Other Applications and Examples

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?

Page 15: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 16: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 17: Lecture 4: Divide and Conquer III: Other Applications and Examples

Closest Pair Problem

Page 18: Lecture 4: Divide and Conquer III: Other Applications and Examples

Divide and Conquer for the Closest Pair Problem

Divide by x-median

Page 19: Lecture 4: Divide and Conquer III: Other Applications and Examples

Divide

Divide by x-median

L R

Page 20: Lecture 4: Divide and Conquer III: Other Applications and Examples

Conquer

Conquer: Recursively solve L and R

L R

1

2

Page 21: Lecture 4: Divide and Conquer III: Other Applications and Examples

Combination I

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

L R

2

Page 22: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 23: Lecture 4: Divide and Conquer III: Other Applications and Examples

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?

Page 24: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 25: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 26: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 27: Lecture 4: Divide and Conquer III: Other Applications and Examples

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.

Page 28: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 29: Lecture 4: Divide and Conquer III: Other Applications and Examples

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

Page 30: Lecture 4: Divide and Conquer III: Other Applications and Examples

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