580 likes | 901 Views
INTERVAL TREE & SEGMENTATION TREE. Juho Lee, UiTae Kim. UiTae Kim. Interval tree. Can we load them all?. Or even should we?. Windowing Query. Query for finding objects in rectangular region. Let’s consider roads as line segments. Let’s make the example even easier…. Orthogonal.
E N D
INTERVAL TREE & SEGMENTATION TREE Juho Lee, UiTae Kim
UiTae Kim Interval tree
Can we load them all? Or even should we?
Windowing Query • Query for finding objects in rectangular region Let’s consider roads as line segments
Let’s make the example even easier… Orthogonal Only two possible orientations Axis-parallel
Data structure • S: set of n axis-parallel line segments • Query: find all line segments in S, intersecting W. • W:=[x:x’] X [y:y’] • We need a data structure for solving this window query efficiently
Check segment as marked 2-D range search Query time: Storage: Entirely inside Intersects once Intersects twice (partially) overlaps boundary
Lemma 10.1 • Let S be a set of n axis-parallel line segments in the plane. The segments that have at least one endpoint inside an axis-parallel query window Wcan be reported in O(logn+k) time with a data structure that uses O(nlogn) storage and preprocessing time, where k is the number of reported segments.
Now problem is reduced to… Checking whether the given line segments intersects ℓ: intersects ℓ iff Now the problem becomes 1-D intersection test
Finding intersection in 1-D • closed set of intervals • median of 2n endpoints • If , we don’t need to consider about the segments placed to the right of .
Finding intersection in 1-D • Construct binary tree • Root(or interim node): • Left child: Tree made of line segments placed to the left of • Right child: Tree made of line segments placed to the right of
Finding intersection in 1-D • Construct binary tree Make tree recursively
Finding intersection in 1-D • Construct binary tree is in interval iff can be done easily if we have sorted list
Interval tree • If , then the interval tree is leaf • Otherwise, let root be • forms left subtree • : stored twice • : sorted from left to right • : sorted from right to left • forms right subtree
Lemma 10.2 • An interval tree on a set of n intervals uses O(n) storage and has depth O(logn).
Lemma 10.2 • pf) Depth part is trivial, since we are storing 2n endpoints in a binary tree. O(log n)
Lemma 10.2 • pf) For storage bound, note that are all disjoint. • Thus, each segments are stored only twice, one for , and the other for • Hence, the storage bound is O(n). • The tree uses O(n) also.
Algorithm: ConstructIntervalTree • ConstructIntervalTree(I) • Input: A set I of intervals on the real line • Output: The root of an interval tree for I
Algorithm: ConstructIntervalTree 1. if 2. then return an empty leaf 3. elseCreate a node ν. Compute , the median of the set of interval endpoints, and store with ν. 4. Compute and construct two sorted lists for : a list sorted on left endpoint and a list sorted on right endpoint. Store these two lists at ν. 5. lc(ν)← ConstructIntervalTree() 6. rc(ν)← ConstructIntervalTree() 7. returnv
Algorithm: ConstructIntervalTree • Finding median: O(n), but better to keep sorted list. O(nlogn) • Let ,then creating takes • Each step takes • Using similar arguments in Lemma 10.2, .
Lemma 10.3 • An interval tree on a set of n intervals can be built in O(nlogn) time.
Algorithm: QueryIntervalTree • QueryIntervalTree(v, ) • Input: The root v of a interval tree and a query point • Output: All intervals containing
Algorithm: QueryIntervalTree 1. ifν is not a leaf 2. then if< 3. thenWalk along the list , starting at the interval with the leftmost endpoint, reporting all the intervals that contain . Stop as soon as an interval does not contain . 4. QueryIntervalTree(lc(ν),) 5. elseWalk along the list , starting at the interval with the rightmost endpoint, reporting all the intervals that contain . Stop as soon as an interval does not contain . 6. QueryIntervalTree(rc(ν),)
Algorithm: QueryIntervalTree • For each step, , where is the # of pts reporting • We go through steps, and . • Query time is
Theorem 10.4 • An interval tree for a set I of n intervals uses O(n) storage and can be built in O(nlogn) time. Using the interval tree we can report all intervals that contain a query point in O(logn+k) time, where k is the number of reported intervals.
Interval Tree Query time: Storage: 2-D range search Query time: Storage: Entirely inside Intersects once Intersects twice (partially) overlaps boundary
Extend query to segment • Let be the set of horizontal segments in . • Query becomes . • looks like .
Extend query to segment • Recursively traveling through the left(right) subtree is still correct
Extend query to segment • However, treat for the is no longer correct Nothing but a range query [)if
Extend query to segment • However, treat for the is no longer correct • Perform range search for the left(right) endpoints • Query is ] (or [) • Uses storage, query time
Data structure • Main structure: interval tree on x-intervals of segments • Instead of , we have range search tree .
Data structure Replaced to range tree
Data structure • Range tree takes , instead of . • Total time for each step becomes • Storage complexity becomes , since range tree dominates the main interval tree • Preprocessing time remains
Theorem 10.5 • Let S be a set of n horizontal segments in the plane. The segments intersecting a vertical query segment can be reported in time with a data structure that uses storage, where k is the number of reported segments. The structure can be built in time.
Lemma 10.1 • Let S be a set of n axis-parallel line segments in the plane. The segments that have at least one endpoint inside an axis-parallel query window Wcan be reported in O(logn+k) time with a data structure that uses O(nlogn) storage and preprocessing time, where k is the number of reported segments.
Corollary 10.6 • Let S be a set of n axis-parallel segments in the plane. The segments intersecting an axis-parallel rectangular query window can be reported in time with a data structure that uses storage, where k is the number of reported segments. The structure can be built in time.
Summary • Query Time: • Storage: • Preprocessing:
Jooho Lee Segment Tree
Problem • Given n segments, find intervals which contain qx. • Let solve it with BST. qx
Elementary Intervals (): Open Interval[]: Closed Interval • At most 4n+1 elementary Intervals.2n(end points)+2n(segments)+1(right side) { (p5:p6) (p3:p4) (p1:p2) (p6:+∞) } (-∞:p1) (p2:p3) (p4:p5) [p4:p4] [p3:p3] [p5:p5] [p6:p6] [p2:p2] [p1:p1] =Elementary Intervals I
BST of Elementary Intervals • Height of BST: O(log(4n+1))=O(log n) u [p3:p3] [p2:p2] (p3:p4) (p1:p2) (p2:p3) (-∞:p1) [p1:p1] (p1:p2) (p3:p4) (-∞:p1) (p2:p3) (p4:∞) [p4:p4] [p3:p3] [p2:p2] [p1:p1] (p4:∞) [p4:p4] Let Int(u) corresponding Interval with u.Int(u) is (p1:p2) interval.
BST of Elementary Intervals • At leaf node u, Store interval I containing Int(u). • Query time: log(n)+k1.find the leaf interval containing qx2.report all intervals stored at the corresponding leaf node. • But Storage can be quadratic..O(n2) u [p3:p3] [p2:p2] (p3:p4) (p1:p2) (p2:p3) (-∞:p1) [p1:p1] (p4:∞) [p4:p4]
Qaudratic Storage Example storages for open leaf intervals :0 1 2 . . . . . . . . .N-1 N N-1 . . . . . . . . 2 1 0 Sum of storages for open leaf intervals is more than N(N+1)/2!
Idea • Internal node w = union of elemental intervals of leaves in the sub-tree(w)! • Segment Tree v u Int(u) Int(v) [p3:p3] [p2:p2] (p3:p4) (p1:p2) (p2:p3) (-∞:p1) [p1:p1] (p1:p2) (p3:p4) (-∞:p1) (p2:p3) (p4:∞) [p4:p4] [p3:p3] [p2:p2] [p1:p1] (p4:∞) [p4:p4]
Which interval is contained at node v? • Each node v store the Interval such that • If ,[x:x`] is stored at parent(v) or parentn(v)