Geometric Seraching
Geometric Seraching
Problem: How many railway bridges are to be built in the crossing of rivers
and what is their locations?
L6
L5
L7
L3
L4
L1
L2
Dormant: L7 L3
L4
L1
L2
L2, L3, L4, L5, L6 are active and are stored into Y-structure in this order.
• Y-structure is a balanced binary search tree. So, dictionary operations are in
O(log n) time.
• Leaves store the segments from left to right in descending order of y-
coordinates.
• Other node (root/intermediate) is the right-most leaf in its left-subtree.
L6 L4
L5 L6 L3
L7
L3
L5 L3 L2
L4 L6
L1 L2
L5 L4
X-structure: Contains all endpoints of line segments which are to the right of the sweep line.
Furthermore, it contains some of the intersections of the line segments to the right of the sweep
line.
The points in the X-structure are sorted according to their X-coordinate, i.e, the X-structure is a
heap.
5 8
2
3 7
1 6
4
5
a
1 b
8
Problem: Given a set of segments on a plane, determine whether any two line
segments intersect.
Assumption 1: No input segment is vertical.
Assumption 2: No more than two input segments intersect at a single point.
Ordering segments: Order the segments that intersect a vertical sweep line
according to the y-coordinate of the points of intersection.
Step 1. T = Φ
Step 2. Sort the endpoints of segments in S from left to right, breaking ties by
putting points with lower y-coordinates first.
Step 3. For each point p in the sorted list of end points do
(a): If p is the left end point of a segment S then
INSERT (T, S)
If (ABOVE (T, S) exists and intersect S or (BELOW (T, S)
exists and intersect S then
return TRUE.
(b): If p is the right end point of a segment S then
If both ABOVE (T, S) and BELOW (T, S) exist and ABOVE (T, S)
intersects BELOW (T, S) then
returnTRUE
DELETE (T, S)
Step 4. Return FALSE.
Time Complexity:
Step 1 takes O(1) time.
Step 2 O(n log n ) time.
Since there are 2n event points, for loop takes at most 2n iteration. Each iteration
take of (log n) time.
Therefore the for loop takes O(n log n) time.
Hence overall time is O(n log n).
An Illustration
a d e
c
f
a a a d d e
e
b c a c d
d
b c b c
b
b b
Exercise 1: Apply Plane sweep algorithm to determine whether any two line
segments intersect in the following graph.
L3
L2 L6
L5
L1
L4