CS68: Geometic Agoithms Handout #6 Design and Anaysis Oigina Handout #6 Stanfod Univesity Tuesday, 5 Febuay 99 Oigina Lectue #7: 30 Januay 99 Topics: Seide s Tapezoida Patitioning Agoithm Scibe: Michae Godwasse Seide s Tapezoida Patitioning Agoithm Oveview A Randomized Incementa Agoithm. Idea: incementa agoithms ae usuay simpe. We buid a stuctue by adding objects one at a time. The pobem is that they often ead to poo pefomance. As a somewhat simpified exampe, suppose we buid a seach tee fo a set of keys by inseting the keys one at a time, without ebaancing. If the keys ae inseted in ode, the tee wi be unbaanced and the constuction wi take Θ(n ) time. Randomization can hep with this pobem: if we inset keys in andom ode, we expect to get a baanced seach tee in O(n og n) time. By andomizing the ode in which we add objects, we speed up the agoithm. The andomization in andomized incementa agoithms has two key popeties. Fist, the andomization is intena to the agoithm; we make no assumptions about the input. Instead, the agoithm does its own coin fips. Second, the andomization is of the Las Vegas type: the agoithm is guaanteed to give a coect answe (whateve that means in the context of the pobem), but the unning time is not guaanteed in any paticua instance. Instead, we tak about the expected unning time, based on the agoithm s intena andomization. Content of Lectue. Basic agoithm and data stuctues. Anaysis 3. Randomized Anaysis O(n og n) 4. Tacing and anaysis of it 5. Extensions
CS68: 6 Data stuctues Figue : The mappings in T(S). Let S be a set of ine segments (with the usua non-degeneacy conditions, such as no two endpoints diecty above and beow one anothe, and no vetica ine segments). Let T(S) be a tapezoida patitioning (aso caed a tapezoidation ). It shoud et us map any tapezoid τ to its bounding segments; and its hoizontay neighboing tapezoids (thee ae at most fou). These mappings ae iustated in fig.. This epesentation ets us tace a poygona cuve C though T(S) in time O( C + k), whee k is the numbe of tapezoids C intesects, povided C S =. Let Q(S) be a point ocation stuctue fo T(S). That is, given a quey point q = (q x, q y ), it aows us to detemine what tapezoid that point is in. Q(S) is a diected acycic gaph with one souce, and with one sink pe tapezoid of T(S). The intena nodes ae of two types:. X-nodes. (See fig..) When we get to these nodes, we ask, is point q to the eft o ight of a paticua X-vaue? We then banch accodingy.. Y-nodes. (See fig. 3.) Hee we ask, is point q above o beow a paticua ine? So to answe a quey (i.e. identify the tapezoid containing point q), we foow a diected path in Q(S), banching to one of the two descendants at each intena node,
CS68: 6 3 X-vaue Figue : An X-node. ine equation Figue 3: A Y-node. and stopping at a sink (i.e. a tapezoid). (See fig. 4.) Now, et e S be a segment ab (a x < b x ) such that e cosses no segment of S. Define S = S {s}. Question: How do we poduce T(S ) fom T(S), and Q(S ) fom Q(S)? See fig. 5. (Note: in this figue, we have S = {e } and the new segment is e.) The basic steps ae: inset eft endpoint, inset ight endpoint, and inset the segment between them. The fist thing to ask is, does the new segment shae an endpoint with any of the existing segments? If not, we add new vetica theads coesponding to the new endpoints. This spits tapezoid 3 into new tapezoids 5 and 6, and spits 4 into 7 and 8 (see fig. 6). But, of couse, e itsef futhe spits 6 into 9 and 0. It aso spits 7, but 7 s bottom haf is then meged with 0 (see fig. 7). We pefom simia opeations on Q(S), as shown in the figues. (Indeed, since we need Q(S) to hep us ocate tapezoids in T(S), we shoud do these opeations in paae.) Anaysis of unning time The unning time has two pats:
4 CS68: 6 T(S) Q(S) e 4 e 4 3 3 (x-coodinate vaues) Figue 4: An exampe of a tapezoida patitioning T(S), and the coesponding point ocation stuctue Q(S). e 4 e 3 Figue 5: Adding a new segment. quey time, i.e. point ocation in Q(S); and tacing time though the tapezoids in T(S), incuding the time to pefom spitting and meging opeations. Now, the tacing time is O() pus a tem popotiona to the numbe of theads in the new patitioning that abut upon e. Note that this does not incude the theads unning though the endpoints; fo instance, the segment in fig. 8 has ony one thead abutting on it.
CS68: 6 5 e 7 8 e 5 6 e 7 8 5 6 Figue 6: Intemediate stage in adding a segment e 9 8 e 5 0 e e 8 5 e 9 0 Figue 7: Adding a segment. T(S) and Q(S) have now been fuy updated. In the wost case, this coud make fo a ot of tacing time. Fo instance, in the poygon shown in fig., if we inset the segments in countecockwise poygon ode stating at the uppe ight, the tace time is Θ( S ). The point ocation cost may aso be excessive. If the segments shown on the eft side of fig. 9 ae inseted bottom to top, the quey data stuctue is vey unbaanced, as shown on the ight side of fig. 9, and the tota quey time is Θ( S ). But what if we ook at the agoithm s aveage o expected pefomance, athe than its wost-case pefomance?
6 CS68: 6 Figue 8: A segment with just one thead abutting on it. Figue 9: (Left) A set of segments in T which, if inseted bottom to top, eads to a wost-case quey time. (Right) The esuting quey stuctue. Randomized anaysis of unning time Let e, e,..., e n be a andom odeing of the segments of S, and et S i = {e, e,..., e i } (a pefix of S), fo 0 i n. Lemma. The expected cost of tacing e i though T(S i ) is 4. Poof: Define the degee of a segment in a patitioning, deg(e, T(S)), as the numbe of vetica theads that abut upon e (e S). Then deg(e, T(S i )) 4i () e S i This is because each thead (thee ae i of them) hits segments. andom e i, the expected degee E(deg(e i, T(S i ))) is at most 4. Now, eca that H n = n i= i = Θ(og n). So fo a
CS68: 6 7 e i q Figue 0: Hee, the addition of e i ceates two new decision eves (instead of one) in the seach path fo q. Lemma. Fo any quey point q, the expected numbe of key compaisons (ove a odeings of S) to ocate q in Q(S n ) is 5H n = O(og n). In othe wods, the numbe of decision nodes in Q(S) that we coss to get to q is O(og n). Poof: We use backwad anaysis. We wi fist find how many exta decision nodes we have to coss, on aveage, as a esut of adding a singe edge. Then we add up a those incementa costs to get the tota cost (effectivey, buiding up Q(S) fom scatch). Suppose, then, that q ies in tapezoid τ i, whee τ i T(S i ), and q aso ies in τ i T(S i ). If we know τ i (and its coesponding sink in Q(S i )), what is the expected cost E i of finding τ i? We, unde what cicumstances wi we have to go though an exta decision eve because of the new edge e i? That is, when is τ i = τ i? This occus if τ i s uppe bounding edge is e i (an event with pobabiity i ); if τ i s owe bounding edge is e i (an event with pobabiity i ); if τ i s eft bounding thead was geneated by e i (an event with pobabiity i ); o if τ i s ight bounding thead was geneated by e i (an event with pobabiity i ). One moe decision eve is possibe. Fo the situation shown in fig. 0, the addition of e i caused two new compaisons to be made, not one. This situation aso has pobabiity i Ṫheefoe, the expected incementa cost E i is 5 i, and the expected tota cost of ocating q is i= n E i = 5H n.
8 CS68: 6 Figue : A poygon whose bounday we woud not want to tace. With these emmas, we can now state the foowing theoem, which appies to abitay sets of disjoint ine segments. Theoem 3. Let S be a set of n segments, disjoint except fo thei endpoints. Then. We can buid T(S) and Q(S) in expected O(n og n) time.. Expected Q(S) = O(n). the expected numbe of tapezoids spit is O(n) 3. Expected quey time fo any point q in Q(S) is O(og n). The second pat of this theoem foows fom Lemma. The thid pat is a estatement of Lemma. The fist pat is a consequence of Lemmas and. Whee does a that time go? It goes to point ocation. How, then, can we avoid point ocation, in the specia case in which the segments fom a poygon? We, we coud stat fom the endpoints of eaie segments, i.e. inset the segments in poygon ode. But then we woud ose the benefits of andomization. Fo exampe, with the poygon in fig., the cost of tacing aound it goes up as Ω(n ). So et us use a compomise soution. We wi inset the segments in andom ode, but peiodicay tace the poygona bounday to speed up point ocation fo ate insetions. (Detais beow.) Lemma 4. Fo any q, if we know that q τ j (whee τ j T(S j )), then the expected cost of ocating q in T(S k ), k j, is 5(H k H j ) = O(og k j ). Poof: The poof is amost exacty the same as that of Lemma, above. Lemma 5. Let R be a andom subset of S, with size R =. Let Z be the numbe of intesections between T(R) and S\R. [Reca that S\R denotes the segments of S that ae not in R.] Then the expected vaue of Z is 4(n ).
CS68: 6 9 Poof: Fo any e (S\R), the numbe of intesections between e and T(R) is deg(e, T(R {e})). So E(Z) = ( n ) R S R = fom (), = ( n ) R S R =+ ( n ) 4 R R S R =+ = 4( + ) ( n + ) ( n ) = 4(n ) deg(e, T(R {e})) e (S\R) e R deg(e, T(R )) i Now, define og (i) {}}{ n = og og og n, and og n = max(h og (h) ). n Aso et N(h) =. og (h) n With these definitions, we now pesent the fina vesion of Seide s agoithm. Seide s Tapezoida Patitioning Agoithm. Geneate a andom ode s, s,..., s n of the segments of P.. Geneate T(S ) and Q(S ) [initiaization]. 3. fo h = to og n do a. fo i = N(h ) + to N(h) do Inset segment e i, poducing T(S i ) and Q(S i ) fom T(S i ) and Q(S i ). b. Tace P though T(S N(h) ) to ocate the endpoints of a e j, fo j > N(h). 4. fo i = N(og n) + to n do Inset e i, poducing T(S i ) and Q(S i ) fom T(S i ) and Q(S i ). Anaysis Let us examine the expected unning times of each step:. O(n). O() 3a. ( N(h) N(h) O() fom Lemma + O(og( N(h ) ) )) fom Lemma
0 CS68: 6 Now, N(h) og( N(h ) ) og( n N(h ) ) og(og (h ) n) = og (h) n. Theefoe, step (3a) takes N(h) O(og (h) n) = O(n) time. 3b. O(n), by Lemma 5. n 4. O(n) og( N(og n) ) = og n) og(og n og = O() }{{} (as in 3a) The tota unning time is O(n) pe phase. Thee ae O(og n) phases, so the fina unning time of the agoithm is O(n og n). This competes the poof of the foowing impoved vesion of Theoem 3. Theoem 6. Let S be a set of n segments that fom a simpe poygon. Then. We can buid T(S) and Q(S) in expected O(n og n) time.. Expected Q(S) = O(n). 3. Expected quey time fo any point q in Q(S) is O(og n). Impementation The agoithm was un on a compute as a demonstation at the end of cass, which seved to emphasize the basic steps invoved in this agoithm: () add a segment at andom; () ocate its endpoints; (3) inset tapezoids. Evey now and then, though, the agoithm woud seach aound the bounday of the poygon. As it did so, it ocated the vetices that had not yet been paced, in tems of the existing tapezoids. Then ate, when it needed to ocate those endpoints, it was abe to use that infomation to stat its seaches of Q(S) fom futhe down the DAG (theeby educing its seach time). This is the compomise discussed eaie on page 8. Extensions Seide s agoithm aso appies to moe genea objects than simpe poygons. Theoem 6 appies to any coection of ine segments, disjoint except fo thei endpoints, that fom a staight-ine embedding of a connected pana gaph we epace the poygon tacing step by a gaph tavesa, but a othe steps emain unchanged. Combining this obsevation with Theoem 3, we get the foowing theoem.
CS68: 6 Theoem 7. Let S be a set of n segments that fom a staight-ine embedding of a pana gaph with k connected components. Then. We can buid T(S) and Q(S) in expected O(k og n + n og n) time.. Expected Q(S) = O(n). 3. Expected quey time fo any point q in Q(S) is O(og n).