27
COSC 6114 Prof. Andy Mirzaian

COSC 6114

Embed Size (px)

DESCRIPTION

COSC 6114. Prof. Andy Mirzaian. More Geometric Data Structures: Windowing. References :. [M. de Berge et al] chapter 10. Data Structures: Interval Trees Priority Search Trees Segment Trees. Applications : Windowing Queries Vehicle navigation systems - PowerPoint PPT Presentation

Citation preview

Page 1: COSC 6114

COSC 6114

Prof. Andy Mirzaian

Page 2: COSC 6114

References: • [M. de Berge et al] chapter 10

Applications: • Windowing Queries• Vehicle navigation systems• Geographic Information Systems• Flight simulation in computer graphics• CAD/CAM of printed circuit design

Data Structures:• Interval Trees

• Priority Search Trees

• Segment Trees

Page 3: COSC 6114

Windowing

PROBLEM 1: Preprocess a set S of non-crossing line-segments in the plane for efficient query processing of the following type:Query: given an axis-parallel rectangular query window W,

report all segments in S that intersect W.

Page 4: COSC 6114

Windowing

PROBLEM 2: Preprocess a set S of horizontal or vertical line-segments in the plane for efficient query processing of the following type:Query: given an axis-parallel rectangular query window W,

report all segments in S that intersect W.

Page 5: COSC 6114

INTERVAL TREES

W

PROBLEM 2: Preprocess a set S of horizontal or vertical line-segments in the plane for efficient query processing of the following type:Query: given an axis-parallel rectangular query window W,

report all segments in S that intersect W.

Page 6: COSC 6114

INTERVAL TREES

SUB-PROBLEM 1.1 & 2.1: Let S be a set of n line-segments in the plane. Given an axis-parallel query window W, the segments of S that have at least one end-point inside W can be reported in O(K + log n) time with a data structure that uses O(n log n) space and O(n log n) preprocessing time, where K is the number of reported segments.

Method: Use 2D Range Tree on segment end-points and fractional cascading.

Page 7: COSC 6114

INTERVAL TREES

SUB-PROBLEM 2.2: Preprocess a set SH of horizontal line-segments in the plane, so that the subset ofSH that intersects a query vertical line can be reported efficiently.

Method: Use Interval Trees.

Now consider horizontal (similarly, vertical) segments in S that intersect W, but their end-points are outside W.They must all cross the left edge of W.

W

Page 8: COSC 6114

INTERVAL TREES

Associated structure for Imed :

L left = list of segments in Imed sorted by their left end-points,

L right = list of segments in Imed sorted by their right end-points.

xmed

Imed

IrightIleft

Imed

Ileft Iright

1

2 3

4

5

67

L left = 3,4,5

L left =1,2 L left =6,7

L right = 5,3,4

L right = 1,2 L right = 7,6

Page 9: COSC 6114

INTERVAL TREES

THEOREM: Interval Tree for a set of n horizontal intervals:• O(n) storage space• O(n log n) construction time• O(K + log n) query time

[report all K data intervals that contain a query x-coordinate.]

Page 10: COSC 6114

INTERVAL TREES

SUB-PROBLEM 2.3: Now instead of the query being on a vertical line, suppose it is on a vertical line-segment.

q(qx, q’y)

(qx, qy)

The primary structure of Interval Trees is still valid.Modify the associated secondary structure.

SOLUTION:

L left = Range Tree on left end-points of Imed ,

L right = Range Tree on right end-points of Imed .

q(-: qx] [qy : q’y]

xmed

Page 11: COSC 6114

INTERVAL TREES

THEOREM: Interval Tree for a set of n horizontal intervals:• O(n log n) storage space• O(n log n) construction time• O(K + log2 n) query time

[report all K data intervals that intersect a query vertical line-segment.]

COROLLARY: Let S be a set of n horizontal or vertical line-segments in the plane. We can preprocess S for axis-parallel rectangular query window intersection with the following complexities:

• O(n log n) storage space• O(n log n) construction time• O(K + log2 n) query time

[report all K data intervals that intersect the query window.]

Page 12: COSC 6114

PRIORITY SEARCH TREES

P = {p1, p2, … , pn } 2.

A Priority Search Tree (PST) T on P is:

• a binary tree, one point per node, • heap-ordered by x-coordinates, • (almost) symmetrically ordered by y-coordinates.

Improving the previous solution:the associated structure can be implemented by Priority Search Trees, instead of Range Trees.

Page 13: COSC 6114

PRIORITY SEARCH TREESpmin point in P with minimum x-coordinate.ymin min y-coordinate of points in Pymax max y-coordinate of points in P

P’ P – {pmin}ymed y-median of points in P’Pbelow { p P’ | py ymed }Pabove { p P’ | py > ymed }

PSTon

Pbelow

PSTon

Pabove

pmin

ymin , ymax

p1

y4 y3

p3

y6 y3

p2

y4 y2

p7

y7 y7

p6

y6 y6

p5

y5 y5

p4

y4 y4

p1

p2

pmin

ymed

p3

p6

p7

p4

p5

Page 14: COSC 6114

PRIORITY SEARCH TREES

Priority Search Tree T on n points in the plane requires:

• O(n) storage space

• O(n log n) construction time: either recursively, or pre-sort P on y-axis, then construct T

in O(n) time bottom-up. (How?)

Priority Search Trees can replace the secondary structures (range trees) in Interval Trees.

• simpler (no fractional cascading)

• linear space for secondary structure.

Page 15: COSC 6114

How to use PST to search for a query range R = (-: qx] [qy : q’y] ?

ALGORITHM QueryPST (v, R)if v = nil or pmin x(v) > qx or ymin(v) > q’y or ymax(v) < qy

then returnif pmin x (v) qx and qy ymin(v) ymax(v) q’y then Report.In.Subtree (v, qx) else do if pmin x (v) R then report pmin x(v)

QueryPST (lc(v), R) QueryPST (rc(v), R) end elseend

q’y

qy

R

ymax

ymin

pmin

PROCEDURE Report.In.Subtree (v, qx)if v=nil then returnif pmin x (v) qx then do report pmin x(v)

Report.In.Subtree (lc(v), qx) Report.In.Subtree (rc(v), qx)end ifend

Truncated Pre-Order on the Heap: O(1 + Kv) time.

v

PST

T vqx

Page 16: COSC 6114

LEMMA: Report.In.Subtree(v, qx) takes O(1 + Kv) time to report all points in the subtree rooted at v whose x-cooridnate is qx , where Kv is the number of reported points.

THEOREM: Priority Search Tree for a set P of n points in the plane has complexities:

• O(n) Storage space• O(n log n) Construction time• O(K + log n) Query time

[report all K points of P in a query range R = (-: qx] [qy : q’y] .]

qy q’y

PST

Page 17: COSC 6114

SEGMENT TREES

Back to Problem 1: Arbitrarily oriented line segments.

Solution 1: Bounding box method.

W W

Bad worst-case. Many false hits.

Page 18: COSC 6114

SEGMENT TREES

Back to Problem 1: Arbitrarily oriented line segments.

Solution 2: Use Segment Trees.a) Segments with end-points in W can be reported using range trees (as before).b) Segments that intersect the boundary of W can be reported by Segment Trees.

SUB-PROBLEM 1.1: Preprocess a set S of n non-crossing line-segments in the plane into a data structure to report those segments in S that intersect a given vertical query segment q = qx [qy : q’y] efficiently.

Page 19: COSC 6114

Elementary x-intervals of S

(-: p1), [p1 : p1], (p1 : p2), [p2 : p2], … , (pm-1 : pm), [pm : pm], (pm : +).

Build a balanced search tree with each leaf corresponding (left-to-right) to an elementary interval (in increasing x-order).

Leaf v: Int(v) = set of intervals (in S) that contain the elementary interval

corresponding to v.

SEGMENT TREESp1 p2 p3 pm. . .

IDEA 1: Store Int(v) with each leaf v.

Storage O(n2), because intervals in S that span many elementary intervals will be stored in many leaves.

Page 20: COSC 6114

SEGMENT TREES

s1

s2 s3 s4

IDEA 2: internal node v: Int(v) = union of elementary intervals corresponding to the leaf-descendents of v.

Store an interval [x:x’] of S at a node v iff Int(v) [x:x’] but Int(parent(v)) [x:x’].Each interval of S is stored in at most 2 nodes per level (i.e., O(log n) nodes).Thus, storage space reduces to O(n log n).

s5

s1

s1 s1

s5

s3

s4

s3

s2,s5

s2,s5

s3,s4

What should theassociated structure be?

Page 21: COSC 6114

SEGMENT TREES

s1

s2

s3

s4

s5

v1

v2v3

S(v1) = {s3}

S(v2) = {s1 , s2}S(v3) = {s5 , s7}

s6

s7

Page 22: COSC 6114

SEGMENT TREES

Associated structure is a balanced search tree based on the vertical orderingof segments S(v) that cross the slab Int(v) (- : +).

s1

s2

s3

s4

s5

s6

s6

s4

s2

s5

s1

s3

Page 23: COSC 6114

SEGMENT TREES

THEOREM: Segment Tree for a set S of n non-crossing line-segments in the plane:

• O(n log n) Storage space• O(n log n) Construction time• O(K + log2 n) Query time

[report all K segments of S that intersect a vertical query line-segment.

COROLLARY: Segment Trees can be used to solve Problem 1 with the above complexities. That is, the above complexities applies if the query is with respect to an axis-parallel rectangular window.

Page 24: COSC 6114

Exercises

Page 25: COSC 6114

1. Let P be a set of n point sin the plane, sorted on y-coordinate. Show that, because S is sorted,a Priority Search Tree of the points in P can be constructed in O(n) time.

2. Windowing queries in sets of non-crossing segments are performed using range query on the set of end-points and intersection queries with the four boundary edges of the query window. Explain how to avoid reporting segments more than once. To this end, make a list of all possible ways in which an arbitrary oriented segment can intersect a query window.

3. Segment Trees can be used for multi-level data structures. (a) Let R be a set of n axis-parallel rectangles in the plane. Design a data structure for R such that the rectangles in R that contain a query point q can be reported efficiently. Analyze the amount of storage and the query time of your data structure. [Hint: Use a segment tree on the x-intervals of the rectangles, and store canonical subsets of the nodes in this segment tree in an appropriate associated structure.] (b) Generalize this data structure to d-dimensional space. Here we are given a set of axis- parallel hyper-rectangles, i.e., polytopes of the form [x1 : x’1] [x2 : x’2] … [xd : x’d] , and we want to report the hyper-rectangles that contain a query point.

4. Let I be a set of intervals on the real line. We want to store these intervals such that we can efficiently determine (a) those intervals that are completely contained in a given interval [x : x’]. Describe a data structure that uses O(n log n) storage and solves such queries in O(K + log n) time, where K is the output size. [Hint: Use a range tree.] (b) The same question as in part (a), except that we want to report the intervals that contain a query interval [x : x’].

Page 26: COSC 6114

5. Consider the following alternative approach to solve the 2-dimensional range searching problem: We construct a balanced search tree on the x-coordinate of the points. For a node v in the tree, let P(v) be the set of points stored in the subtree rooted at v. For each node v we store two associated priority search trees of P(v), a tree T left allowing for range queries that are unbounded to the left, and a tree T right for range queries that are unbounded to the right. A query with a range [x : x’] [y : y’] is performed as follows. We search for the node vsplit

where the search paths toward x and x’ split in the tree. Now we perform a query with range [x : + ) [y : y’] on T right(lc(v)) and a query with range (- : x’] [y : y’] on T left(rc(v)). This gives all the answers (there is no need to search further down in the tree!). (a) Work out the details of this approach. (b) Prove that the data structure correctly solves range queries. (c) What are the bounds for preprocessing time, storage space, and query time of this structure? Prove your answers.

Page 27: COSC 6114

END