100%(7)7 out of 7 people found this document helpful
This preview shows page 10 - 11 out of 11 pages.
SupposeJ1= [x1,y1]is any interval of the left half andJ2= [x2,y2]is any interval of the right half. Couldthis be a larger overlap than anything found byF? We’ll prove that it cannot. SinceJ2is an interval ofthe right half, its left endpoint is at leastx, i.e.,x2≥x.Therefore the intersectionJ1∩J2is containedin[x,∞), soJ1∩J2= [x,y1]∩J2.LetJ= [x0,y0]be the interval found in step 8.Similarly, we’ll haveJ∩J2= [x,y0]∩J2. Now due to the wayJwas selected in step 8, the right endpoint ofJis at least as large asthe right endpoint ofJ1, i.e.,y0≥y1. This means that[x,y1]⊆[x,y0]. It follows that[x,y1]∩J2⊆[x,y0]∩J2,i.e.,J1∩J2⊆J∩J2, i.e., overlap(J1,J2)≤overlap(J,J2). Now given the way the loop in steps 9–10 works,we see thatO≥overlap(J,J2), soO≥overlap(J1,J2). ThereforeJ1,J2cannot have higher overlap thanwhat was returned byF. This means thatFcorrectly finds the largest possible overlap between any pair ofintervals.Alternate proof of the last paragraph: SupposeJ1is an interval of the left half andJ2is an interval of theright half. Then the left endpoint ofJ2is at leastx. Therefore their intersection lies in[x,∞). The leftendpoint ofJ1is at mostx, therefore it does not affect the size of the overlap. In other words, we can replacethe left endpoint ofJ1withxand nothing changes. Now if we hypothetically assume all left endpoints ofCS 170, Fall 2014, Sol 210
the left intervals arex, it is obvious that the best we can do is find the one that has the highest right endpoint(regardless of the choice ofJ2we end up with the highest overlap). Line 9 finds exactly this interval, andthen we check its overlap with all intervals of the right half. Therefore we must have considered at least oneof the pairs from the left half and the right half that have the highest overlap.Comment about pseudocode:Notice that we didn’t bother spelling out how you find the interval whoseright endpoint is maximal (step 8), how to compute the overlap of two intervals (step 10), or other low-levelimplementation details. Try to follow this practice in your own solutions, too: don’t drown the reader in low-level details, write your pseudocode at a high level of abstraction so that it is as easy to read and understandas possible.CS 170, Fall 2014, Sol 211