300 likes | 408 Views
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.
E N D
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 ************************
c a d b X = Y = • MULT(X,Y) • if |X| = |Y| = 1 then doreturn XY • else return Can We do Better? • Divide and Conquer
Complexity • By the Master Theorem: • The Divide and Conquer Tree if n = 1 if n > 1
Can we do better?(Karatsuba 1962) • Gauss Equation • MULT(X,Y) • if |X| = |Y| = 1 then doreturn XY • else • A1= MULT(a,c); A2 = MULT(b,d); • A3= MULT((a+b)(c+d));
Complexity • By the Master Theorem: • The Divide and Conquer Tree if n = 1 if n > 1
Geometry • Points in space • Dimensions • Distances • Subspaces: lines, planes, hyperplanes • Shapes: circles, spheres, cubes, ellipsoids • More complex shapes: convex hulls,… • Axiomization
Distances • Euclidean distance
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.
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 • d=Quick-Sort-ish-Closest-Pair(A,1,n) • Divide • t = Partition(A,1,n,q) • Conquer • d1= Quick-Sort-ish-Closest-Pair(A,1,t) • d2= Quick-Sort-ish-Closest-Pair(A,t+1,n) • Combine • Return min(d1, d2,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 x-coordinates
Divide and Conquer for the Closest Pair Problem Divide by x-median
Divide L R Divide by x-median
Conquer L R Conquer: Recursively solve L and R
Combination I L R d2 Takes the smaller one of d1 , d2 : d = min(d1 , d2 )
Combination IIIs there a point in L and a point in R whose distance is smaller than d ? L R Takes the smaller one of d1 , d2 : d = min(d1 , d2 )
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 d ? L R Takes the smaller one of d1 , d2 : d = min(d1 , d2 )
Combination IIIs there a point in L and a point in R whose distance is smaller than d ? L R Need only to consider the narrow band O(n) time
Combination IIIs there a point in L and a point in R whose distance is smaller than d ? 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 d if and only if there exist two points in S whose distance is less than d. • If S is the whole thing, did we gain any thing? • If s and t in S has the property that ||s-t|| <d, 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 d ? L R There are at most one point in each box
Closest-Pair • Closest-pair(P) • Preprocessing: • Construct Pxand Pyas sorted-list by x- and y-coordinates • Divide • Construct L, Lx, Lyand R, Rx, Ry • Conquer • Let d1= Closest-Pair(L, Lx, Ly) • Let d2= Closest-Pair(R, Rx, Ry) • Combination • Let d = min(d1 , d2 ) • 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 d, update the das 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