Download presentation
Presentation is loading. Please wait.
1
Lecture 4: Divide and Conquer III: Other Applications and Examples Shang-Hua Teng
2
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
3
How to multiply 2 n-bit numbers ************ ************************
4
Can We do Better? Divide and Conquer cd ab X = Y = MULT(X,Y) –if |X| = |Y| = 1 then do return XY –else return
5
Complexity By the Master Theorem: The Divide and Conquer Tree if n = 1 if n > 1
6
Can we do better? (Karatsuba 1962) Gauss Equation MULT(X,Y) –if |X| = |Y| = 1 then do return XY –else A 1 = MULT(a,c); A 2 = MULT(b,d); A 3 = MULT((a+b)(c+d));
7
Complexity By the Master Theorem: The Divide and Conquer Tree if n = 1 if n > 1
8
Geometry Points in space Dimensions Distances Subspaces: lines, planes, hyperplanes Shapes: circles, spheres, cubes, ellipsoids More complex shapes: convex hulls,… Axiomization
9
Distances Euclidean distance
10
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
11
Closest Pair Problems Input: –A set of points P = {p 1,…, p n } in two dimensions Output: –The pair of points p i, p j that minimize the Euclidean distance between them.
12
Closest Pair Problem
14
Divide and Conquer O(n 2 ) 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?
15
Quick-Sort --- ish =Quick-Sort-ish-Closest-Pair(A,1,n) –Divide 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])
16
Divide and Conquer Divide and conquer has a chance to do better than O(n 2 ). Assume that we can find the median in O(n) time!!! We can first sort the point by their x- coordinates
17
Closest Pair Problem
18
Divide and Conquer for the Closest Pair Problem Divide by x-median
19
Divide Divide by x-median L R
20
Conquer Conquer: Recursively solve L and R L R
21
Combination I Takes the smaller one of 1, 2 : = min( 1, 2 ) L R 22
22
Combination II Is 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
23
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?
24
Combination II Is 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
25
Combination II Is there a point in L and a point in R whose distance is smaller than ? Need only to consider the narrow band O(n) time L R
26
Combination II Is there a point in L and a point in R whose distance is smaller than ? Denote this set by S, assume S y is sorted list of S by y-coordinate. L R
27
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 S y.
28
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
29
Closest-Pair Closest-pair(P) –Preprocessing: Construct P x and P y as sorted-list by x- and y-coordinates –Divide Construct L, L x, L y and R, R x, R y –Conquer Let 1 = Closest-Pair(L, L x, L y ) Let 2 = Closest-Pair(R, R x, R y ) –Combination Let = min( 1, 2 ) Construct S and S y For each point in S y, check each of its next 15 points down the list If the distance is less than , update the as this smaller distance
30
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.