Understanding Interval Segment Queries and Segment Trees

slide1 n.w
1 / 30
Embed
Share

Explore the concept of interval segments through closed and open segments, stabbing queries, output sensitivity, and segment tree construction. Learn about storage data in a binary tree, elementary intervals, and endpoint changes in segments. Dive into examples of segments, queries, and the significance of elementary intervals and segment tree leaf nodes.

  • Interval
  • Segment Queries
  • Segment Trees
  • Elementary Intervals
  • Storage Data

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Interval S = [3,10] {x | 3 x 10} Closed segment S = (3,10) {x | 3 < x < 10} Opened segment S = [3,3] {3} Point

  2. The Problem There are several segments Si= [ai,bi] Given, static We want to ask Give x Find every segment sisuch that x Si

  3. Stabbing Query Line x Green = reported

  4. Output Sensitive What is the size of the output of stabbing query? O(N)? So, stabbing query is O(N) ?? We says it s O(K) Where K is the number of output

  5. Segment Tree Construction Given int begin[n], int end[n] Operation void create(int n,int *begin, int *end); O(n log n) void query(int x,int *ans,int &n); O(log n + K)

  6. Storage Data A binary Tree Each node contain a set of intervals It use O( n log n ) space

  7. Example of segments s1 s3 s2 s4 s5

  8. Example of queries s1 s3 s2 s4 s5 Answer = {2,5}

  9. Example of queries s1 s3 s2 s4 s5 Answer = {3,4,5}

  10. Answer may change at the endpoints of segment only How it works s1 s3 s2 s4 s5 {1,2,5} {2,5} {3,5} {3,4,5} {3,4} {1} {1,2,5} {3,5} {3,4} {1} {1,2,5} {2,5} {5}

  11. Elementary Intervals (EI) e = array of 2N endpoints of {Si} eg, S1= {a1,b1}, S2= {a2,b2}, S3= {a3,b3} E = [a1,b1,a2,b2,a3,b3] E* = Sorted E eg, E* = [a1,a2,b1,a3,b2,b3] a1 a2 b1 a3 b2 b3 EI = every interval between each element of E* And every point of E* itself (point is also an interval) sorted Eg, EI = (- ,a1), [a1,a1], (a1,a2) , [a2,a2], (a2,b1) , [b1,b1]

  12. Segment Tree Each leaf= elementary interval s3 s2 s4 s1 s5

  13. Each internal node Union of child intervals Segment Tree s3 s2 s4 s1 s5

  14. Internal Node Construction Construct internal node progressively from bottom to top Pairing adjacent child Notice that child does not intersect! Interval of child is a subset of the interval of the root Hence, Each node in the tree corresponds to an interval Let u be the node I(u) is our the interval associated with the node

  15. Construction s3 s2 s4 s1 s5

  16. Canonical List Each node, additionally, store a list of input segment The segment is stored in the canonical list of node u only when I(u) is a subset of the segment Do not store in the children if the parent has it

  17. Canonical List S2, S5 S5 s1 s1 S3 S3,S4 s1 S2, S5 S3 S4 s3 s2 s4 s1 s5

  18. Tree Summary Given segments, we can construct tree Each leaves is the elementary interval Each internal node is the union of the leaves i.e., each node is associated with an interval Each node stores canonical list , a list of segment that cover the interval i.e., the interval is a subset of the segment Do not store in the children

  19. Answering Stabbing Query Start at root Recursively go through child that intersect the query Report everything in canonical list of node in the path O( log n )

  20. Construction of the Canonical List Construct a tree first, without any canonical list Insert every segment into the list Insertion each segment Start at root node Recursively process children that the segment intersect If the interval of each node is a subset of the segment, store the segment into the canonical list

  21. Tree Structure Use Heap style Because the structure of the tree does not change

  22. Query Code void query(node u,float x, int *ans, int &count) { append_canonical(u,ans,n); if u.isLeaf() == false if is_intersect(x, interval( u.left ) ) query(u.left,x,ans, count); else query(u.right,x,ans, count); }

  23. Create void create(int n, int *begin, int *end) { CreateTree(n,begin,end); for (int i = 0;i < n;i++) { insert(root, segment(begin[i],end[i] ); } }

  24. Insert Code void insert(node u,segment s) { if is_subset(interval(u),s) store_canonical(u,s) else { if is_intersect(s, interval( u.left ) ) insert(u.left,s); if is_intersect(s, interval( u.right ) ) insert(u.right,s); } }

  25. RMQ problem Given an array A of integer What is the minimal value in A[i] .. A[k]?

  26. RMQ nave A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 1 3 2 5 6 1 4 7 9 3 min(2,7) = a[5] Preprocess = O(N2) Query = O(1)

  27. RMQ better M[0] = A[0] M[1] = A[5] M[2] = A[6] M[3] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 1 3 2 5 6 1 4 7 9 3 min(2,7) = min(M[1], A[2], A[6],A[7]) Preprocess = O(N) Query = O(N0.5)

  28. RMQ segment Tree [0,9] [0,4] [5,9] [5,7] [8,9] [0,2] [3,4] [0,1] [2] [3] [4] [5,6] [7] [8] [9] [5] [6] [0] [1] Preprocess = O(N log N) Query = O(log N) A segment tree for the interval [0, 9]

More Related Content