Klee’s Measure Problem Computational Geometry, WS 2007/08 Group Work Prof. Dr. Thomas Ottmann Khaireel A. Mohamed Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg Overview • The measure problem • 1D analysis • 2D analysis • Naïve solution • Implicit area computation by segment tree • Improved solution Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 2 The Measure Problem • Victor Klee, 1977 • Given a set of n horizontal line-segments, compute the length of their union on the real line. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 3 1D Problem (Original) • Sorting and partitioning – Total partitions (space): – Time: • Sweep direction: Left to right – Event Q: – Status-measure T: Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 4 1D Problem (Original) • Time complexities – Time per event: – Total sweep time: – Total runtime: Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 5 The Measure Problem in 2D • Jon Bentley, 1977 • Given a set of n rectangles on the 2D plane, compute the area of their union. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 6 Representing a Rectangle • Assume that all rectangles in the set are non-trivial • Sufficient to store bottom-left and top-right coordinates (xhi, yhi) yhi r ylo (xlo, ylo) xlo Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann xhi 7 2D Problem – Sweepline Approach Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann epi epi+1 8 2D Problem – Sweepline Approach • Sorting and partitioning – Total partitions (space): – Time: • Sweep direction: Left to right – Event Q: epi – Status-structure T: Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 9 2D Sweepline – Naïve Solution epi epi+1 • Let m(i) be the 1D measure of the active vertical segments at epi. • Then the measure for the 2D slab between epi and epi+1: • In general, for k 2D slabs, the measure for the entire set of rectangles: Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 10 Analyses – 2D Naïve • Maintaining the status-structure T, per event point epi – Time (per insertion): – Time (per deletion): – Time to compute m(i): • Total insertions (into T) = Total deletions (from T) = • Overall performance of the naïve algorithm – Time complexity: Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 11 Analyses – Naïve Worst Case • Worst case runtime: • Can this be avoided? Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 12 Improving the Naïve Approach • Main reason for inefficiency: • Overcoming the inefficiency: – Limit the amount of work done at epi by Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 13 Observation for Improvement • Consider the slab between epi and epi+1 when a new rectangle q becomes active. epi epi+1 epi q epi+1 q epi epi+1 q Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann epi epi+1 q 14 Decomposing the Vertical Sweep Idea: • All segments appearing on the vertical sweepline can be composed of a collection of consecutive fragments [vj, vj+1]. • A single offline structure is sufficient to represent the vertical sweeps to collectively compute the measure m(i) at each epi. • Insertions and deletions of segments affect the structure only by changing the augmented values in the internal nodes. • At the start of the event point epi, the measure for the previous segment m(i-1) can be read off the root immediately. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 15 The Segment Tree as Status-Structure • The ordered (vertical) segments are entered as leaves in a balanced binary search tree . • Each vj (except v1 and vm) will occur twice, representing the closed non-overlapping segment [vj, vj+1]. 9 vm 8 9 C 8 8 7 6 6 6 5 5 4 B E A 3 5 D 2 3 3 1 Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann v1 1 0 0 1 2 3 4 5 6 7 8 9 10 16 Keeping Track of Segments • The segment tree represents all O(n) partitions on the vertical axis. • When a rectangle becomes active (or inactive), we mark the internal nodes in accordingly; i.e. we should be able to know – if the subtree under a node x in fully or partially covers a set of intervals – and thus, the measure at node x, and – the number of segments that fully covers the range of x. x’ vk x Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann vk x vj vj vj vj vi vi 17 Implicit Handling • It is NOT necessary to mark all the individual fragments from which a segment q is composed. Why? Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 18 Classifying Nodes based on a Segment q • Given a segment q = [vi, vj], then a node in is said to be – q-full if it covers a segment fully contained in q – q-partial if it is not q-full but has a child that is either q-partial or q-full • Consider the search paths for ‘[vi’ and ‘vj]’; let the node where the two paths diverge be t. – All nodes encountered before t must have been vm v j] t q [vi v1 Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 19 1-Umbrella of the Segment q v j] hi-tip hi-line t q lo-line lo-tip [vi • The 1-umbrella of a segment q is the subtree of rooted at t consisting of – all the q-partial nodes on the hi-line from t to hi-tip, – all the q-partial nodes on the lo-line from t to lo-tip, and – all the q-full nodes which are directly connected to the hi-line or lo-line. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 20 1-Umbrella of the Segment q Lemma: • The 1-umbrella of the segment q can be built in O(log n) steps. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 21 Marking Nodes on the 1-Umbrella • We maintain the following information in for any sets of segments, where augment in the node x – x.val : The measure of the fragments in the subtree of x which are covered by 1-umbrellas through it or below it – x.cnt : A count of the number of times in which x becomes a q-full node of some umbrella. vm t vk vj vj vi Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 22 Inserting a Segment q into • Input: – The root of the segment tree – The range q = [vi, vj] • Output: – The measure of the vertical line-segments at root.val • Idea: – Percolating measure differences between successive levels in • • • • Determine the 1-umbrella for q. Handle measure differences from lo-tip to t. Handle measure differences from hi-tip to t. Combine measure differences and percolate to root. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 23 Example – Activate qA [8,9] [6,9] [5,9] v=0 c=0 [5,6] v=0 c=0 [1,5] v=0 c=0 9 8 [6,8] [1,9] v=0 c=0 9 C 8 8 7 6 6 B A 5 6 E 4 5 qA 3 [3,5] v=0 c=0 5 3 1 [1,3] 3 0 1 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 24 Example – Activate qB [8,9] [6,9] v=0 c=0 [5,9] v=1 c=0 [1,9] v=3 c=0 [1,5] v=2 c=0 [6,8] v=0 c=0 9 9 8 8 8 7 6 6 qB C B A 5 [5,6] v=1 c=1 6 [3,5] v=2 c=1 5 3 1 [1,3] 3 0 E 4 5 1 3 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 25 Example – Activate qC [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=5 c=0 [1,5] v=2 c=0 [6,8] v=2 c=1 9 9 8 8 8 7 6 6 C B A 5 [5,6] v=1 c=2 6 [3,5] v=2 c=1 5 3 1 [1,3] 3 0 E 4 5 1 qC 3 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 26 Special Cases • When t equals lo-tip, then t also equals hi-tip. Why? • The percolated measure difference have to be ‘killed’ if x.parent.cnt ≥ 1. Why? Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 27 Inserting a Segment Lemma: • Inserting a segment and maintaining the information in requires O(log n) time. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 28 Deleting a Segment Lemma: • Deleting a segment and maintaining the information in requires O(log n) time. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 29 Example – Deactivate qA [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=8 c=1 [1,5] v=2 c=0 [6,8] v=2 c=1 9 9 8 8 8 7 6 6 C B A 5 [5,6] v=1 c=2 6 [3,5] v=2 c=1 5 3 1 [1,3] 3 0 E 4 5 1 qA 3 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 30 Example – Deactivate qC [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=8 c=1 [1,5] v=0 c=0 [6,8] v=2 c=1 9 9 8 8 8 7 6 6 C B A 5 [5,6] v=1 c=1 6 [3,5] v=0 c=0 5 3 1 [1,3] 3 0 E 4 5 1 qC 3 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 31 Example – Activate qD, qE [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=3 c=0 [1,5] v=0 c=0 [6,8] v=2 c=1 9 9 8 8 8 7 6 [5,6] v=1 c=1 6 [3,5] v=0 c=0 5 [1,3] v=0 c=0 3 qE C B 6 E A 5 4 5 3 3 1 D 2 1 qD 0 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 32 Example – Deactivate qD [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=5 c=0 [1,5] v=2 c=0 [6,8] v=2 c=2 9 9 8 8 8 7 6 6 [5,6] v=1 c=1 6 [3,5] v=0 c=0 5 [1,3] v=2 c=1 3 C B E A 5 4 5 3 3 1 D 2 1 qD 0 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 33 Example – Deactivate qE, qB [8,9] [6,9] v=2 c=0 [5,9] v=3 c=0 [1,9] v=3 c=0 [1,5] v=0 c=0 [6,8] v=2 c=2 9 9 8 8 8 7 6 qE C B 6 A 5 [5,6] v=1 c=1 6 [3,5] v=0 c=0 5 3 1 [1,3] v=2 c=0 3 0 E 4 5 1 3 D 2 0 1 2 3 4 5 6 7 8 9 10 Sweep direction Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 34 The Improved Solution Theorem: • The measure problem for a set of n rectangles in the plane can be solved on O(n log n) time. Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann 35