Download presentation

1
**INTERVAL TREE & SEGMENTATION TREE**

Juho Lee, UiTae Kim

2
**Interval tree UiTae Kim**

Hi, this time we will learn about the interval trees and segment trees I’m Uitae Kim, and let’s start with the interval trees

3
**When you drive, how can you find the path?**

With the navigation. Then, what is the data we need to consider for navigation?

4
**Or even should we? Can we load them all? A map. A huge map.**

In a view of making navigation, Can we load this entire map? Or even should we? Of course not. We need data only near the driver.

5
**Windowing Query Query for finding objects in rectangular region**

It can be done by solving windowing query Windowing query is used for find some objects in given rectangular region How can we do that? By looking through all the map we have? It is not that better than load the entire map data. So we have to think about how to solve the windowing query efficiently To do so, let’s consider roads as line segments Let’s consider roads as line segments

6
**Let’s make the example even easier…**

Orthogonal Only two possible orientations To start with the easiest example, let’s make it even easier. There are only two kinds of line segments are accepted, One is parallel to the x-axis, and the other is orthogonal Axis-parallel

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
**2-D range search Query time: 𝑂( log 𝑛 +𝑘) Storage: 𝑂(𝑛 log 𝑛 )**

Check segment as marked 2-D range search Query time: 𝑂( log 𝑛 +𝑘) Storage: 𝑂(𝑛 log 𝑛 ) It can be done with 2d range search, as we learn in ch5. Entirely inside Intersects once Intersects twice (partially) overlaps boundary

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
𝑙 ≔(𝑥= 𝑞 𝑥 ) Then what about the case that has both endpoints are placed outside the window? Since line segments that has at least one endpoint inside are already marked, We can figure the lines that intersects twice by only checking the intersection with l. To get some more insight, let’s just consider l to be a full line.

11
**Now problem is reduced to…**

Checking whether the given line segments intersects ℓ: 𝑠≔ 𝑥,𝑦 ( 𝑥 ′ ,𝑦) intersects ℓ iff 𝑥≤ 𝑞 𝑥 ≤𝑥′ Now the problem becomes 1-D intersection test

12
**Finding intersection in 1-D**

𝐼≔{ 𝑥 1 : 𝑥 1 ′ , 𝑥 2 : 𝑥 2 ′ ,…, 𝑥 𝑛 : 𝑥 𝑛 ′ } closed set of intervals 𝑥 𝑚𝑖𝑑 median of 2n endpoints If 𝑞 𝑥 < 𝑥 𝑚𝑖𝑑 , we don’t need to consider about the segments placed to the right of 𝑥 𝑚𝑖𝑑 .

13
**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 𝑥 𝑚𝑖𝑑

14
**Finding intersection in 1-D**

Construct binary tree Make tree recursively

15
**Finding intersection in 1-D**

Construct binary tree 𝑞 𝑥 is in interval 𝑥 𝑗 : 𝑥 𝑗 ′ ∈ 𝐼 𝑚𝑖𝑑 iff 𝑥 𝑗 ≤ 𝑞 𝑥 can be done easily if we have sorted list

16
**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

17
Interval tree

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

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**

1. if 𝐼=∅ 2. then return an empty leaf 3. else Create 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. return v

23
**Algorithm: ConstructIntervalTree**

Finding median: O(n), but better to keep sorted list. O(nlogn) Let 𝑛 𝑚𝑖𝑑 ≔𝑐𝑎𝑟𝑑( 𝐼 𝑚𝑖𝑑 ), then creating ℒ 𝑙𝑒𝑓𝑡 𝑣 , ℒ 𝑟𝑖𝑔ℎ𝑡 𝑣 takes 𝑂( 𝑛 𝑚𝑖𝑑 log 𝑛 𝑚𝑖𝑑 ) Each step takes 𝑂(𝑛+ 𝑛 𝑚𝑖𝑑 log 𝑛 𝑚𝑖𝑑 ) Using similar arguments in Lemma 10.2, 𝑂(𝑛 log 𝑛 ).

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

25
**Algorithm: QueryIntervalTree**

QueryIntervalTree(v, 𝑞 𝑥 ) Input: The root v of a interval tree and a query point 𝑞 𝑥 Output: All intervals containing 𝑞 𝑥

26
**Algorithm: QueryIntervalTree**

1. if ν is not a leaf 2. then if 𝑞 𝑥 < 𝑥 𝑚𝑖𝑑 (𝜈) 3. then Walk 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. else Walk along the list 𝐿 𝑟𝑖𝑔ℎ𝑡 (𝜈), starting at the interval with the rightmost endpoint, reporting all the intervals that contain 𝑞 𝑥 . 6. QueryIntervalTree(rc(ν), 𝑞 𝑥 )

27
**Algorithm: QueryIntervalTree**

For each step, 𝑂(1+ 𝑘 𝑣 ), where 𝑘 𝑣 is the # of pts reporting We go through 𝑂( log 𝑛 ) steps, and 𝑘 𝑣 =𝑘. Query time is 𝑂( log 𝑛 + 𝑘 𝑣 )

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
**Interval Tree 2-D range search**

Query time: 𝑂( log 𝑛 +𝑘) Storage: 𝑂(𝑛) 2-D range search Query time: 𝑂( log 𝑛 +𝑘) Storage: 𝑂(𝑛 log 𝑛 ) It can be done with 2d range search, as we learn in ch5. Entirely inside Intersects once Intersects twice (partially) overlaps boundary

30
**Extend query to segment**

Let 𝑆 𝐻 ⊆𝑆 be the set of horizontal segments in 𝑆. Query becomes 𝑞≔ 𝑞 𝑥 ×[ 𝑞 𝑦 : 𝑞 𝑦 ′ ]. 𝑠∈ 𝑆 𝐻 looks like 𝑠≔[ 𝑠 𝑥 : 𝑠 𝑥 ′ ]× 𝑠 𝑦 .

31
**Extend query to segment**

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

32
**Extend query to segment**

However, treat for the 𝐼 𝑚𝑖𝑑 is no longer correct Nothing but a range query [ 𝑞 𝑥 :+∞)×[ 𝑞 𝑦 : 𝑞 𝑦 ′ ] if 𝑥 𝑚𝑖𝑑 < 𝑞 𝑥

33
**Extend query to segment**

However, treat for the 𝐼 𝑚𝑖𝑑 is no longer correct Perform range search for the left(right) endpoints Query is (−∞: 𝑞 𝑥 ]×[ 𝑞 𝑦 : 𝑞 𝑦 ′ ] (or [ 𝑞 𝑥 :+∞)×[ 𝑞 𝑦 : 𝑞 𝑦 ′ ]) Uses 𝑂( 𝑛 𝑚𝑖𝑑 log 𝑛 𝑚𝑖𝑑 ) storage, 𝑂( log 𝑛 𝑚𝑖𝑑 +𝑘) query time

34
Data structure Main structure: interval tree Τ on x-intervals of segments Instead of ℒ 𝑙𝑒𝑓𝑡 (𝑣), ℒ 𝑟𝑖𝑔ℎ𝑡 (𝑣), we have range search tree Τ 𝑙𝑒𝑓𝑡 (𝑣), Τ 𝑟𝑖𝑔ℎ𝑡 (𝑣).

35
Data structure Replaced to range tree

36
**Data structure Range tree takes 𝑂( log 𝑛 𝑚𝑖𝑑 +𝑘), instead of 𝑂( 𝑘 𝑣 ).**

Total time for each step becomes 𝑂( log 2 𝑛 +𝑘) Storage complexity becomes 𝑂(𝑛 log 𝑛 ), since range tree dominates the main interval tree Preprocessing time remains 𝑂(𝑛 log 𝑛 )

37
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 𝑂( log 2 𝑛 +𝑘) time with a data structure that uses 𝑂(𝑛 log 𝑛 ) storage, where k is the number of reported segments. The structure can be built in 𝑂(𝑛 log 𝑛 ) time.

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 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 𝑂( log 2 𝑛 +𝑘) time with a data structure that uses 𝑂(𝑛 log 𝑛 ) storage, where k is the number of reported segments. The structure can be built in 𝑂(𝑛 log 𝑛 ) time.

40
**Summary Query Time: 𝑂( log 2 𝑛 +𝑘) Storage: 𝑂(𝑛 log 𝑛 )**

Preprocessing:𝑂(𝑛 log 𝑛 )

41
Jooho Lee Segment Tree

42
**Problem Given n segments, find intervals which contain qx.**

Let solve it with BST. qx

43
**{ } Elementary Intervals**

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

44
**BST of Elementary Intervals**

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

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(n2) 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 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 v u (-∞:p1) [p1:p1] [p2:p2] (p1:p2) (p2:p3) [p3:p3] (p3:p4) [p4:p4] (p4:∞) (-∞:p1) (p1:p2) (p2:p3) (p3:p4) (p4:∞) [p1:p1] [p2:p2] [p3:p3] [p4:p4] Int(u) Int(v)

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 parentn(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 paths. 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)) O(log n+k) for a query! Int(lc(v)) Int(v)

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`], parent(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 which 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.

Similar presentations

OK

6/14/2015 6:48 AM(2,4) Trees1 9 10 14 2 5 7. 6/14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)

6/14/2015 6:48 AM(2,4) Trees1 9 10 14 2 5 7. 6/14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on save water in hindi Ppt on instrument landing system for sale Ppt on south african culture groups Ppt on power grid failure exercise Ppt on ehv ac dc Ppt on e waste management in india Ppt on international business environment Ppt on bluetooth protocol architecture Free ppt on mobile number portability Ppt on current changes in information technology