Trapezoidal Maps Shmuel Wimer Bar Ilan Univ., School of Engineering March 2011 1 Trapezoidal Map Planar subdivision Abscissas are all distinct n segments 6n+4 vertices at most 3n+1 trapezoids at most March 2011 2 to p leftP o in t leftP o in t to p bottom bottom to p to p leftP o in t leftP o in t bottom bottom Trapezoidal map can be constructed in O(nlogn) time by a scan-line algorithm. March 2011 3 Randomized Incremental Algorithm T S - trapezoidal m ap of a set S s i i 1 of line n segm ents w ith distinct abscissae of end points. T he algorithm builds a by-product data s tructure D S supporting point location queries in T S . A n advantage over construction by scan-line w hich doesn't yield query supporting data stru cture. March 2011 4 T S D S p1 B A s1 x-node q1 q1 p1 E D p2 A q2 s1 G s2 B p2 s2 C F q2 G E y-node C S2 Inner nodes have degree 2 trapezoid D F T rapezoids of T S and leaves of D S are interlinked by pointers March 2011 5 Querying a point location Does q lie to the left or to the right ? p1 B A s1 q1 q1 p1 E D p2 A q2 s1 G s2 B p2 s2 C F q2 G E C S2 Does q lie above or below? March 2011 D F 6 Randomized Construction Algorithm S eg m en ts are ad d ed to T S ran d o m ly, o n e at a tim e. T h e stru ctu re o f D S is affected b y selectio n o rd er. S o m e o rd ers m ay yield D S w ith b ad q u ery tim e o f O n , o th ers m ay yield g o o d q u ery tim e o f O l o g n . Som e D S m y be of large size O n 2 , others m ay b e of size O n . E xpected query tim e is O log n and expected size is O n ! March 2011 7 T rap ezoid alM ap ( S ) { // S - non crossing segm ents // R eturns T S an d D S bound S by a rectang le R ; initialeze T 0 and D 0 ; for ( s i S , 1 i n , random ly selected ) { find 0 , rem ove ,k T 0 , i 1 , ,k T i 1 j si ; and insert new i trapezoids in T appearing by s i insertion ; rem ove corresponding leaves from D i i 1 , create i leaves in D for new trapezoids and interlink to T ; } } March 2011 8 T w o invariants of T rap ezoid alM ap : 1) T is a trapezoidal m ap of S i s1 , , si , i i i 2) D is a valid search structure for T . Initialization: T T o m odify T derive 0 , i 1 0 T R, D 0 is a corresponding leaf. w e query p i by searching in D i 1 an d , k successively from left to righ t. j If righ tP oin t is above s i , then j 1 is its low er-right neighbor, otherw ise it is upper right. ( A bscissa uniqueness ensures there are only tw o right-adjacen t trapezoids). March 2011 9 0 1 si 3 2 If p i doesn't exist in S i 1 , searching in D i 1 w ill end at a leaf (trapezoid 0 ). March 2011 10 sj pi q j si 0 If p i exists in S i 1 , it m ust m eet an internal node. If it is x -node laying on a vertical line, proceed ing query w ith p i = p i obtaine s 0 . pi p j si sj 0 If it is y -node, it m ust be a left point p j , s j S i 1 . S lops of s i and s j are com pared and 0 is decided ac cordingly. March 2011 11 F ollow S egm en t ( T i 1 ,D i 1 // R eturns sequence 0 , search p i in T i 1 , si : p i , q i ) { , k inter sected by s i to find 0 ; j0 ; j w hile ( q i righ tP oin t j i f ( righ tP oin t { j 1 else { ) { // progress rightw ard along s i is above s i ) is low er-right neighbor of j 1 j } is upper-right neighbor of j } ++j ; } return sequence 0 , ,k; } March 2011 12 New segment insertion T i 1 pi si qi D i 1 D T D i i 1 pi C B A A D qi si C March 2011 i B D 13 Assuming that a point is contained in Δ, the sub tree replacing its leaf is sufficient to determine whether the point is in A, B, C or D. The information attached to new trapezoids is their left and right neighbor trapezoids, top and bottom segment and points defining their left and right vertical segment. If the information in Δ is properly stored, above info can be determined in a constant time from si and Δ. If pi=leftPoint(Δ) and / or qi=rightPoint(Δ), Δ is divided into two or three trapezoids and sub-tree replacement is simpler. March 2011 14 Intersection with more than one trapezoid 0 1 2 si T 3 i 1 D A qi i E C pi T F B Any of p i and q i that didn't exist partition 0 and k into three trapezoids. B y using the inform ation stored in 0 , , k , t he vertical extensions are shortened to incidence s i , consum ing constant tim e per trapezoid. March 2011 15 0 1 2 T 3 si i 1 D T A i E qi F C pi B D D D 0 1 i i 1 i 1 si 2 3 si B si D qi si F E A March 2011 C 16 If p i 0 and / or q i k are internal points, leaf is replaced w ith an internal x -node pointing to a leaf and a y -node of s i w hich points to tw o leav es. E lse, a leaf is replaced by a y -node pointing to tw o leaves. T he leaves of 1 to k 1 are replaced w ith a y -node of s i pointing to tw o leaves. T he fact that different trapezoids in T i 1 m erge into i a single trapezoid in T results few incom ing arcs to i a leaf in D . March 2011 17 T h eorem : T S and its associated search structure D S for n line segm ents set S , are constructe d in O n log n expected tim e, occupying O n storage and yielding expected query tim e of O log n . Proof : A query traverses a path in D S from root to a leaf (trapezoid). X i - the num ber of new nodes on the query path, w hich have been created at s i insertion. X i , 1 i n are random variables satisfying X i 3. March 2011 18 T h e len g th o f a search p ath s is o b tain ed b y su m m in g th e ran d o m variab les X i o ver all in sertio n s, i 1 X i . n B y lin earity o f ex p ectatio n it fo llo w s th at th e ex p ected len g th o f a search p ath is: n E Xi i 1 i 1 E X i . n Pi - probability that there exists a node on the search path of q , created at s i insertion. T herefo re E X i X i Pi 0 1 Pi 3 Pi . March 2011 19 q S i 1 - trapezoid after insertion of s i 1 contain ing q . T herefore, Pi P r q S i q S i 1 Pr q Si T i 1 4 i. R .H .S follow s from the observation that the probability of q S i to disappear by the rem oval of s i from T is i 4 i at m ost since s i m ay participate in defining any of its four border edges. March 2011 20 P utting everything togather, the expecte d length of a query path is bounded by: n E Xi i 1 i 1 3 Pi i 112 n n H n is th e n -th h arm o n ic n u m b er i 12 1 1 1 2 n i 1 1 i 12 H n 1 , w h ich n satisfies ln n H n ln n 1 (recall th at ln n n 1 d x x ). In co n clu sio n , q u ery tim e is O lo g n . T o obtain the size of D S w e'll bound its n ode count. March 2011 21 D S leaves are in 1:1 correspondence w ith T S trapezoids, w hose num ber is bounded by O n . T o bound the num ber of inner node let k i b e the m um ber of new trapezoids created by s i ins ertion. It follow s by construction of D that the num ber of inner nodes is exactly k i 1. i H ow m any new trapezoids are in T ? N o m ore i than total num ber in T w hich is bounded b y O ( i ). March 2011 22 S u m m in g o ver all in sertio n , a w o rst-case u p p er b o u n d o f D S size is g iven b y: O n i 1 n . O i O n 2 M o re in terestin g is th e ex p ected size, w h ich b y lin earity o f ex p ectatio n is: n O n E ki 1 O n i 1 i 1 E k i n i F or a trapezoid T and a segm ent s S i define 1 if T i w hen s is rem oved from S i , , s 0 otherw ise. March 2011 23 4 segm ents at m ost cause trapezoids disa ppearence. H ence, sS i T i , s 4 | T | O i . i | S i | i and the probability of a segm ent to b e chosen is 1 i . k i is the num ber of trapezoids create d (rem oved) by insertion (rem oval) of s i . T aking the average over all s i S i , E ki March 2011 1 i , s s S i T i O i i O 1 . 24 In conclusion the expected num ber of new trapezoids created at a segm ent insertion is O 1 . T h is yield s O n sto rag e b o u n d . E x p ected ru n tim e co n sists o f O 1 in itializatio n tim e, O lo g i q u erin g to fin d p i at s i in sertio n , an d E k i O 1 fo r in tern al n o d es creatio n . T his yields O n log n creation tim e. ■ March 2011 25 Given a set of segments, nothing is guaranteed on the maximal run time, which can be quadratic. Considering all possible problems of n segments, what is the expected maximal query time? O(logn) March 2011 26