Presentation is loading. Please wait.

Presentation is loading. Please wait.

Segment Trees Basic data structure in computational geometry.

Similar presentations


Presentation on theme: "Segment Trees Basic data structure in computational geometry."— Presentation transcript:

1 Segment Trees Basic data structure in computational geometry.
Computations with geometric objects. Points in 1-, 2-, 3-, d-space. Closest pair of points. Nearest neighbor of given point. Lines in 1-, 2-, 3-, d-space. Machine busy intervals. IP router-table filters (10*, [20, 60]). Closest pair of points in 3D  track aircraft. 2D  verify that all holes are sufficiently far apart in a design Objects on earth (longitude and latitude). Nearest neighbor  find nearest gas station to current location. Nearest ship (longitude and latitude). IP router table find shortest interval that contains destination address (point). 10* matches all destinations addresses that begin with the bits 10. If addresses are 4 bits long, then 10* matches addresses of the form 10??. I.e., all in the range [8, 11]. [20,60] matches all addresses between 20 and 60. IP router-table rule is [filter, action] where action could be drop packet or send to a particular next machine. Find all lines that contain the point given by (dest addr), find shortest such line (longest matching filter), find highest-priority line.

2 Segment Trees Rectangles or more general polygons in 2-space.
VLSI mask verification. Sentry location. 2-D firewall filter. (source address, destination address) (10*, 011*) When addresses are 4 bits long this filter matches addresses in the rectangle ([8,11], [6,7]) 8 11 6 7 Source address Sufficient overlap between rectangles that represent wires and contact points on components of a VLSI design. Building represented as (say) rectilinear polygon. Place sentries either on interior or exterior so that all of building interior or exterior is visible by at least one sentry. Finding most-specific matching rule requires finding the smallest rectangle that contains the point given by the packet’s (source,dest). Detect conflicting 2-d filters requires rectangle intersection detection.

3 Segment Tree Application
Store intervals of the form [i,j], i < j, i and j are integers. [i,j] may, for example represent the fact that a machine is busy from time i to time j. Answer queries of the form: which intervals intersect/overlap with a given unit interval [a,a+1]. List all machines that are busy from 2 to 3. Equivalently, which intervals contain the unit interval [a,a+1]? An alternative format stores intervals of the form (a,b] and answers point containment queries. [a,b] becomes (a-1,b].

4 Segment Tree – Definition
Binary tree. Each node, v, represents a closed interval. s(v) = start of v’s range. e(v) = end of v’s range. s(v) < e(v). s(v) and e(v) are integers. Root range = [1,n]. e(v) = s(v) + 1 => v is a leaf node (unit interval). e(v) > s(v) + 1 => Left child range is [s(v), (s(v) + e(v))/2]. Right child range is [(s(v) + e(v))/2, e(v)].

5 Example – Root range = [1,13]
1,7 7,13 1,4 4,7 7,10 10,13 7,8 8,10 10,11 11,13 1,2 2,4 4,5 5,7 2,3 3,4 8,9 9,10 11,12 12,13 5,6 6,7 Cream colored boxes are leaves/unit intervals.

6 Store Interval [3,11] 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 Node’s interval list may be stored as a chain/doubly-linked list or in some other fashion depending on application. May be a max heap for max-priority matching. Unit intervals of [3,11] highlighted. Each interval [i,j], i < j, is stored in one or more nodes of the segment tree.

7 Store Interval [3,11] 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 4,7 7,10 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 10,11 3,4 [3,11] is stored in node p iff all leaves in the subtree rooted at p are highlighted and no ancestor of p satisfies this property.

8 Store Interval [4,10] 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 4,7 7,10 1,2 2,4 2,3 4,5 5,7 5,6 6,7 7,8 8,10 11,13 11,12 12,13 8,9 9,10 Each node of a segment tree contains 0 or more intervals.

9 Which Nodes Are Stored? 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 Need to store only those nodes that contain intervals plus the ancestors of these nodes.

10 Segment Tree Height 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 Range = [1,n] => Height <= ceil(log2 (n-1)) + 1.

11 Properties 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 This property is actually part of the definition of a segment tree. [i,j] in node v => [i,j] not in any ancestor of v .

12 Properties 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 This property is actually part of the definition of a segment tree. [i,j] in node v => [i,j] not in sibling of v .

13 Properties 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 3 or more nodes  2 siblings between leftmost and rightmost have the interval Suppose 3 nodes A, B, C on same level have interval I. Suppose A left of B left of C. All descendants of B’s sibling must be part of I and so B cannot have I in it; must be at an ancestor of B. [i,j] may be in at most 2 nodes at any level. Each interval is in O(log n) nodes.

14 Top-Down Insert — [3,11] 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 4,7 7,10 10,11 3,4

15 Top-Down Insert insert(s, e, v)
{// insert [s,e] into subtree rooted at v if (s <= s(v) && e(v) <= e) add [s,e] to v; // interval spans node range else { if (s < (s(v) + e(v))/2) insert(s,e,v.leftChild); if (e > (s(v) + e(v))/2) insert(s,e,v.rightChild); }

16 Complexity Of Insert 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 10,11 3,4 Let L and R, respectively, be the leaves for [s,s+1] and [e – 1,e].

17 Complexity Of Insert 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 The range of other child of each ancestor is either disjoint from the range being inserted or is contained in this range. In the worst-case, L, R, all ancestors of L and R, and possibly the other child of each of these ancestors are reached.

18 Complexity Of Insert 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 Complexity analysis assumes add to each node list takes O(1) time. Complexity is O(log n).

19 Top-Down Delete delete(s, e, v)
{// delete [s,e] from subtree rooted at v if (s <= s(v) && e(v) <= e) delete [s,e] from v; // interval spans node range else { if (s < (s(v) + e(v))/2) delete(s,e,v.leftChild); if (e > (s(v) + e(v))/2) delete(s,e,v.rightChild); } Complexity is O(log n) if delete from each node list can be done in O(1) time. This is possible if we have a doubly linked list in each node together with a table that stores a list of all dbl list nodes that have this segment. But then, you don’t need above scheme to do the delete.

20 Search – [a,a+1] Follow the unique path from the root to the leaf node for the interval [a,a+1]. Report all segments stored in the nodes on this unique path. No segment is reported twice, because no segment is stored in both a node and the ancestor of this node.

21 Search – [5,6] 1,13 1,7 7,13 1,4 4,7 7,10 10,13 1,2 2,4 2,3 3,4 4,5 5,7 5,6 6,7 7,8 8,10 10,11 11,13 11,12 12,13 8,9 9,10 Variants of segment trees … Leaves are points and internal nodes are open-closed intervals (3,6]. Use for point queries … find all segments that contain the point 4. … layered segment tree used to detect intersections of rectilinear line segments (given a collection of orthogonal lines find all intersections with a query line that is either horizontal or vertical). 5,6 O(log n + s), where s is the # of segments in the answer.


Download ppt "Segment Trees Basic data structure in computational geometry."

Similar presentations


Ads by Google