Upload
surangma-parashar
View
224
Download
0
Embed Size (px)
Citation preview
8/3/2019 z-aalg10
1/19
Advanced AlgorithmDesign and Analysis (Lecture 10)
SW5 fall 2004Simonas altenisE1-215b
8/3/2019 z-aalg10
2/19
AALG, lecture 10, Simonas altenis, 2004 2
Computational geometry
Main goals of the lecture: to understand the concept ofoutput-sensitive
algorithms;
to be able to apply the divide-and-conqueralgorithm design technique to geometricproblems;
to remember howrecurrences are used toanalyze the divide-and-conquer algorithms;
to understand and be able to analyze theJarviss march and the divide-and-conquerclosest-pairalgorithms.
8/3/2019 z-aalg10
3/19
AALG, lecture 10, Simonas altenis, 2004 3
Size of the output
In computational geometry, the size of analgorithms outputmay differ/depend onthe input.
Line-intersection problem vs. convex-hullproblem
Observation: Grahams scan running timedepends only on the size of the input it isindependent of the size of the output
8/3/2019 z-aalg10
4/19
AALG, lecture 10, Simonas altenis, 2004 4
Gift wrapping
Would be nice to have an algorithm thatruns fast if the convex hull is small Idea:gift wrapping (a.k.a Jarviss march)
1. Start with the lowest point a, include it in the
convex hull 2. The next point in the convex hull has to be in the
clockwise direction with respect to all other pointslooking from the current point on the convex hull
3. Repeat 2. until a is reached.
a a a
8/3/2019 z-aalg10
5/19
AALG, lecture 10, Simonas altenis, 2004 5
Jarviss march
How many cross products are computed for thisexample?
The running time of Jarviss march:
Find lowest point O(n)
For each vertex in the convex hull: n1 cross-productcomputations
Total: O(nh), where h is the number of vertices in theconvex hull
8/3/2019 z-aalg10
6/19
AALG, lecture 10, Simonas altenis, 2004 6
Output-sensitive algorithms
Output-sensitive algorithm: its runningtime depends on the size of the output.
When should we use Jarvis march instead ofthe Grahams scan?
The asymptotically optimal output-sensitivealgorithm of Kirkpatrick and Seidel runs inO(n lg h)
8/3/2019 z-aalg10
7/19AALG, lecture 10, Simonas altenis, 2004 7
Closest-pair problem
Given a setPofnpoints, findp,qP, suchthat the distance d(p, q) is minimum
Checking the distance between two points isO(1)
What is the brute-force algorithm and itsrunning time?
8/3/2019 z-aalg10
8/19AALG, lecture 10, Simonas altenis, 2004 8
Steps of Divide-and-Conquer
What are the steps of a divide-and-conquer algorithm?
If trivial (small), solve it brute force
Else 1.divide into a number of sub-problems 2.solve each sub-problem recursively
3.combine solutions to sub-problems
8/3/2019 z-aalg10
9/19AALG, lecture 10, Simonas altenis, 2004 9
Dividing into sub-problems
How do we divide into sub-problems? Idea: Sort on x-coordinate, and divide into left and right
parts:p1p2 ...pn/2 ...pn/2+1 ...pn
Solve recursively the left sub-problem Pl(closest-pairdistance dl) and the right sub-problem Pr(distance dr)
8/3/2019 z-aalg10
10/19AALG, lecture 10, Simonas altenis, 2004 10
Combining two solutions
How do we combine two solutions to sub-problems? Let d= min{dl, dr}
Observation 1: We already have the closest
pair where both points are either in the left orin the right sub-problem, we have to checkpairs where one point is from one sub-problemand another from the other.
Observation 2: Such closest-pair can only besomewhere in a strip of width 2daround thedividing line! Otherwise the points would be more than dunits
apart.
8/3/2019 z-aalg10
11/19AALG, lecture 10, Simonas altenis, 2004 11
Combining two solutions
Combining solutions: Finding the closestpair ( , ) in a strip of width 2d, knowing
that no ( , ) or ( , ) pair is closer than d
8/3/2019 z-aalg10
12/19AALG, lecture 10, Simonas altenis, 2004 12
Combining Two Solutions
Do we have to check all pairs of points in thestrip? For a given pointp from one partition, where can there
be a pointq from the other partition, that can form theclosest pair withp?
In the ddsquare:
How many points can there be in this square?
At most 4!
p
8/3/2019 z-aalg10
13/19AALG, lecture 10, Simonas altenis, 2004 13
Combining two solutions
Algorithm for checking the strip: Sort all the points in the strip on the y-
coordinate
For each pointp only 7 points ahead of it in theorder have to be checked to see if any of themis closer top than d
p
8/3/2019 z-aalg10
14/19
AALG, lecture 10, Simonas altenis, 2004 14
Pseudocode
What is the trivial problem? That is when do we stop recursion?
Closest-Pair(P, l, r)
// First call: an array P of points sorted on x-coordinate, 1, n
01 if r
l < 3 then return Brute-Force-CPair(P, l, r)02 q (l+r)/203 dl Closest-Pair(P, l, q-1)04 dr Closest-Pair(P, q, r)05 d min(dl, dr)06 for i l to r do07 if P[q].x - d P[i].x P[q].x + d then08 append P[i] to S
09 Sort S on y-coordinate
10 for j 1 to size_of(S)-1 do11 Check if any of d(S[j],S[j]+1), ..., d(S[j],S[j]+7) is
smaller than d, if so set d to the smallest of them
12 return d
8/3/2019 z-aalg10
15/19
AALG, lecture 10, Simonas altenis, 2004 15
Example
How many distance computations are donein this example?
8/3/2019 z-aalg10
16/19
AALG, lecture 10, Simonas altenis, 2004 16
Running time
What is the running time of this algorithm? Running time of a divide-and-conquer
algorithm can be described by a recurrence
Divide = O(1)
Combine = O(n lg n)
This gives the following recurrence:
if 3
( ) 2 ( / 2) log otherwise
n nT n
T n n n
Total running time: O(n log2 n)
Better than brute force, but
8/3/2019 z-aalg10
17/19
AALG, lecture 10, Simonas altenis, 2004 17
Improving the running time
How can we improve the running time ofthe algorithm?
Idea: Sort all the points by x and y coordinateonce
Before recursive calls, partition the sortedlists into two sorted sublists for the left andright halves: O(n)
When combining, run through the y-sorted list
once and select all points that are in a 2dstriparound partition line: O(n)
How does the new recurrence look like andwhat is its solution?
8/3/2019 z-aalg10
18/19
AALG, lecture 10, Simonas altenis, 2004 18
Conclusion
The closest pair can be found in O(n log n)time with divide-and-conquer algorithm
Plane-sweep algorithm with the sameasymptotic running time exists
This is asymptotically optimal
8/3/2019 z-aalg10
19/19
AALG, lecture 10, Simonas altenis, 2004 19
Exercise: Convex-hull
Lets find the convex-hull using divide-and-conquer
What is a trivial problem and how we solve it?
How do we divide the problem into sub-problems?
How do we combine solutions to sub-problems?