Toy Gog ITEE Uiersity of Queeslad
I the secod lecture last week we studied the biary search algorithm that soles the problem of determiig if a particular alue appears i a sorted list of iteger or ot. We proed that the worst-case ruig time of the algorithm uder the RAM model that we hae defied is 2 + 6 log 2, where (the legth of the sorted sequece) is the problem size. Today we will look at some further examples of how the biary search algorithm rus to help us uderstad its behaiour.
Example 1 Suppose we hae the followig iput set, where = 8 ad we are tryig to fid the alue 7. 8 7 2 3 5 7 11 13 17 19
Example 1 We begi by iitialisig left to be 1 ad right to (i this case 8). 8 7 L M R 1 8 2 3 5 7 11 13 17 19
Example 1 Sice the alue of right is strictly greater tha the alue of left, we proceed by computig mid. 8 7 L M R 1 4 8 2 3 5 7 11 13 17 19
Example 1 We the do a compariso of the alue we re lookig for with the alue i the sorted sequeced idexed by mid. We get lucky i this case ad fid that they are actually equal, meaig we ca stop here ad say that the alue does appear i the sorted sequece. 8 7 L M R 1 4 8 2 3 5 7 11 13 17 19
The preious example illustrates that the algorithm may termiate i far less time (if we get lucky) tha 2 + 6 log 2. This worst-case ruig time that we proed gies us a guaratee o how fast our algorithm rus, ad is idepedet of luck. Let s look at aother example.
Example 2 I this example, we keep the same sorted sequece but look for the alue 11 istead. L ad R get iitialised as before, M gets computed ad we perform the compariso to fid that is greater tha the Mth term i the sequece. 8 11 L M R 1 4 8 2 3 5 7 11 13 17 19
Example 2 The meas that what we are lookig for is i the right half of the sorted sequece, hece we set L to be M + 1 (ad coceptually discard the left half of the sequece). We the recompute M ad do aother compariso, this time fidig that is less tha the alue at M. 8 11 L M R 5 6 8 11 13 17 19
Example 2 So ow we adjust R (ad throw away the right half) to be M 1, ad fid that L ad R hae coerged, meaig there s oly a sigle alue for us to check. Ideed we fid that it is the alue we are lookig for, therefore the output i this case is yes. 8 11 L M R 5 5 11
Example 3 For this last example, we will look at a case where the alue we are lookig for does ot appear i the sorted sequece. Here the first compariso tells us that would hae to be i the right half of the sequece (if it was to appear at all). 8 15 L M R 1 4 8 2 3 5 7 11 13 17 19
Example 3 The secod compariso agais says that must i the right half. 8 L M 15 5 6 R 8 11 13 17 19
Example 3 The third compariso tells us that must be to the left of the highlighted alue of 17, but we see that there are t ay alues to the left of 17... 8 L M 15 7 7 R 8 17 19
Example 3 Ad so we arrie at the base case where we o loger hae L R, which tells us that what we are lookig for does ot appear i the sequece. 8 L M R 15 7 6
The Sum of Two Itegers Problem Now let s look at how biary search ca be applied. Suppose we hae the followig problem: Problem Iput: There is a sequece of positie itegers i strictly icreasig order i memory at the cells umbered from 1 up to. The alue has bee placed i Register 1, ad the a positie iteger has bee placed i Register 2. Goal: Determie whether if there exist two itegers x ad y (ot ecessarily distict) i the sorted sequece such that x + y =.
Example A yes -iput with = 12 12 30 2 3 5 7 11 13 17 19 23 29 31 37 A o -iput with = 12 12 29 2 3 5 7 11 13 17 19 23 29 31 37
A First Attempt A aïe algorithm to sole this problem is to eumerate all possible pairs i the sorted sequece ad check if they sum to. Gie a sequece of legth, there are: 1 + 2 + + = ( + 1) 2 such pairs. This is o the order of 2, but ca we do better tha this? Hit: Take adatage of the fact that the gie sequece is sorted!
Biary Search The Aswer If we rearraged the equatio ad put it i the form y = x, we ca rephrase the problem i terms of whether if such a y exists i the sequece for eery x i the sequece. The idea is the to let x ru oer the sequece, compute y as x ad use biary search to see if y exists i the sequece.
The Repeated Biary Search Algorithm i Pseudocode 1. Let be register 1 ad be register 2 2. register i 1, register oe 1 3. while i 4. read ito register x the memory cell at address i 5. y x 6. if BiarySearch(y) = yes 7. retur yes 7. i i + oe (effectiely icreasig i by 1) 8. retur o
Ruig Time I the worst case (this happes whe the output is o ), the algorithm eeds to ru biary search times. A precise cout yields a time of f () = 3 + 7 + 6 log 2. This is a improemet oer the aïe algorithm we cosidered preiously. The story does t ed there howeer... because we ca do ee better tha this!
A Ee Better Algorithm I fact the sortedess of our sequece of itegers meas that we ca fid the solutio by cosiderig each term oly oce! The idea is as follows: 12 29 2 3 5 7 11 13 17 19 23 29 31 37 Coceptually we will hae two poiters (as highlighed i blue), ad they will begi by poitig at the start ad the ed respectiely. If we summed the two umbers beig poited to, we get 39, which is greater tha the desired alue of 29.
A Ee Better Algorithm 12 29 2 3 5 7 11 13 17 19 23 29 31 37 This tells us that: 37 will eer appear i a alid solutio, because 2 is the smallest term i the sequece ad the sum is already greater tha the desired alue; ad i order to get a solutio, we should moe the right poiter towards the left, sice this will decrease the oerall alue of the sum.
A Ee Better Algorithm After fidig that 31 ad 29 are also too large, we arrie at the followig: 12 29 2 3 5 7 11 13 17 19 23 The sum is ow too small, ad what this tells us is that 2 eer appears i a solutio because 23 is the largest alue that we hae, ad that the solutio is still too small meas that 2 plus ay other umber i the sequece would also be too small. This meas that we should moe the left poiter towards the right to icrease our estimate.
A Ee Better Algorithm Oe shall fid after moig the left poiter that 3 ad 5 are also too small, arriig at: 12 29 7 11 13 17 19 23 Ad ow it s too big ad we eed to moe the right poiter, etc.
The Mai Idea Essetially, if we thik of x as the elemet beig poited to by the left poiter ad y as the elemet beig poited to by the right poiter: if x + y =, we re doe; if x + y >, we eed to make the sum smaller, so moe y towards the left; ad if x + y <, we eed to make the sum bigger, so moe x towards the right.
Pseudocode 1. let be register 1, ad be register 2 2. register left 1, right 3. while left right 4. read ito register x the memory cell at address left 5. read ito register y the memory cell at address right 6. if x + y = the 7. retur yes 8. else if x + y > the 9. right right 1 10. else 11. left left + 1 12. retur o This algorithm has a worst-case ruig time of 5 + 4.
Recap I this week s tutorial, we reiewed the biary search algorithm ad looked at a problem that ca be soled by repeated applicatio of biary search (although the best algorithm tured out to be ee cleerer). You are ecouraged to ru this algorithm o some iput sets ad coice yourself that it actually gies the correct result.