Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTERVAL TREE & SEGMENTATION TREE Juho Lee, UiTae Kim.

Similar presentations


Presentation on theme: "INTERVAL TREE & SEGMENTATION TREE Juho Lee, UiTae Kim."— Presentation transcript:

1 INTERVAL TREE & SEGMENTATION TREE Juho Lee, UiTae Kim

2 INTERVAL TREE UiTae Kim

3

4

5 Windowing Query  Query for finding objects in rectangular region Let’s consider roads as line segments

6 Let’s make the example even easier… Only two possible orientations Axis-parallel Orthogonal

7 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

8 1.Entirely inside 2.Intersects once 3.Intersects twice 4.(partially) overlaps boundary Check segment as marked

9 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 W can 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.

10

11 Now problem is reduced to… Now the problem becomes 1-D intersection test

12 Finding intersection in 1-D

13

14  Construct binary tree Make tree recursively

15 Finding intersection in 1-D  Construct binary tree

16 Interval tree

17

18 Lemma 10.2  An interval tree on a set of n intervals uses O(n) storage and has depth O(logn).

19 Lemma 10.2  pf) Depth part is trivial, since we are storing 2n endpoints in a binary tree. O(log n)

20 Lemma 10.2

21 Algorithm: ConstructIntervalTree  ConstructIntervalTree(I)  Input: A set I of intervals on the real line  Output: The root of an interval tree for I

22 Algorithm: ConstructIntervalTree

23

24 Lemma 10.3  An interval tree on a set of n intervals can be built in O(nlogn) time.

25 Algorithm: QueryIntervalTree

26

27

28 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.

29 1.Entirely inside 2.Intersects once 3.Intersects twice 4.(partially) overlaps boundary

30 Extend query to segment

31  Recursively traveling through the left(right) subtree is still correct

32 Extend query to segment

33

34 Data structure

35 Replaced to range tree

36 Data structure

37 Theorem 10.5

38 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 W can 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.

39 Corollary 10.6

40 Summary

41 SEGMENT TREE Jooho Lee

42 Problem  Given n segments, find intervals which contain qx.  Let solve it with BST. qx

43 Elementary Intervals (-∞:p1) (p1:p2) (p2:p3) (p3:p4) (p4:p5) (p5:p6) (p6:+∞) [p1:p1] [p2:p2] [p3:p3] [p4:p4] [p5:p5][p6:p6] (): Open Interval []: Closed Interval =Elementary Intervals I { }  At most 4n+1 elementary Intervals. 2n(end points)+2n(segments)+1(right side)

44 BST of Elementary Intervals (-∞:p1) (p1:p2) (p2:p3) (p3:p4) (p4:∞) [p1:p1] [p2:p2] [p3:p3] [p4:p4] u (-∞:p1)[p1:p1] [p2:p2](p1:p2) (p2:p3) [p3:p3] (p3:p4) [p4:p4] (p4:∞) Let Int(u) corresponding Interval with u. Int(u) is (p1:p2) interval.  Height of BST: O(log(4n+1))=O(log n)

45 BST of Elementary Intervals  At leaf node u, Store interval I containing Int(u).  Query time: log(n)+k 1.find the leaf interval containing qx 2.report all intervals stored at the corresponding leaf node.  But Storage can be quadratic..O(n 2 ) u (-∞:p1)[p1:p1] [p2:p2] (p1:p2) (p2:p3) [p3:p3] (p3:p4) [p4:p4] (p4:∞)

46 Qaudratic Storage Example storages for open leaf intervals : N-1 N N Sum of storages for open leaf intervals is more than N(N+1)/2!

47 Idea  Internal node w = union of elemental intervals of leaves in the sub-tree(w)!  Segment Tree (-∞:p1) (p1:p2) (p2:p3) (p3:p4) (p4:∞) [p1:p1] [p2:p2] [p3:p3] [p4:p4] Int(u) Int(v) v u (-∞:p1)[p1:p1] [p2:p2] (p1:p2) (p2:p3) [p3:p3] (p3:p4) [p4:p4] (p4:∞)

48 Which interval is contained at node v?  Each node v store the Interval such that  If, [x:x`] is stored at parent(v) or parent n (v)

49 Storage in Segment Tree

50 Construction of Segment Tree

51 Construction  There is a node to split a path.  Once the paths diverge, as we follow the left path, whenever the path goes to the left child, the right child must store [x:x`].  Similarly, as we follow the right path, whenever the path goes to the right child, the left child must store [x:x`].  So, For one interval insertion, we just travel only at most two pat hs.  Since Height is O(log N), time complexity to build segment tree will be O(N log N)

52 Reporting Query with Segment Tree qx v lc(v)rc(v) Int(lc(v)) Int(v) O(log n+k) for a query!

53 Storage Complexity  A segment tree on a set of n intervals uses O(n log n) storages.  Height of Tree H: O(log n)  For any, contained by at most two nodes at the same depth.  Therefore, (number of segments)*(2*H) = O(n log n)! If v1,v3 contain [x:x`], pa rent(v2) contain [x:x`]. Contradiction!

54 N log N storage Example  N/2 segments for filling points.  N/2 segments for storing log n times.  Then N/2* log N=O(NlogN) for storing!

55 Summary  Query Time: O(log n + k)  Storage: O(n log n)  Preprocessing: O(n log n)

56 Quiz  For given n integers, give a algorithm to handle queries whic h is to calculate sum of interval (a, b).

57 Hint  Use segment tree.

58 Solution  Get rid of closed interval  For each node v, calculate total of Int(v).  Similar way with previous method.


Download ppt "INTERVAL TREE & SEGMENTATION TREE Juho Lee, UiTae Kim."

Similar presentations


Ads by Google