computational geometry - tulane university
TRANSCRIPT
![Page 1: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/1.jpg)
CS 5633 Analysis of Algorithms 13/22/04
CS 5633 -- Spring 2004
Computational GeometryCarola Wenk
Slides courtesy of Charles Leiserson with small changes by Carola Wenk
![Page 2: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/2.jpg)
CS 5633 Analysis of Algorithms 23/22/04
Computational geometryAlgorithms for solving “geometric problems”in 2D and higher.Fundamental objects:
point line segment lineBasic structures:
polygonpoint set
![Page 3: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/3.jpg)
CS 5633 Analysis of Algorithms 33/22/04
Computational geometryAlgorithms for solving “geometric problems”in 2D and higher.Fundamental objects:
point line segment lineBasic structures:
convex hulltriangulation
![Page 4: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/4.jpg)
CS 5633 Analysis of Algorithms 43/22/04
Orthogonal range searching
Input: n points in d dimensions• E.g., representing a database of n records
each with d numeric fieldsQuery: Axis-aligned box (in 2D, a rectangle)
• Report on the points inside the box: • Are there any points?• How many are there?• List the points.
![Page 5: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/5.jpg)
CS 5633 Analysis of Algorithms 53/22/04
Orthogonal range searching
Input: n points in d dimensionsQuery: Axis-aligned box (in 2D, a rectangle)
• Report on the points inside the boxGoal: Preprocess points into a data structure
to support fast queries• Primary goal: Static data structure• In 1D, we will also obtain adynamic data structuresupporting insert and delete
![Page 6: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/6.jpg)
CS 5633 Analysis of Algorithms 63/22/04
1D range searchingIn 1D, the query is an interval:
First solution:• Sort the points and store them in an array
• Solve query by binary search on endpoints.• Obtain a static structure that can listk answers in a query in O(k + log n) time.
Goal: Obtain a dynamic structure that can listk answers in a query in O(k + log n) time.
![Page 7: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/7.jpg)
CS 5633 Analysis of Algorithms 73/22/04
1D range searchingIn 1D, the query is an interval:
New solution that extends to higher dimensions:• Balanced binary search tree
• New organization principle:Store points in the leaves of the tree.
• Internal nodes store copies of the leavesto satisfy binary search property:
• Node x stores in key[x] the maximumkey of any leaf in the left subtree of x.
![Page 8: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/8.jpg)
CS 5633 Analysis of Algorithms 83/22/04
Example of a 1D range tree
11
66 88 1212 1414
1717
2626 3535 4141 4242
4343
5959 6161
key[x] is the maximum key of any leaf in the left subtree of x.
![Page 9: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/9.jpg)
CS 5633 Analysis of Algorithms 93/22/04
Example of a 1D range tree
121211
66 88 1212 1414
1717
2626 3535 4141 4242
4343
5959 6161
66 2626 4141 5959
11 1414 3535 4343
424288
1717xx
≤ x > x
key[x] is the maximum key of any leaf in the left subtree of x.
![Page 10: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/10.jpg)
CS 5633 Analysis of Algorithms 103/22/04
1212
88 1212 1414
1717
2626 3535 4141
2626
1414
Example of a 1D range query
11
66 4242
4343
5959 6161
66 4141 5959
11
1212
88 1212 1414
1717
2626 3535 4141
2626
1414 3535 4343
424288
1717
RANGE-QUERY([7, 41])
xx
≤ x > x
![Page 11: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/11.jpg)
CS 5633 Analysis of Algorithms 113/22/04
General 1D range queryroot
split node
![Page 12: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/12.jpg)
CS 5633 Analysis of Algorithms 123/22/04
Pseudocode, part 1:Find the split node
1D-RANGE-QUERY(T, [x1, x2])w ← root[T]while w is not a leaf and (x2 ≤ key[w] or key[w] < x1)
do if x2 ≤ key[w]then w ← left[w]else w ← right[w]
// w is now the split node[traverse left and right from w and report relevant subtrees]
![Page 13: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/13.jpg)
CS 5633 Analysis of Algorithms 133/22/04
Pseudocode, part 2: Traverse left and right from split node
1D-RANGE-QUERY(T, [x1, x2])[find the split node]// w is now the split nodeif w is a leafthen output the leaf w if x1 ≤ key[w] ≤ x2else v ← left[w] // Left traversal
while v is not a leafdo if x1 ≤ key[v]
then output the subtree rooted at right[v]v ← left[v]
else v ← right[v]output the leaf v if x1 ≤ key[v] ≤ x2[symmetrically for right traversal]
w
![Page 14: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/14.jpg)
CS 5633 Analysis of Algorithms 143/22/04
Analysis of 1D-RANGE-QUERY
Query time: Answer to range query representedby O(log n) subtrees found in O(log n) time.Thus:
• Can test for points in interval in O(log n) time.• Can report the first k points in interval in
O(k + log n) time.• Can count points in interval in
O(log n) time (exercise)Space: O(n)Preprocessing time: O(n log n)
![Page 15: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/15.jpg)
CS 5633 Analysis of Algorithms 153/22/04
2D range trees
![Page 16: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/16.jpg)
CS 5633 Analysis of Algorithms 163/22/04
Store a primary 1D range tree for all the pointsbased on x-coordinate.
2D range trees
Thus in O(log n) time we can find O(log n) subtreesrepresenting the points with proper x-coordinate.How to restrict to points with proper y-coordinate?
![Page 17: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/17.jpg)
CS 5633 Analysis of Algorithms 173/22/04
2D range treesIdea: In primary 1D range tree of x-coordinate,every node stores a secondary 1D range treebased on y-coordinate for all points in the subtreeof the node. Recursively search within each.
![Page 18: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/18.jpg)
CS 5633 Analysis of Algorithms 183/22/04
Analysis of 2D range treesQuery time: In O(log2 n) = O((log n)2) time, we canrepresent answer to range query by O(log2 n) subtrees.Total cost for reporting k points: O(k + (log n)2).
Preprocessing time: O(n log n)
Space: The secondary trees at each level of theprimary tree together store a copy of the points.Also, each point is present in each secondarytree along the path from the leaf to the root.Either way, we obtain that the space is O(n log n).
![Page 19: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/19.jpg)
CS 5633 Analysis of Algorithms 193/22/04
d-dimensional range trees
Query time: O(k + logd n) to report k points.Space: O(n logd – 1 n)Preprocessing time: O(n logd – 1 n)
Each node of the secondary y-structure storesa tertiary z-structure representing the pointsin the subtree rooted at the node, etc.
Best data structure to date:Query time: O(k + logd – 1 n) to report k points.Space: O(n (log n / log log n)d – 1)Preprocessing time: O(n logd – 1 n)
![Page 20: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/20.jpg)
CS 5633 Analysis of Algorithms 203/22/04
Primitive operations: Crossproduct
Given two vectors v1 = (x1, y1) and v2 = (x2, y2),is their counterclockwise angle θ
• convex (< 180º),• reflex (> 180º), or• borderline (0 or 180º)?
v1
v2θ v2
v1
θconvex reflex
Crossproduct v1 × v2 = x1 y2 – y1 x2= |v1| |v2| sin θ .
Thus, sign(v1 × v2) = sign(sin θ) > 0 if θ convex,< 0 if θ reflex,= 0 if θ borderline.
![Page 21: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/21.jpg)
CS 5633 Analysis of Algorithms 213/22/04
Primitive operations:Orientation test
Given three points p1, p2, p3 are they• in clockwise (cw) order,• in counterclockwise (ccw) order, or• collinear?
(p2 – p1) × (p3 – p1)> 0 if ccw< 0 if cw= 0 if collinear p1
p3
p2
cw p1
p2
p3
ccw
p1
p2
p3
collinear
![Page 22: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/22.jpg)
CS 5633 Analysis of Algorithms 223/22/04
Primitive operations:Sidedness test
Given three points p1, p2, p3 are they• in clockwise (cw) order,• in counterclockwise (ccw) order, or• collinear?
Let L be the oriented line from p1 to p2.Equivalently, is the point p3
• right of L,• left of L, or• on L?
p1
p2
p3
collinear
p1
p3
p2
cw p1
p2
p3
ccwL
L
![Page 23: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/23.jpg)
CS 5633 Analysis of Algorithms 233/22/04
Line-segment intersectionGiven n line segments, does any pair intersect?Obvious algorithm: O(n2).
a
b
c
de
f
![Page 24: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/24.jpg)
CS 5633 Analysis of Algorithms 243/22/04
Sweep-line algorithm• Sweep a vertical line from left to right(conceptually replacing x-coordinate with time).
• Maintain dynamic set S of segmentsthat intersect the sweep line, orderedby y-coordinate of intersection.
• Order changes when• new segment is encountered,• existing segment finishes, or• two segments cross
• Key event points are therefore segment endpoints.
segmentendpoints
sweep-line status
![Page 25: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/25.jpg)
CS 5633 Analysis of Algorithms 253/22/04
a
b
c
de
f
aab b b b b b f f f f
ca
ca d d e d b e ed
c c d b d d de e e b
![Page 26: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/26.jpg)
CS 5633 Analysis of Algorithms 263/22/04
Sweep-line algorithmProcess event points in order by sorting segmentendpoints by x-coordinate and looping through:
• For a left endpoint of segment s:• Add segment s to dynamic set S.• Check for intersection between sand its neighbors in S.
• For a right endpoint of segment s:• Remove segment s from dynamic set S.• Check for intersection betweenthe neighbors of s in S.
![Page 27: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/27.jpg)
CS 5633 Analysis of Algorithms 273/22/04
bed
bdc
bdce
bcad
aab
cab
a
b
c
de
f
intersection found
![Page 28: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/28.jpg)
CS 5633 Analysis of Algorithms 283/22/04
Analysis
Use balanced search tree to store dynamic set S.
![Page 29: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/29.jpg)
CS 5633 Analysis of Algorithms 293/22/04
Sweep-line algorithmProcess event points in order by sorting segmentendpoints by x-coordinate and looping through:
• For a left endpoint of segment s:• Add segment s to dynamic set S.• Check for intersection between sand its neighbors in S.
• For a right endpoint of segment s:• Remove segment s from dynamic set S.• Check for intersection betweenthe neighbors of s in S.
O(n)
Ο(log n)
Ο(log n)
Ο(n log n)
![Page 30: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/30.jpg)
CS 5633 Analysis of Algorithms 303/22/04
Analysis
Use balanced search tree to store dynamic set S.Total running time: O(n log n).
Note that the algorithm stops after finding the first intersection point. If we want to report allintersection points, the algorithm can be extendedto run in O((n+k) log n) time, where k is the numberof intersections.
![Page 31: Computational Geometry - Tulane University](https://reader034.vdocuments.us/reader034/viewer/2022051204/6277f6e0a84cc205c7055144/html5/thumbnails/31.jpg)
CS 5633 Analysis of Algorithms 313/22/04
CorrectnessTheorem: If there is an intersection,the algorithm finds it.Proof: Let X be the leftmost intersection point.Assume for simplicity that
• only two segments s1, s2 pass through X, and• no two points have the same x-coordinate.
At some point before we reach X,s1 and s2 become consecutive in the order of S.Either initially consecutive when s1 or s2 inserted,
or became consecutive when another deleted.