Segment Tree and Its VLSI Layout Applications Shmuel Wimer Bar Ilan Univ., School of Engineering Jan 2011 Segment Tree 1 Segment Tree Definition • Introduced by J. L. Bentley in 1977 • Data structure designed to handle intervals on the real line • Intervals end points belong to a fixed set of abscissas • Abscissas can be normalized to range [1,N] without loss of generality by using a lookup table • Given an interval [l,r], the segment tree T(l,r) is a rooted binary tree defined recursively Jan 2011 Segment Tree 2 Every node v is characterized by two parameters B v : beginning of node's world (left end) E v : end of node's world (right end) If r l 1 a midpoint B v E v 2 is defined and two sub-trees dividing v's world into two halves are rooted at v : LSON v is the root of a left sub-tree T l , B v E v 2 RSON v is the root of a right sub-tree T B v E v 2 , r Jan 2011 Segment Tree 3 v T l, r RSON v LSON v T l , B v E v 2 T B v E v 2 , r The intervals B v , E v are called standard intervals of T vT The unit intervals stored at leaves (r l 1) are called elementry intervals of T Jan 2011 Segment Tree 4 4,15 4,9 9,15 4,6 4,5 6,9 5,6 9,12 6,7 7,9 7,8 Jan 2011 9,10 8,9 Segment Tree 12,15 10,12 10,11 11,12 12,13 13,15 13,14 14,15 5 Insertion and Deletion Segment tree is a ststic data structure w.r.t the set of abscissae. It is designed to dynamically store intervals whose ends belong to a set l , l 1,, r 1, r , namely, supporting insertions and deletions. For r l 3, an interval l , r is partitined into log 2 r l + log 2 r l 2 standard intervals of T l , r at most, hence for a set of n interval with 2n distinct ends, the total number of standard intervals is bounded by O n log 2 n . Jan 2011 Segment Tree 6 void INSERT / DELETE (int b, int e, node v) { if ((b B v ) and (E v e)) { allocate / de-allocate b, e to v; } else { if (b B v E v 2 ) { INSERT / DELETE (b, e, LSON v ) } if ( B v E v 2 e) { INSERT / DELETE (b, e, RSON v ) } } UPDATE (v) ; // application specific, computed at recurrence exit } Jan 2011 Segment Tree 7 PIN 1,257 74,107 1,129 65,129 65,97 65,81 97,129 81,97 PL 97,115 97,105 73,81 105,115 77,81 73,77 105,109 73,75 PR 75,77 105,107 74,75 Jan 2011 Segment Tree 8 The action INSERT (b, e, ROOT (T )) / DELETE (b, e, ROOT (T )) imposes a tour in T , having the following structure: An initial path PIN (possible empty) starting at the root and ending at a node v* called fork. From v* two (possibly empty) path PL and PR issue. Either b, e is allocated entirely to v* (then PL and PR are empty), or all right sons of PL , which are not on PL , and all left sons of PR , which are not on PR , are allocated (de-allocated), thus defining b, e fragmantation into standard intervals. Jan 2011 Segment Tree 9 Allocation and De-Allocation • Depends on application. • If we wish to know the cardinality of cover of [B[v],E[v]] then a counter C[v] is associated with node v: – C[v] = C[V]+1 is allocation for INSERT – C[v] = C[V]-1 is de-allocation for DELETE • In many cases of VLSI aplication C[v] indicates the presence of material, so we’ll be interested in whether C[v] > 0 (material exists) or C[v]==0 (no material). Jan 2011 Segment Tree 10 VLSI Layout Applications • Useful for calculating area and perimeter of polygon ensemble for lateral and fringe capacitance extraction. • Mask Boolean operations like UNION, INTERSECTION, DIFFERENCE, and more, where contour is required. – Construction (contour) problems are typically more difficult than reporting (area, perimeter) problems. • More applications: – Design rule checking. – Cross coupling capacitance • Very robust and simple for implementation. There are other, more efficient and more complex data structures. Jan 2011 Segment Tree 11 This is what mask designer is drawing This data is used by lithography for mask generation, obtained by UNION operation We may be interested in calculating the underling area for extracting lateral capacitance and the perimeter for the extraction of fringe capacitance Jan 2011 Segment Tree 12 This is what mask designer is drawing This is the diffusion mask for manufacturing obtained by Boolean DIFFERENCE Jan 2011 Segment Tree 13 The electric field between the metal wires implies line-to-line capacitance, which is the reason of noise and power dissipation This is what mask designer is drawing L D We’d like to calculate the common parallel run L and the distance D so the line-to-line capacitance is L/D Jan 2011 Segment Tree 14 1D Measure of Union of Intervals Given n intervals b1 , e1 , , bn , en in the real line, how efficiently their measure of UNION can be found? We sort the abscissae b1 , e1 , b2 , e2 , , bn , en in ascending order p1 , p2 , , p2 n and associating to every point whether it was an opening (left) or closing (right) end point of an interval. In case of tie, openning end point preceeds closing one. Jan 2011 Segment Tree 15 Let m be the measure of UNION and C the multiplicity of overlapping intervals. void 1D_MEASURE_OF_UNION( intervals obtain p1 , p2 , bi , ei n i 1 ){ , p2 n in ascending order ; m 0 ; C 1 ; // initialize measure and multiplicity (cardinality) of cover for ( i 2 ; i 2n ; i ) { if ( C 0 ) { m m pi pi 1 } if ( pi is left (opening) end point ) { C C 1 } else { C C 1 } // pi is closing end point } } Jan 2011 Run time is O n log2 n. Segment Tree 16 2D Measure (Area) of Union of Rectangles Problem: How to calculate the area of UNION without double counting overlapping areas? Consider an imaginary vertical line is positioned at abscissa x, intersecting the rectangles with measure m x . The area of UNION of rectangles is then given by - m x dx. We'll simulate the imaginary vertical line, called scan - line or sweep - line in a discrete mannar. Jan 2011 Segment Tree 17 Given rectangles Ri i 1 , let x1 , x2 , n , x2 n be their abscissae sorted in ascending order (left-to-right). Associate with an abscissa whether it belongs to an openning (left) or closing (right) edge. In case of tie in x, openning edge preceeds closing one. scan-line The projection (measure) of the UNION of rectangles on the scan-line can cahnge only at abscissae of edges and remains constant at the semiopen interval xi , xi 1 . The area increment is obtained by m xi xi 1 xi . Jan 2011 xi xi 1 Segment Tree 18 Efficient Calculation of m(xi) Calculating m xi efficiently is the key of the 2D algorithm and is obtained in O log 2 n time by segment tree implementation of the scan-line. Since the ordinates of the vertical edges (position of horizontal edges) are known apriori and their count is 2n at most, an appropriate segment tree T 1, 2n can be constructed. An opening (left) edge triggers an insertion into T 1, 2n , while closing (right) edge triggers deletion. Every insertion or deletion to v T 1, 2n updates C v (number of rectangles covering the node). Area is contributed iff C v 0. A parameter m v is associated to a node v, indicating how much of the segment B v , E v is covered by the rectangles at xi . Clearly, m root T m xi . Jan 2011 Segment Tree 19 void INSERT / DELETE (int b, int e, node v) { if ((b B v ) and (E v e)) { C v / C v } else { if (b B v E v 2 ) { INSERT / DELETE (b, e, LSON v ) } if ( B v E v 2 e) { INSERT / DELETE (b, e, RSON v ) } } if ( C v 0 ) { m v =E v B v } // node is fully covered else { // node is either partially covered or empty if ( v is not a leaf ) { m v =m LSON v m RSON v } else { m v =0 } } } Given a vertical edge at abscissa xi and ordinates bi , ei , the measure m root T m xi is obtained in O log 2 n time. Jan 2011 Segment Tree 20 void 2D_MEASURE_OF_UNION ( rectangles Ri i 1 ) { n sort vertical edges of retngles in ascending order x1 , x2 , , x2 n ; Initialize root of tree ; construct segment tree T spanning min bi , max ei ; 1i 2 n 1i 2 n m 0 ; // measure initalization for ( i 1 ; i 2n ; i ) { if ( i 1 ) { m m root T xi xi 1 } if ( xi is opening / closing ) { INSERT / DELETE (bi , ei , root T ) ; } } Overall run-time is O n log2 n. Overall storage is O n . Jan 2011 Segment Tree 21 Perimeter of Union of Rectangles m xi - m xi -1 m xi-1 m xi Perimeter is the length sum of: vertical edges horizontal edges Observation: m xi - m xi -1 is the vertical contribution to perimeter resulting from insertion or deletion of an edge. Jan 2011 Segment Tree 22 i 2 i 6 xi 1 xi xi 1 xi Horizontal contribution is added along scan-line progression. If i = 2 number of scan-line intersections in xi 1 , xi . Contribution to perimeter is i xi xi 1 . Jan 2011 Segment Tree 23 How to compute i efficiently along scan-line progression? Let F n i 1 Ri . v 2 number of the scan-line S x F intersections with B v , E v . 1 B v S x F LBD v otherwise 0 B v , E v 1 E v S x F RBD v otherwise 0 B v , E v LBD v and RBD v avoid double counting of horizontal edges in case that a continous segment is split between two sons. Jan 2011 Segment Tree 24 B v v E v B v E v 2 RBD 1 LBD 1 v , LBD v and RBD v are maintained at every INSERT and DELETE. Jan 2011 Segment Tree 25 void UPDATE ( node v ) { if ( C v 0 ) { v 2 ; LBD v RBD v 1 ; } else { v LSON v RSON v 2 RBD LSON v LBD RSON v ; RBD v RBD RSON v ; LBD v LBD LSON v ; } } The term 2 RBD LSON v LBD RSON v compensates for the case of artificail split between the two sub-trees of sons.There exists i ROOT T . Jan 2011 Segment Tree 26 void PERIMETER_OF_UNION ( rectangles Ri i 1 ) { n sort vertical edges of retngles in ascending order x1 , x2 , , x2 n ; construct segment tree T spanning min bi , max ei ; 1i 2 n 1i 2 n mo 0 ; p 0 ; // measure and perimeter initalization for ( i 1 ; i 2n ; i ) { * root T ; // take at the beginning of xi 1 , xi if ( xi is openning / closing ) { INSERT / DELETE (bi , ei , root T ) } m* m root T ; p m* mo ; // vertical contribution mo m* ; // store old measure if ( i 1 ) { p * xi xi 1 } // horizontal contribution } Jan 2011 Segment Tree 27 Overall time is O n log2 n . Overall storage is O n . Questions : What happens if edges have same abscissa and closing edges are preceding opening ones? Although material is continuous when two rectangles are abutting, the edge of abutment will be accounted twice in perimeter! Edges are sorted such that for same abscissa opening edges are preceding closing ones. Jan 2011 Segment Tree 28 The Contour of Union of Rectangles Given a collection of rectangles Ri i 1 , find the contour of F n n i 1 Ri . Rectangles are counterclockwise oriented. Hence contour is also oriented. Resulting countour orientation must be consistent. External countours are counterclockwise oriented. Internal contours (holes) clockwise oriented. Jan 2011 Segment Tree 29 S c S c F E A left (opening) edge E is located at abscissa c. What portion it contributes to the contour? E Jan 2011 Segment Tree S c F , >0 30 S c S c E F A right (closing) edge E is located at abscissa c. What portion it contributes to the contour? E Jan 2011 Segment Tree S c F , >0 31 Two phase algorithm (Lipski - Preparata 1980): Find the set V of vertical edges of contour of UNION Link the vertical edges in V by horizontal edges to form properly oriented cycles. Derivtion of V is done by a segment tree implementation of scan-line. Let S c denote vertical scan-line at abscissa c. The intersection S c F consists of a set of disjoint vertical intervals. Let x1 , x2 , , x2 n be the sorted abscissae of Ri i 1 . S x F doesn't change n at the open interval xi x xi 1. Jan 2011 Segment Tree 32 The meaning of c with regard to scan-line is that the contribution of a left (opening) edge is computed before edge INSERT takes place. Similarily, c means that the contribution of a right (closing) edge is computed after edge DELETE takes place. In addition to standard segment-tree parameters of a node v, we define 1 C u 0 u T v a potential indicator P v 0 otherwise Initiaization: C v =0 and P v =1 at construction of the segment-tree. S x F B v , E v C v 0 , so its complement is the gaps between these intervals, which are found with the aid of C v and P v . Jan 2011 Segment Tree 33 The contribution S x F E of an edge E : b, e is given by: C v 0 CONTR v B v , E v P v 1 P v 0 CONTR LSON v CONTR RSON v Edge E : b, e is INSERT (DELETE) Decomposed into standard intervals Scan-line intersection with F n i 1 Ri E : b, e intersection with complement E : b, e Problem: contiguous segments are split S x F Jan 2011 E S x F Segment Tree 34 The formation of contiguous segments of E S x F is obtained by using a STACK data structure. Traversal of E decomposition into standard intervals is done left-to-right, hence abutment across standard intervals can be detected on-the-fly. Set STACK at any new abscissa of sorted x1 , x2 , , x2 n . if ( B v TOP STACK ) { TOP STACK E v } // contiguous else { B v STACK ; E v STACK ; } // start new segment Edges are sorted such that for same abscissa openning edges are preceding closing ones. Opening (closing) edges having same abscissa are sorted in ascending order of their lower ordinate to ensure contiguity and uniqueness. Jan 2011 Segment Tree 35 void CONTR (int b, int e, node v) { if (C v ==0) { // C v >0 implies B v , E v S x F if ((b B v ) and (E v e) and (P v 1)) { // contribution to contour if ( B v TOP STACK ) { TOP STACK E v } // contiguous else { B v STACK ; E v STACK ; } // start new segment } else { if (b B v E v 2 ) { CONTR (b, e, LSON v ) } if ( B v E v 2 e) { CONTR (b, e, RSON v ) } } } } Jan 2011 Segment Tree 36 void INSERT / DELETE (int b, int e, node v) { if ((b B v ) and (E v e)) { C v / C v } else { if (b B v E v 2 ) { INSERT / DELETE (b, e, LSON v ) } if ( B v E v 2 e) { INSERT / DELETE (b, e, RSON v ) } } if ( C v >0 ) { P v =0 } // node cannot contribute to contour else if ( v is a leaf ) { P v =1 } // node can contribute to contour // node inherits contribution potential from sons else if ((P LSON v ==1) and (P LSON v ==1)) { P v =1 } else { P v =0 } // contribution may come from lower nodes } Jan 2011 Segment Tree 37 void VERTICAL_EDGES_OF_CONTOUR ( rectangles Ri i 1 ) { n sort vertical edges of rectangles in ascending order x1 , x2 , , x2 n ; construct segment tree T spanning min bi , max ei ; 1i 2 n 1i 2 n V ; STACK ; // initalization for ( i 1 ; i 2n ; i ) { // contribution preceds edge insertion if ( xi is opening ) { CONTR (bi , ei , ROOT T ) ; INSERT (bi , ei , ROOT T ) ; } else { // contribution proceeds edge deletion DELETE (bi , ei , ROOT T ) ; CONTR (bi , ei , ROOT T ) ; } if (( xi 1 > xi ) or (opening / closing status change)) { V V STACK ; STACK ; } } } Jan 2011 Segment Tree 38 Completing the Cycles of Contour Denote by x ; b, t vertical edge b, t at abscissa x and by y ; l , r horizontal edge l , r at ordinate y. Horizontal edge of contour is supported by two vertical edges. x1 ; y1, y2 x1 , y1 x1 , y2 ; y1 y1 ; x1, x2 ; y2 x2 , y1 x2 , y0 ; y0 x2 ; y0 , y1 ; y1 Associate to every vertical edge e j V , e j x j ; b j , t j , two triplets x ,b ; t j j Jan 2011 j and x ,t ; b j j j pointing to each other. Segment Tree 39 Let V q. We sort x , y ; z j j j , 1 j 2q, lexicoraphically in ascending order by y j as primary key and x j as secondary key. (Why all triplets must be distinct?) Let x , y ; z 1 1 1 , , x2 q , y2 q ; z2 q be sorted. It is possible by traversal over it and by usage of triplets mutual pointers to construct all contour cycles in consistent directions (external contours and holes). Traversal takes time linear in triplet list, so O q log q sorting time defines run-time complexity. The resulting countor may consist of several cycles. Counterclockwise oriented contours are the external boundary of polygons, while clockwise oriented cycles are holes of polygons. Jan 2011 Segment Tree 40 e3 e6 e8 e5 e7 e1 e10 e2 e9 e4 Edges ei , 1 i 10, were obtained in this order by first scan-line part. x2 , b2 ;t2 x4 , b4 ;t4 x4 , t4 ; b4 x9 , b9 ;t9 x9 , t9 ;b9 x10 , b10 ;t10 x5 , b5 ;t5 x7 , b7 ;t7 x1 , b1 ;t1 x3 , b3 ;t3 x6 , b6 ;t6 x8 , b8 ;t8 x8 , t8 ;b8 Jan 2011 x7 , t7 ;b7 x1 , t1 ;b1 x10 , t10 ;b10 x3 , t3 ;b3 x6 , t6 ;b6 x2 , t2 ;b2 x5 , t5 ;b5 Segment Tree 41 There’s no ambiguity in deciding whether to go to left or right triplet when an horizontal edge is decided. It follows that a pair of successive triplets defines horizontal edges. Consequently, once two successive triplets are traversed and define a new horizontal edge, the number of triplets on both the left and the right parts of the list must be even. Therefore, if the index of a triplet is even, its left adjacent triplet is paired, otherwise, the right triplet is paired. Jan 2011 Segment Tree 42 Run-time Complexity Lemma : Let T be a balanced binary tree with n leaves and L a subset of its nodes. Denote by P u the set of nodes on the paths from u L to the root. Total number of nodes along the paths satisfies: P u L log 2 uL 16n . L Proof : We can assume that L is a subset of T 's leaves, as this is the worst case, yielding the longest and least overlapping paths to root. Let T L be the tree whose leaves are L and T0 L T L containing v T L whose parent or v itself has at least two descendants in L. Jan 2011 Segment Tree 43 The number of leaves in T0 L is L. This follows since a leaf is either in both T0 L and T L , or if a leaf u is in T L \T0 L then a path to root P u must enter T0 L . Also, two paths P u and P u , u L and u L cannot merge at a laef of T0 L . T L consists of a "top" part T0 L whose L leaves are 1:1 connect with disjoint paths (a path is possible empty) to the leaves of T L . Jan 2011 Segment Tree 44 Given fixed L, The largest T L is obtained when T0 L is balanced and topmost in T L . This follows since two paths in T L connecting leaves to root are merged as close as possible to root, resulting in maximal count of distinct nodes of the two paths. U’ U” U’ |P(U’)+ P(U”)|=7 U” |P(U’)+ P(U”)|=5 T L 2 L 1 L log 2 n log 2 L L 2 log 2 n 1 log 2 L 1 L log 2 Jan 2011 Segment Tree 16n . Q.E.D L 45 Consider the contribusion to contour ocurring by an edge Ei , 1 i 2n, in CONTR. Let ni be the number of distinct pieces of Ei S x F . There are 3 possibilities for a node v in CONTR. If C v >0 traversal is aborted. P v =1, whole node's interval B v , E v is contributed and traversal is aborted. Most time consuming part occurs at partially covered nodes, C v =0 and P v =0. Contribution is made by recursive call of CONTR and decomposition into contiguous standard intervals obtained along the left and right paths issued at v. Jan 2011 Segment Tree 46 Each of the ni disjoint pieces of Ei S x F results in at most two search paths terminating at most in two leaves. Hence the total constribution of ni such searches is 2ni leaves at most, with a constant amount of work at each node. Let p i 1 ni be the total amount of contour vertical pieces. 2n By Lemma the total node visits (paths overlap included) is 16n bounded by C i 1 2ni log 2 . 2ni 2n Jan 2011 Segment Tree 47 Since p i 1 ni , 2n i1 2ni log 2 2n 16n is maximized for ni = p 2n . 2ni Hence, the number of visited nodes is bounded by 64n 2 16n 2 2n p 16n . Cp log 2 C i 1 log 2 C i 1 2ni log 2 p p n 2ni 2n INSERT and DELETE are standard and consume O n log 2 n , and same for intial sort of input vertical edges. Building initial segment tree is O n . Theorem : The p edge contour of UNION of n isothetic rectangles can be found in O n log 2 n p log 2 n 2 p . Jan 2011 Segment Tree 48 Theorem : The run-time complexity of finding a contour of F n i 1 Ri without holes is bounded by n log 2 n . Proof : We'll show that the problem sorting N numbers x1 , x2 , , xn can be transformed in time O n into finding the contour of UNION of rectangles. Let x1 , x2 , , xn be arbitrary real positive numbers. We define Ri : x, y 0, xi 0, M xi , 1 i n, M max xi 1. 1i n Jan 2011 Segment Tree 49 Finding the contour solves the sorting problem, known to be bounded by n log 2 n . Q.E.D Jan 2011 Segment Tree 50