sweep line algorithm · ch08-320201: algorithms and data structures 550 visualization and computer...
TRANSCRIPT
CH08-320201: Algorithms and Data Structures 550
Visualization and Computer Graphics LabJacobs University
Sweep Line Algorithm
• Event C: Intersection point– Report the point.– Swap the two intersecting line segments
in the sweep line status data structure.– For the new upper segment: test it
against its predecessor for an intersection.
– Insert intersection point (if found) into event queue.
– Similar for new lower segment (with successor).
• Complexity: – k such events– O(lg n) each – O(k lg n) total.
CH08-320201: Algorithms and Data Structures 551
Visualization and Computer Graphics LabJacobs University
Example
s4
s2
s1s0
s3
e1
a1
b1
a0
b0
a2
b2
a4
a3
b3
b4 Sweep Line status:
s0, s1, s2, s3
Event Queue:
a4, b1, b2, b0, b3, b4
CH08-320201: Algorithms and Data Structures 552
Visualization and Computer Graphics LabJacobs University
Example
s4
s2
s1s0
s3Actions:
Insert s4 to SLS
Test s4-s3 and s4-s2.
Add e1 to EQ
Sweep Line status:
s0, s1, s2, s4, s3
Event Queue:
b1, e1, b2, b0, b3, b4
e1
a1
b1
a0
b0
a2
b2
a4
a3
b3
b4
CH08-320201: Algorithms and Data Structures 553
Visualization and Computer Graphics LabJacobs University
Example
s4
s2
s1s0
s3
e2
a1
b1
a0
b0
a2
b2
a4
a3
b3
b4
e1
Actions:
Delete s1 from SLS
Test s0-s2.
Add e2 to EQ
Sweep Line status:
s0, s2, s4, s3
Event Queue:
e1, e2, b2, b0, b3, b4
CH08-320201: Algorithms and Data Structures 554
Visualization and Computer Graphics LabJacobs University
Example
s4
s2
s1s0
s3
a1
b1
a0
b0
a2
b2
a4
a3
b3
b4
e1
e2
Actions:
Swap s3 and s4 in SLS
Test s3-s2.
Sweep Line status:
s0, s2, s3, s4
Event Queue:
e2, b2, b0, b3, b4
CH08-320201: Algorithms and Data Structures 555
Visualization and Computer Graphics LabJacobs University
Complexity Analysis
• Total time complexity: – O((n+k) lgn). – Best case: k = O(1), then this is much better than the naive
algorithm.– Even for k ≈ n, this is still the case.– Worst case: k ≈ n 2, then this is slightly worse than the naive
algorithm.• Total space complexity:
– Event queue: min-heap• O(n+k)
– Sweep line status: red-black tree• O(n).
– Total: O(n+k).
CH08-320201: Algorithms and Data Structures 556
Visualization and Computer Graphics LabJacobs University
6.2 Convex Hull
CH08-320201: Algorithms and Data Structures 557
Visualization and Computer Graphics LabJacobs University
Convex HullConvex Hull• The convex hull of a set of points is the smallest convex polygon
containing the set of points.• A convex polygon is a non-self-intersecting polygon whose internal
angles are all convex (i.e., less than 180 degrees)• In a convex polygon, a segment joining any two vertices lies entirely
inside the polygon.
convex nonconvex
Thursday March 12, 2015 557
segment not contained!
CH08-320201: Algorithms and Data Structures 558
Visualization and Computer Graphics LabJacobs University
Convex HullConvex Hull• Think of a rubber band snapping around the points• Remember to think of special cases
– Collinearity: A point that lies on a segment of the convex is not included in the convex hull
Thursday March 12, 2015 558
CH08-320201: Algorithms and Data Structures 559
Visualization and Computer Graphics LabJacobs University
ApplicationsApplications• Motion planning
– Find an optimal route that avoids obstacles for a robot• Bounding Box
– Obtain a closer bounding box in computer graphics• Pattern Matching
– Compare two objects using their convex hulls
obstacle
end
Thursday March 12, 2015 559
CH08-320201: Algorithms and Data Structures 560
Visualization and Computer Graphics LabJacobs University
Finding a Convex HullFinding a Convex Hull
One algorithm for determining a convex polygon is one which, when following the points in counterclockwise order, always produces left-turns
Thursday March 12, 2015 560
CH08-320201: Algorithms and Data Structures 561
Visualization and Computer Graphics LabJacobs University
Calculating OrientationCalculating Orientation• The orientation of three points (a, b, c) in the
plane is clockwise, counterclockwise, or collinear– Clockwise (CW): right turn– Counterclockwise (CCW): left turn– Collinear (COLL): no turn
• The orientation of three points is characterized by the sign of the determinant ∆(a, b, c)
a
b
c
a
c
b
c
ba
CW
CCW
COLL
Thursday March 12, 2015 561
111
),,(
cc
bb
aa
yxyxyx
cba =Δ
function isLeftTurn(a, b, c):return (b.x - a.x)*(c.y - a.y)–
(b.y - a.y)*(c.x - a.x) > 0
CH08-320201: Algorithms and Data Structures 562
Visualization and Computer Graphics LabJacobs University
ExamplesExamples
a (0,0)
b (0.5,1)
c (1, 0.5)
a (0,0)
c (0.5,1)
b (1, 0.5)
c(2,2)b(1,1)
a(0,0)
CW
CCW
COLL
Thursday March 12, 2015 562
Using the isLeftTurn() method:
(0.5-0) * (0.5-0) - (1-0) * (1-0) = -0.75 (CW)
(1-0) * (1-0) - (0.5-0) * (0.5-0) = 0.75 (CCW)
(1-0) * (2-0) - (1-0) * (2-0) = 0 (COLL)
CH08-320201: Algorithms and Data Structures 563
Visualization and Computer Graphics LabJacobs University
Orientations and convex hullOrientations and convex hull• Let a, b, c be three consecutive vertices of a polygon, in
counterclockwise order– b’ is not included in the hull if a’, b,’ c’ is non-convex,
i.e. orientation(a’, b’, c’) = CW or COLL– b is included in the hull if a, b, c is convex,
i.e. orientation(a, b, c) = CCW, and all other non-hull points have been removed
a’b’
c’
b
c
a
Thursday March 12, 2015 563
CH08-320201: Algorithms and Data Structures 564
Visualization and Computer Graphics LabJacobs University
Graham Scan AlgorithmGraham Scan Algorithm
• Find the anchor point (the point with the smallest y value)• Sort points in CCW order around the anchor• We can sort by increasing angle of the line segments from the
anchor to the points with the x-axis
Thursday March 12, 2015 564
Anchor
2
3
4
56
7
8
1
CH08-320201: Algorithms and Data Structures 565
Visualization and Computer Graphics LabJacobs University
Graham Scan AlgorithmGraham Scan Algorithm• The polygon is traversed in sorted order• A sequence H of vertices in the hull is maintained.• For each point a, add a to H.• While the last turn is a right turn, remove the second-to-last
point from H– In the image below, p,q,r forms a right turn.– Thus q is removed from H. – Similarly, o,p,r forms a right turn.– Thus p is removed from H.
p
q
r
H
op
r
H
n
or
H
Thursday March 12, 2015 565
CH08-320201: Algorithms and Data Structures 566
Visualization and Computer Graphics LabJacobs University
Graham ScanGraham Scan
function graham_scan (pts):// Input: Set of points pts// Output: Hull of pointsfind anchor pointsort other points in CCW order around anchorhull = []for p in pts:
add p to hullwhile last turn is a “right turn”
remove 2nd to last pointadd anchor to hullreturn hull
Thursday March 12, 2015 566
CH08-320201: Algorithms and Data Structures 567
Visualization and Computer Graphics LabJacobs University
AnalysisAnalysisfunction graham_scan(pts):
// Input: Set of points pts// Output: Hull of pointsfind anchor point // O(n)sort other points in CCW order around anchor // O(n lg n)hull = [] // O(1)for p in pts: // O(n)
add p to hull // O(1)while last turn is a “right turn” // O(1)
// Each point is looked at, at most, twice: // 1x left-turn, 1x right-turn.
remove 2nd to last point // O(1)add anchor to hull // O(1)return hull
Overall run time: O(n lg n)Space complexity: O(n) [often a stack is used]
Thursday March 12, 2015 567
CH08-320201: Algorithms and Data Structures 568
Visualization and Computer Graphics LabJacobs University
Incremental AlgorithmIncremental Algorithm
• What if we already have a convex hull, and we just want to add one point q?
• Get the angle from the anchor to q and find points p and r, the hull points on either side of q
• If p,q,r forms a left turn, add q to the hull• Check if adding q creates a concave shape
– If you have right turns on either side of q, remove vertices until the shape becomes convex
– This is done in the same way as the static Graham Scan
Thursday March 12, 2015 568
CH08-320201: Algorithms and Data Structures 569
Visualization and Computer Graphics LabJacobs University
Incremental AlgorithmIncremental Algorithm569Thursday March 12, 2015
H
Original Hull:q
H
Want to add point q:
p
r
q
H
q
H
Find p and r:
p
r
p, q, r forms a left turn, so add q:
r
q
H
on
o, p, q forms a right turn, so remove p
on
n, o, q forms a right turn, so remove o
r
q
H
n
s s s
CH08-320201: Algorithms and Data Structures 570
Visualization and Computer Graphics LabJacobs University
Incremental AlgorithmIncremental AlgorithmNow we look at the other side:
r
q
Hs r
q
H
Since q, r, s is a left turn, we’re done!
s
q
H
n
Since m, n, q is a left turn, we’re done with that side.
• Remember that you can have right turns on either or both sides, so make sure to check in both directions and remove concave points!
Thursday March 12, 2015 570
m
CH08-320201: Algorithms and Data Structures 571
Visualization and Computer Graphics LabJacobs University
AnalysisAnalysis
• Let n be the current size of the convex hull, stored in a binary search tree around the anchor for efficiency
• To check if a point q should be in the hull, we insert it into the tree and get its neighbors (p and r on the prior slides):O(lg n)
• We then traverse the ring, possibly deleting d points from the convex hull:O((1 + d) lg n)
• Therefore, incremental insertion is O(d log n) where d is the number of points removed by the insertion
Thursday March 12, 2015 571
CH08-320201: Algorithms and Data Structures 572
Visualization and Computer Graphics LabJacobs University
A Divide-and-Conquer Approach
• First, sort all points by their x coordinate.• Then divide and conquer:
– Find the convex hull of the left half of points.– Find the convex hull of the right half of points.– Merge the two hulls into one.
CH08-320201: Algorithms and Data Structures 573
Visualization and Computer Graphics LabJacobs University
A Divide-and-Conquer Approach
CH08-320201: Algorithms and Data Structures 574
Visualization and Computer Graphics LabJacobs University
Merging Hulls
Idea:• Find the two lines that are upper and lower tangent to
the two hulls.• Then, remove points that are cut off.
CH08-320201: Algorithms and Data Structures 575
Visualization and Computer Graphics LabJacobs University
Finding upper tangent line• Start with the right-most point of the left hull and
the left-most point of the right hull.• While the line is not an upper tangent to both hulls
– While the line is not an upper tangent to the left hull• Move to next point of left hull counter-clockwise.
– While the line is not an upper tangent to the right hull• Move to next point of right hull clockwise.
CH08-320201: Algorithms and Data Structures 576
Visualization and Computer Graphics LabJacobs University
Finding lower tangent line• Start with the right-most point of the left hull and
the left-most point of the right hull.• While the line is not an lower tangent to both hulls
– While the line is not an lower tangent to the left hull• Move to next point of left hull clockwise.
– While the line is not an lower tangent to the right hull• Move to next point of right hull counter-clockwise.
CH08-320201: Algorithms and Data Structures 577
Visualization and Computer Graphics LabJacobs University
Checking Tangentness• How can we check whether a line is an upper or lower
tangent?• Let‘s check upper tangentness for left hull:
– pr,pl,pl-ccw should be a left turn• Other cases are checked analogously
CH08-320201: Algorithms and Data Structures 578
Visualization and Computer Graphics LabJacobs University
Lower Tangent Example• Initially, T=(4, 7) is only a lower
tangent for A. • The A loop does not execute,
but the B loop increments b to 11.• But now T=(4, 11) is no longer a
lower tangent for A, so the A loop decrements a to 0.
• Again, T=(0, 11) is not a lower tangent for B, so B is incremented to 12.
• T=(0, 12) is a lower tangent for both A and B, so T is returned.
CH08-320201: Algorithms and Data Structures 579
Visualization and Computer Graphics LabJacobs University
Analysis • Preprocessing: Sort the points by x-coordinate.
– O(n lg n)• Let T(n) be the time complexity for n sorted points.• Divide: Split the set of points into two subsets of
(almost) same size.– O(1)
• Conquer: Recursive calls of algorithm for two subproblems of about half the size.– 2 T(n/2)
• Merge: Combine the two hulls by finding upper and lower tangent and removing points in between.– O(n)
CH08-320201: Algorithms and Data Structures 580
Visualization and Computer Graphics LabJacobs University
Analysis
• Recurrence:T(n) = 2 T(n/2) + c n
• Solution (Case 2 of Master theorem)T(n) = O(n lg n)
• Altogether, pre-processing and divide-and-conquer approach lead to O(n lg n).
CH08-320201: Algorithms and Data Structures 653
Visualization and Computer Graphics LabJacobs University
7. Outro
CH08-320201: Algorithms and Data Structures 654
Visualization and Computer Graphics LabJacobs University
Algorithmic concepts
• Divide & Conquer• Dynamic Programming• Greedy Approaches
CH08-320201: Algorithms and Data Structures 655
Visualization and Computer Graphics LabJacobs University
Efficient algorithms• Sorting• Searching• Power of a number• Fibonacci numbers• Matrix multiplication• VLSI layout• Longest Common Subsequence• Activity Selection problem• Knapsack problem• BFS and DFS• Minimum Spanning Trees• Shortest Paths• Maximum Flow• Line-segment Intersections• Convex Hull• Voronoi Diagrams
CH08-320201: Algorithms and Data Structures 656
Visualization and Computer Graphics LabJacobs University
Runtime analysis
• Upper bounds• Lower bounds• Tight bounds• Solving recurrences
CH08-320201: Algorithms and Data Structures 657
Visualization and Computer Graphics LabJacobs University
Data structures
• Array• Stack• Queue• Linked List• Trees• BST• Red-black Trees• Heap• Hash Tables• Graphs
CH08-320201: Algorithms and Data Structures 658
Visualization and Computer Graphics LabJacobs University
Memory analysis
• Also asymptotic bounds• Additional memory• In-situ algorithms
CH08-320201: Algorithms and Data Structures 659
Visualization and Computer Graphics LabJacobs University
The End