Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Chapter 1 (0:00)

0:00 - 1:12
the following content is provided under a Creative Commons license your support will help MIT open courseware continue to offer highquality educational resources for free to make a donation or view additional materials from hundreds of MIT courses visit MITopencourseware atocw.mit.edu all right good morning everyone let's get started a new new module today we're going to uh spend a few lectures on uh on randomized algorithms and so um not only will we look at U slightly different waysof solving old problems like sorting uh we'll also look at how we can analyze this new kind of algorithm that generates random numbers uh in order to actually make decisions as it's executing and uh we'll end up obviouslyuh with u analysis that uh gives us uh the expected runtime of the algorithm for example or uh whether the algorithm is going to produce the correct result or not with what probability will this algorithm produce a correct
result so I'll talk a little bit about uh why we're interested in randomized algorithms in a in a couple of minutes but let me Define what uh a randomized algorithm or a probabilistic algorithm is to start things
off and so randomized algorithm is somethingthat generates a random

2. Chapter 2 (1:51)

number now this could be a coin flip but more often than not you're generating u a real number that comes from a certain range sometimes you're generating a vector you'll see a different uh a couple of different exampleshere in today's lecture uh and in and in section and it's going to make decisions based on this
value based on R's actual value
now you can imagine that an algorithm would be recursive and at every level of recursion it's going to generate a random R and so uh when you're executing at a particular level of recursion you may be doing differentthings uh based based on R and not only that if you rerun the algorithm again on the same input the execution will be different because U you're assuming a true random number generator as opposed to a a Pudorandom one uh and and the RS that you're going to get at different levels of recursion or through the execution of the algorithm are going to be different from the first time to the second time so on thesame

3. Chapter 3 (3:18)

input on different executions two things might happen um the algorithm may run for a different number number of
steps so you might get lucky on the first execution and the algorithm finishes let's say in 100 time units the second time around it takes a a long time it takes 700 time units um our goal here isto try and analyze what this probabilistic runtime would be to ask for um an expectation to be able to compute an expectation for the runtime or if you're talking about a different scenario where different executions I could actuallyproduce different in different
outputs and in this case um it's possible that uh uh one or more of these outputs are incorrect you actually get the wrong answer uh and uh obviously uh that's going to happen with a certain probability we're goingto have to u to decide or analyze what that probability is and uh generally speaking we won't be happy with a high probability of error as you can imagine and we'd like to set up an algorithm such thatyou can reduce that probability of incorrect output to be something really really small uh and it might take you uh longer to get to that that uh low level of uh incorrect error or incorrect output uh in onecase uh for a certain set of inputs versus another case right so uh that's this setup here in terms of randomized um you're going to have algorithms that are uh you can think of them as uh probably correct
okay so these are algorithms you want to think of them as probably correct and they do have a a name they're called Monte Carlo algorithms um and then you have algorithms that are probably
fast so in the case of pro probably correct uh you you could you know have a constant probability that they're going to give you the correct answer 99% uh and you could obviously try and parameterize that in thecase of probably fast um you say things like it runs an expected polinomial time and um really what that means is that uh you may have to run it uh for more iterations so rather than taking uh 100iterations or 100 uh steps to sort something it might take you 110 right but in the case of probably fast you do get the sorted result at the end and when the algorithm is finished execution you do getthat sort of result at the end so correct and probably fast um or uh uh probably correct and uh deterministically fast okay and this is this is Las Vegas so you have Monte Carlo versus Las Vegas here um
so I uh yesterday I and it occurred to me and I've thought this class A bunch of times but it occurred to me for the first time last night that there should be algorithms that are uh probably correctand probably fast which means that they're Incorrect and slow you know some of the time right so what do you think those algorithms are called sorry what the T the T oh oh that deserves a frisbee oh mygoodness all right here you go there you go all right now they're not called the T okay so we should write that down for uh so so everyone knows uh probably
correct and probably fast which is I guess yeah I I guess they don't get you anywhere I don't know what that means Incorrect and slow in the case of the T um uh so the MBTA huh okay nowthat's that's not the any guesses I mean think about uh what we have for uh for Monte Carlo Las Vegas you extrapolate these are the kinds of questions you're going to get on your quiz so what other youI guess you guys don't get you don't go to yeah yeah go ahead Atlantic City that deserves a frisbee yeah absolutely right there're actually it turns out Atlantic City uh isn't uh isn't a name that's really caught onuh but it was uh in terms of uh being used in this context uh most of the time if you do have a probably correct probably fast algorithm you can convert it into a Monte Carlo algorithm or a
Las Vegas algorithm there are some uh Prime testing algorithms uh to test whether a particular number is a prime or not that run in probabilistic polinomial time and they may incorrectly tell you that uh the number is aprime so that's uh an example of an Atlantic City algorithm um we won't actually do Atlantic City uh what we'll do is we'll take a look at u a couple of different algorithms and and both of these willmotivate why randomized algorithms are interesting the uh Monte Carlo example is is checking Matrix multiply so you've gotten a couple of square matrices both of them are n byn matrices and uh you multiplied them out a * Band you produce C and so you got the C Matrix and I rather than REM multiplying and checking the result you'd like to do something better uh you'd like to verify with some probability that you can parameterize that
the output Matrix is in fact the product of the two input matrices and so that's a randomized algorithm that's Monte Carlo because you're not guaranteeing that that output Matrix is in fact the product of um the first twomatrices or the operand matrices but you're getting a good sense of uh of uh How likely that is and you can kind of Squish that probability of error down to uh um how low you want it to uhbe except you have run it run the algorithm for longer right so that's that's an example of Monte Carlo now quick sort it doesn't make sense to say you you I guess you could but it doesn't make toomuch sense to say that you have an almost sorted array right uh um what does that mean exactly you have to characterize that so quick sort is an example where you're guaranteed to get a sorted array at the
end of it right so it's correct you will get a sorted array that's what you wanted descending order ascending order but uh it might not run in uh order log n log n time uh you can't that's expectedtime order n log n is expected time right so that's what probably fast uh would correspond to all right so uh so that's the setup uh you can kind of see why these are interesting because you could imaginethat in Practical scenarios uh you might want to do some checking in a in a probabilistic way uh uh and uh you want to do that with having to redo all the work right you don't want to youyou don't want obviously you don't want your checker for Matrix multiply to be as slow as multiplying the two matrices otherwise it makes no sense so let's dive into u a matrix product and our first example of a

4. Chapter 4 (11:20)

probably correct algorithm a Monte Carlo algorithm so what I want to do here is c equal a * B and the simple algorithm I guess those of us who went to high school myself included did my four
years um know of an N cubed algorithm or learned it back then uh that it simply corresponds to uh taking rows and uh and and columns and uh uh you you get get an entry you have n Squareuh entries that you need to compute corresponding to the output Matrix C and uh you're going to do um order and multiplications and additions but we're really going to consider multiplications here when I talk about n here it'snot the total number of operations it's the number of multiplications and the reason for that is I this may have gone away a little bit but uh it's still probably true that multiplication is just uh in computer ittakes longer uh to multiply two numbers uh integers or floating Point numbers than adding numbers um used to be much more dramatic the difference between a multiply and an add uh in in in uh uh computers but you
know thanks to pipelining and lots of optimizations uh multiplies are actually very fast but uh they are obviously a more sophisticated operation than addition so we'll be counting multiplies right so so when you've seen CaRu divide and concerfor multiply you know back in6 I remember that we were counting multiplications and we were actually trading off multiplications for additions we were trying to shrink that uh that number associated with the complexity of the algorithm when countingthe number of multiplies and if you actually counted the number of additions they were they were going up at least from a constant Factor standpoint not necessarily from an ASM totic complexity standpoint um and so that's the simplealgorithm um youve probably heard of Sten some of you might have seen it um Bas essentially what happens with Sten is you multiply two 2x two matrices um using seven multiplications as opposed to
eight and so now if you do that and this is similar to the katuba analysis you get uh you can you can do this in N ra to log 27 time which is uh essentially n ra to 2.81time and so rather than n cubed you can go down to n uh n ra to 2.81 now it turns out people have obviously uh not stopped with this uh you can go to n ra 2.70 by doingsomething of the order of 143,000 multiplications for 70 by 70 matrices right so you can play around just like you had tomb cook I don't know if you remember that or it got covered but you know caruba couldget generalized into this thing called tomb cook and uh and the same thing Sten you could go off and and do uh uh divide and conquer that uh uh whose base case is not 2 by two but 70
by 70 and that improves things but it turns out uh you know there's other ways arithmetic series summation ways and so a famous algorithm that up until 2010 was the uh was the best complexity uh algorithm known wascopper Smith Winograd which is uh 2376 and then at some point we had a faculty candidate here who uh who uh um uh uh you know shrunk this from 2.37 6 to 2373 um and it turns out thatuh there were two different uh researchers who came up with the 2373 but this particular candidate you know in the six decimal place won right so she had an eight and the other person had a nine or somethingbut anyway all of these are impractical okay you you don't want to use them the the constant factors associated with these things are much larger uh than uh what you have here I mean the constant factors here are
I guess it's one right make sense that it would be one yeah um so uh forgetting the additions of course right so if you have large constant factors then you need a billion by a billion Matrix in orderto win and if you have you know billion by billion matrices that you want to multiply do something else okay uh you don't want to go there uh even in even even in the day of the Internet it'sit's not going to work so uh what we'd like to do now is do something better so we will try given that I we are uh theoretical uh computer science class um it makes sense to say that ourverification algorithm should be better than n raised to 2376 or 2.3 whatever right uh otherwise it you know it doesn't feel good right so um so what we'd like to do and we can do this is uh try
and get um an order n Square algorithm

5. Chapter 5 (17:02)

uh that uh does this so it's um a probably correct Monte Carlo algorithm where if you have a * b equal C then the probability of the output equals yes is is one so in fact uh if yougot it right then the verifier is going to not give you a a false negative right it's not going to say no you got it wrong when you got it right uh but it could give you a afalse positive with someprobability where you have the probability of output equals yes and that's a false positive but you can bound that to be less than half okay so it's going to say yes so I mean obviously if the verifier keptsaying yes all the time uh it you would't have this right it wouldn't be very interesting it would be constant time uh but uh it it wouldn't be very interesting uh what is interesting here is that uh uh
when they're not equal uh you're going to get an incorrect result with with some upper bound on the probability right and so so you say well 1 half it seems kind of high you know 50% you know flippinga coin the good news is that uh you these algorithms can uh you can run them over and over you can run this Checker um over and over and as long as the executions are independent and you cancertainly ensure that they're Independent by ensuring that the randomness from one execution to another the flipping of the coins are independent okay and so that's relatively easy to do and certainly in all of the scenarios we'll be lookingat in know for six is relatively easy to do you can now drive this probability down to one quarter with two executions uh because you'll just check different things and then 1/ 18 with three and so on and
so forth right so that's what's cool about it and now if you kind of look at the runtime you say well runtime is still order than Square now that's the beauty of this right because I'm just putting anextra constant Factor here where I have K and square right where K is a constant and I got this uh effectively I have this nice relationship in terms of the probability um of error going down to 1 /2 raed to K right and what I have here is a KNN square right so so that's what's cool about it and obviously KNN square is still order n Square in a polinomial time and but the the probablycorrect aspect of it gets better and better okay um any questions so
far all right right good so what we're going to do is uh this algorithm actually works uh for uh arbitrary matrices at the structure at least we're going to assume that the Matrix entries are are Boolean or we'regoing to work in the the finite field mod two and it's just an easier proof uh it's easier to see so uh the complexities are the same you're still multiplying numbers they happen to be small a multiplication costsyou one operation and you need to do n cubed multiplies to actually get the C Matrix and you have to uh uh verify it in order and square time right and so the number of multiplies again that youwant uh to use in your verification algorithm has to be order and square right we're ignoring the additions right um so uh that's that's what we'd like out of our Matrix uh product Checker and the algorithm we're going
to look at is called free walls
algorithm cute little algorithm that uh does uh the following so so the algorithm itself is very straightforward you know a couple of lines a minute or so to describe and the the the interesting aspect of it is theanalysis the fact that uh you can show this that's that's the cool part if you could show that there's nothing cool about this algorithm so we're going to choose um a random binary Vector so there you go here'syour Randomness and this binary Vector every time you run it as the K increases here the random binary Vector is different from one to another that's important you can't run the same thing again and then expect a differentresult right that's called Insanity U but uh you are going to assume that uh given that we're working in the space the binary space and this is a binary Vector
uh you can assume that r i equal 1 is half independently for I = 1 through n and and the algorithm essentially is this we're going to do a bunch of Matrix Vector multiplies okay uh a matrix anN byn Matrix multiplied by an N byn Matrix gives you an N byn Matrix and that's your n cubed right so these are all I think I said this but I should have written this down these are allsquare matrices that are n byn and that's where you get your n Cube um a matrix Vector would be something where you have typically we have a column Vector here you're going to get something like that and uhyou have n Square multiplications here right so each uh you you got to grab one of these uh and then multiply it by that and get an entry here and that obviously is um n multiplications uh but you
only have n elements to produce here in this Vector so you only got n squ right that Mak sense um and so so what we're going to do is we're going to take this R and we're going tocompute a * BR R and so the brackets are important because uh it says that you're going to compute what's inside the brackets first okay otherwise it' be a problem because you'd be multiplying a * B and obviouslythat's order n Cub right you don't want that so A * BR R equals CR okay so R remember is a is a column vector and C is an N byn Matrix as are A and B we're goingto Output

6. Chapter 6 (24:15)

yes um else if if these two are not equal you're going to Output no okay and so that's it that's one run of the algorithm generate a random R and do the do the multiplication uh as you seehere so let's uh uh be uh clear about complexity uh and uh let's make sure we understand the simpler aspects of the algorithm before we get into the analysis associated with bounding the false positive probability okay that theHeart part is going to be bounding the false positive probability um and so uh but the easy part is uh first the complexity right and uh so how many how many Matrix Vector products am I doing here howmany Matrix Vector products am I doing here in this check on one ex iteration of the algorithm yeah three all right uh all right you need to stand up this is fun this is this is this is the
the hardest throw I've had to make in 6046 I got to put this down warm up a little bit right so yeah it's kind of cold you know what you whoa terrible all right uh you can the personwho gets up and gets that owns it and we're going to do this again right all right let's let's see how long this takes is this yes well the first one fail you know false whatever right all right
I got a few more all let me see I think I need to go here right this is good and I need to uh I need to be all right number three thank you thank you all right soso um it was three right three perfect uh three Matrix Vector products because I got to do this that's the Matrix Vector product remember I'm getting a column Vector out of this which is important and and then I'mgoing to multiply this Matrix with the column Vector Matrix Vector product number two and then there's a matri VOR Vector product over here right so then at that point remember I have a vector and a vector and checkingthe equivalence of two vectors is simply checking the equivalence of each element in the vector uh one by one so first one same as the first one second one the same as the second one Etc um and so
so so this is order and square but three is is is something that is worth thinking about simply because every once in a while we're interested in constant factors and the other thing that's interesting about this um makesure I write this let me write this over here um that uh you are going to uh if um AB uh equals c uh then and uh there's no issue associated with uh error here right so there's nonotion of a of a false negative because if AB equals c then uh you know thanks to the associativity of U matrix multiplication be it whether they're n byn matrices orum or columns you have uh this relationship here and I hope you can read it at the back essentially what I have here is if a equals c so if in fact uh The Matrix multiply happened correctly uh
I'm in a situation where um it is clear that AB R equals uh this thanks to the associativity of a matrix multiply and that of course is exactly the same as as CR right so that should convince youuh thanks to associativity of Matrix multiply that you don't have any false negatives in this algorithm right make sense um so we're all good all we have to do uh given uh what we have with respect to freeallis to do this part here which is going to take a little bit of doing um and the challenge always with a simple algorithms is you don't quite know why they work and then of course you know youhave sophisticated algorithms and you don't quite know where they work but

7. Chapter 7 (29:00)

so this will take a few minutes it's not super complicated there's a little insight as always with these things that are not immediately obvious uh but we'll have to um look at uh the number of RS uh theso you have an R Vector that you've generated randomly and it may be a bad Vector it may be a vector that doesn't show you that the two matrices or the product Matrix has an incorrect entry remember there'sn Square entries in the in this Matrix exactly one of them may be wrong and you need to find it right uh uh and so there may be a lot of entries which are all correct but you gotto find that one entry that's incorrect and so you could miss it a given R Vector might miss it and of course now if you keep generating the RS you'd like to find it uh and uh decare that
the matrices Wen multiplied correctly uh and that probability is what we have to compute so uh we want to get this result where we're analyzing the correctness um in the case we've already analyzed the correctness uh in thecase where um AB equals c but now we have to analyze the correctness in the case where AB is not equal to C right um and so uh the claim is that if a is not equal to Cthen the probability of a r not equal to C is greater than or equal to half right so this is greater than or equal to over there I'm just talking about the false negative probability where I'm actually gettingan incorrect yes when you have uh the matrices being multiplied um uh uh wrongly incorrectly and so that's why I get uh this is what I want I want there to be a a greater than 1/2 probability for
R to be um uh to have discovered that for for R to have discovered that okay uh I'll stop for questions in a second in case but let me do a little bit more I'm going to compute thedifference Matrix uh and I'm not Computing this because obviously this would take a while to compute it's just for the purpose of analysis I'm going to look at this difference Matrix D equal a minus C because you wantD to be zero and we're going to do some analysis that says we're going to try and find these nonzero entries in D because clearly the nonzero entries in d are tell you if there's non-zero entry in Dyou got a problem here the matrices we multiplied properly right so so that's why we have D here don't think of it as we're actually Computing that um so what we'd like is

8. Chapter 8 (32:02)

um uh is uh to as I said discover these entries where our hypothesis now is that D is not equal to zero because that's the case we're considering we know that D is not equal to Zer if thematrices were multiplied incorrectly and when I say d is not equal to zero it means that there are n Square entries in D and one of them is not zero right it they all have to be identically zerothat's all it means d not equal to zero means one entry at least is not zero and so now what we need to do is we need to show
that there are many R there's obviously uh it's a binary Vector of length n and you can obviously you think about 2 ra to n possibilities with respect to R and what we really want to show is thatthat there's a large fraction more than half of the RS are going to actually discover uh that that the matrices were were multiplied incorrectly okay so we want to we want to show that there many RS uh suchthat Dr is not equal to
zero and because if Dr is not equal to zero then uh you're obviously going to discover that AB r is not equal to CR right so if a r is not equal to CR that's identical to saying thatDr is not equal to zero that Mak sense um so specifically if you look at the claim and writing it in terms of Dr we want to say that the probability of Dr not equal to zero is greaterthan or equal to half for a randomly chosen

9. Chapter 9 (34:03)

R okay this is
looking and so that's it that's the setup uh that uh we have to show we have to do a counting argument corresponding to uh these R vectors that are being generated randomly so let's do that um
so the general argument we're going to make here is simply that we're going to uh roughly speaking we're going to look at a bad R what's a bad r a bad R is something that doesn't Discover the incorrectmultiplication right that's what a bad R is so um your your D is not equal to zero but Dr equals zero okay that's a bad R right it's quite possible that that would be the case um and souh uh you you want to try and figure out how many of these bad RS are there because those are the ones that are causing the the false uh negatives right uh um so uh that counting argument isthe Crux of uh the proof of the claim right so let's let's look at that um and what we're going to do is we're going to pick a bad R and we're going to say there these good RS
that are associated with this bad R and for every bad R there's a good R right and a good R is something that actually discovers the incorrect multiply and and given that for every bad R there's a goodR you know half of the RS are good RS right that's it so I'll write it out that's the essence of the argument and I'll go a little more slowly so hopefully you you'll get that but so let'slook at the case where Dr equals zero case because that's the interesting case right that's the case where the r is bad even though we had an incorrect multiply and you get this um um uh this uh Ishould have said false U you get a false positive right so I'm sorry I think just before I I said false negative but I meant false positive right so you you have a false positive in this case um
and D equal AB minus c not equal to zero implies there exists an i and J such that D J is not equal to zero okay um and there's just one entry at least if you say that Matrixis not equal to zero there's got to be an entry that's not equal to zero so let's take a look at that entry and let's just draw it out that's my D Matrix and there's going to be anan i and a j so that's my I row and my jth column and there you go I have an entry here which is D J and uh I'm just picking that I don't care what I and Jare but there's got to be an entry that's not equal to zero now I'm going to create a vector v right so this Vector is a is is not R it's a vector v that is chosen uh deterministically
given the DI where it's got zeroseverywhere except at at VJ so if this is the if this is the J entry columnwise everywhere else you got zero and you just got the one uh associated with the uh The The J going downward The TheJ entry okay so it's a one hot Vector if you will it's got one one so now if you multiply these two things out you know that you're going to get something and we can call this uh DVright so you take D and you multiply it by V uh Matrix multiplied by a a vector um you're you're guaranteed given that all of these are zero when I do my this times that plus this times thisplus this times this all of these are going to produce zero this times one is going to produce something that's non zero and then all the other ones are going to produce zero right so I'm just adding a
bunch of zeros to this nonzero multiplied by one so I'm going to get something that's non zero right all make sense um so I'm going to see something here which is the J entry that's not equal to zeroall and so that implies that DV is not equal to zero and in particular what I'm saying is um DV of J so if I just look at that entry that is identically D J which is not equalto zero because I'm multiplying it by one and I'm adding a bunch of zeros to that's it okay yeah question J or I um so I picked the J here so um I think I'm going J J rightthat make sense right so I go one if this was if J was seven and and this is seven down then I'm going it would be it would be the the the the the seventh would it because this
is going to turn into that right okay um now either way I if I you know picked it in the middle uh it doesn't really matter the point is there's going to be one entry so hang in therethere's going to be one entry that's non zero okay if you didn't quite get that um so di J is not zero and this is one more observation we're going to make in order to do the counting ofthese bad RS because this is a this is a bad r that we're looking at if you say that Dr equals zero um we've created a v that has nothing to do with r but we're going to usethe V to go from a bad R which is our example here to a good one right that's pretty much it uh that's the last step here so what we're going to do is we're going to take any
r that can be
chosen by our algorithm and such that you know Dr equals zero because that's the case that we're looking at and um we're going to compute RP Prime which is r + V okay we're just going to and thisremember this is mod 2 arithmetic uh you're only going to get zeros and ones so if you have 1 plus 1 it gives you zero obviously 0 plus 0 gives you zero and uh the other cases are clearand this plus here remember is also other thing that's important is this is not only mod 2 um these are all vectors so your R is a vector uh and you can think of it as a column Vectorthat's how I drew it uh you you're adding up a column Vector with a V that's a column Vector the way I drew that uh you could do this with rows if you like but it's just notation and

10. Chapter 10 (41:36)

you're going to compute an r r Prime here um what can you say about Dr R Prime someone yeah go ahead it's not zero uh and and uh you'll get a I'll give you a frisbee but then youcan explain I can you stand up I don't want to take this lady's head off um so can you explain why
R absolutely right so essentially what we have is this is simply woo yellow um Dr + V uh highlights um 0+ DV not equal to zero all right do we like yellow or do we like white yellow isfine um so so that's pretty much it so what's cool about this is this final step which I think you've gotten but I'm just going to say it out loud now which is that um R to R Primeum is one: one for for any given R such that Dr equals zero um given the situation where you know capital D is not equal to zero and there's some Di and there could be many ds I justneed one I've constructed based on that di this this V Vector which has the J entry corresponding to the V Vector being a one with all of the other entries being a zero but I can now create an
R to r that is one to one in the sense that um if um R Prime equal = r + V and that equals R Prime + V right so if I ever have a situation where in orderto show this one to one I want to say that it's not many to one right so or even two to one so if I have an R Prime that equals r + V and you tell me thatRP Prime also equals R prime plus v i can make the the argument that R and R Prime are exactly the same right so then Ral R Prime right so what am I saying there um I'm just I'mjust saying that um for any given any given R uh that this has Dr equals Zer I can twiddle at the J element of that R and go from 0 to 1 or 1 to zero if you tell
me that there's a DI J somewhere in that Matrix that uh uh is non zero and I do that little twiddle remember it's all zeros and ones Boolean matrices so if I do one twiddle um it it's aone to one uh if I do two twiddles and I go you know one to zero I'm back to I'm back to one again and that's all this says because you have mod two that's all that says rightso one little tweak um and I'm going to be able to take a bad R and turn it into a good R because the good R the r Prime in this case has Dr Prime not equal to zerookay and that's it that's my counting argument and All That Remains is to essentially close this by saying um just to write this out uh to get to the to get to the final claim and get the 1/2
the one: one essentially gives you the 1/2 half of at least half of these things are going to be good RS um if you had if you had a
Dr if you had a Dr that's not equal to zero uh and that's that's the case that you have here um then we're going to discover an RP Prime such that the Dr Prime is not equal to zeroand R to RP Prime is a one to one mapping

11. Chapter 11 (45:58)

um and so the number of R
Prime for which Dr Prime is not equal to zero is greater than or equal to the number of R for which Dr equals z right and so that implies that the probability of Dr not equal to zero soif you just choose an R this is now a randomly chosen r i mean not that others weren't but I'm I'm treating it a little bit differently here this was a specific R for which Dr was equal tozero I made an argument that you can always get this R Prime 1 to one such that Dr Prime is not equal to zero and now going back to what I had initially with respect to the claim herethe r here was a a randomly chosen R I'm I'm saying thanks to this little argument this line up up top I'm I'm going to be able to say this is greater than or equal to2 okay cool any
questions

12. Chapter 12 (47:17)

yeahum on the last colum be this should be Iyes people agree with that majority vote all right I'm good let's make that aney oh yeah of course once you do that yeah once you do that you you you you have to have an eye there good um so you're looking at uh you're looking at a particular entry it it itmakes a difference whether you used a column or a row if I done uh um now that I remember uh um if you think if you turn this into a a a row Matrix and this becomes a rowMatrix you'll essentially get the dvj okay so it depends on which way you look at it but thanks for pointing that out um it's the specifics of I andj weren't particularly important to uh the proof itself uh thekey thing is you zoom in on a particular entry that is not equal to zero uh and then you tweak uh that entry corresponding to the R so uh once you tweak that you make that zero or one
or one to zero you can get this result um and uh uh I'm sorry I'm pointing the wrong spot this result uh and and then you get your you get your claim okay so summarize uh we have abound we run it over and over uh and we get it to the point where we can have a 01 prob ility that uh if the matrices were multiplied uh incorrectly uh that you wouldn't discover that because youran it long enough that for enough hours independently chosen that that probability becomes as low as possible okay so that was uh um Monte Carlo um let's do uh a Las Vegas algorithm and you guys are probably thinkingmy goodness another sorting algorithm uh after I don't know 17 different sorting algorithms but let me this all sorting algorithms that you've ever learned so far right so mer sort doesn't work um and the reason it doesn't work
in practice uh if you're really into performance is because um is is because of um the auxiliary space that merge sort requires right so if you recall um there's uh the notion of uh in place sorting so let'suh move on to the next thing here which is um uh quick sort which is a new sorting algorithm and I want to motivate it for just a couple of minutes and the primary motivation really is practical performancenot U asmic complexity so I I'll be uh upfront about that uh it's it's it's all about practical performance corresponding to quick sort uh and uh quick sort is divid and concer algorithm randomized algorithm invented in 62 unlikemerge sword it's got two interesting properties uh the first is that it's in place like I just said so no auxiliary space in merge sort um you can try and get around this uh no I should say order
n auxiliary space you need U uh you need a little temporary uh variable in order to do swapping uh but uh you have uh you don't have the order and auxilary space so you don't have to constantly allocateand remember n could be large it could be in the billions or trillions so uh from that standpoint quick sort ends up winning simply because of relatively mundane things like memory allocation in uh uh your your computer uhand uh the other interesting thing about quick sort in relation to sord is that all the work is in the Divide

13. Chapter 13 (51:37)

step um so in merge s remember we just split and we recurse uh and what happens uh when you when you uh come back is you have to do the the the the finger merging algorithm uh by lookingat the two sorted arrays and looking at what uh the new uh merge array is going to look like so the work is in the merge uh but uh in quick sort the work is going to be inthe Divide because we're going to have to um do a bunch of work um associated with figuring out how to keep the partitions balanced and a little bit like uh we had to do when we did uh medianfinding back a couple of weeks ago um I'm going to talk about three different variants of quick sort the the variant that we're going to spend the most time on is the Las Vegas quick sort where we'd like
to show that it's probably fast and uh make a statement about the expected runtime uh but we'll get to that uh by talking about a couple of other interesting variants and this will uh be uh elaborated on tosome extent um in section tomorrow so before we get to variance of course let's try and set up the structure corresponding to quick sort and as always we have an N element array a you have divide that correspondsto picking a pivot
element X in a and then we're going to a partition

14. Chapter 14 (53:30)

the array into a sub
arrays and what we have here is uh this little picture should uh make things clear and you kind of saw this um in the median finding but here we go again let's assume all the array elements are uniqueum we have l e and G uh L is less than G is greater than and so your pivot element is going to break this array up into L and G where you got all the elements uh thatare less on the left and all the elements that are greater on the right and you're going to recurse um on the LNG so recursively
sort
subarrays LNG um combine is Trivial or merge is Trivial because you've already broken things up thanks to the pivoting and uh you just concatenate those arrays and that's why you can do this in in place uh there's noissues uh you're really recursively sorting sub arrays you are moving things around a little bit uh when you do the partition obviously the initial array may have all the elements uh you may pick the pivot such that thepivot is all the way on on the on the right hand side in the sense that it's a large element uh that is not necessarily a good thing we'll talk about that but if you pick a an aninteresting privot or a good pivot uh you're going to have to move the elements in the array U to the left of the pivot if they're less than the pivot and you got to move the elements to the
right if they're to the right of the pivot um non-trivial piece of code not super complicated but you can look at U clrs at page 171 to look at in place uh partitioning uh where you don't have touse uh another order end space uh to move these elements around such that they look like that picture that I have up there starting from some random starting point right so you want to have your um uh thepicture that you have here and you need to go from some well the very same array uh needs to and X is somewhere here and you know you got X+ one here and X plus xus one here forexample uh you need to move those things around so they look like l e and G and U uh that's something that U that you can do in place and you can look at the code for that uh
in clrs I won't I won't cover that here um so let's look at a bunch of different variants corresponding to quick sort um and there's some real simple ones each of these we can knock off with respect toU uh complexity uh and runtime uh fairly easily with with one exception that we'll spend some time on which is the Las Vegas uh quick sword but we'll call these different names let's talk about the basic quick sortwhich is also a useful algorithm that people use uh in practice and uh amazingly this algorithm uh is is simply something that says I'm just going to constantly pivot on either the first entry or the last entry soI'm going to pick my Pivot to be A1 and um when I pick my Pivot to be A1 it's a value that I'm talking about here x is a value it's not an index it's the A1 value maybe
that's 75 then I'm going to create my L Matrix corresponding to this pivot where all the entries are strictly less than 75 and uh the G would be strictly greater than 75 and I could do that for A1I could do that for a n so remember that the pivot is a value uh um uh now if I look at this I'm going to do the partition uh given X just like you saw there and thisis going to be done in order end time it makes sense that you're going to look at every element and you're going to move it to an appropriate location to the left of X which is the E arrayor to the right and you do that that takes order end time and as I mention you can you can look at this to see uh how this is done um in place um so let's talk let's take
a look at the analysis of uh of basic quick sort and what I'm interested in is of course is the worst case analysis and I asked this question I think uh before when we were doing median finding butum what is the worst case complexity of the basic quick sort algorithm that chooses the pivot as A1 what is the complexity order and square right it's order n square and the reason for that is that um youmay have an array that is uh uh sorted uh or reverse sorted you know depending on whether you're picking A1 or a n you can have a worst case situation where one side uh L or G has nminus one elements and the other uh has zero elements um and so if you look at our recurrence associated with this you could have TN which is t 0 + TN minus1 plus Theta n right and why do
I have a Theta n here well remember that I I still have to do this divide step or this partition step um in order to compute this uh unbalanced array so I do have to look at each ofthese elements and do the comparison uh and maybe you know I I don't actually have to move them but I have to do the comparison with the with the A1 which is the X pivot and uh that's goingto do the uh and in some cases if I'm if I'm doing the wrong thing reverse sorted I also have to do the move either way I have a Theta n complexity associated with um The Divide step soif you go off and you look at what happens with um uh with this well you got uh TN = tnus 1 + Theta n which ends up with Theta n Square complexity so I hand waved a little
bit you know two weeks ago uh for a similar analysis but you can kind of look at it a little more uh precisely here by writing the actual recurrence out and you see that you get the recurrence tnaltnus 1 plus Theta n which is a an N Square uh result or the the solution is n Square okay so basic quick sword looks bad it's got a worst case complexity of theta n Square uh it workswell on random inputs in practice and it turns out that uh it's a fashion of algorithm partly because it's in place and it's easy to code that what people do is uh uh they take their inputs and theyshuffle them uh they just kind of you might get a bad input and it might take you a long time to run but if you take an input and you Shuffle it and you do that in Theta n

15. Chapter 15 (61:03)

time uh you just move things around and randomize the input then effectively you have a random input and this thing uh works pretty well in practice right now what is pretty well well we're going to do an analysisthat is going to um not be exactly the analysis that you'd have to do on basic quick sort with on on random inputs but essentially you can say that uh uh basic quick sort on random inputs is goingto run in um expected Theta n log n time okay uh it it's uh uh it's something that you'll see a little bit of how to do that today and and in in section perhaps for median finding insection tomorrow uh but that's uh all I wanted to say about basic quick sort um it's a practical algorithm um it does require a little bit of shuffling up at the beginning and then you can simply use the
pivot A1 and because you've done the shuffle uh generally you get balanced partitions the L and GS look balanced and you don't end up with Theta n square if you have any sort of balance associated with the twopartitions L and G you're going to get a nice divide and concer which is going to give you your Theta n log n okay so so that's a basic quick sort um there's uh another way to do thisand so this is a question for you guys suppose I wanted to use the quick sort strategy and get a uh a worst case Theta n log n through an intelligent pivot selection so I want to do apivot selection
intelligently okay so um how would I get under the structure of quick sort that you see up there uh on the left there how would I select a pivot such that I get worst case Theta n log n

16. Chapter 16 (63:06)

complexity go ahead linear median find linear median finding Perfect that's exactly right um there was a gentleman at the back who raised his hand and I decided I'd chicken out um I think one time to the back ofthe room is enough for a day right you can you can come have a frisbee left hopefully you can get one right um so U the intelligent pivot selection algorithm is the median finding algorithm because that's going toguarantee me that I'm going to get balanced partitions if you tell me that A1 and remember we're talking medians are values right so don't get confused with indices when I say something is a median I'm talking about thevalue that all of given its value that all these other n /2 uh values that are less than it uh uh roughly speaking and N over two values that are greater than it uh uh and so A1 I
have no idea whether it's large or small and uh so I couldn't say much about it but if I want to be worst case and I want to guarantee that I I have balanced partitions I can choose themedian and if I choose the median every time I'm going to get perfectly balanced partitions right they're going to be half on the left and half on the right um so and we do know a way of gettingbalanced partitions we can guarantee that balanced LNG using median
selection that runs in Theta n time and we showed that a couple of weeks ago now that median selection algorithm was non-trivial okay uh it uh it it had this weird thing where you broke things up into fivearrays of size subarrays of size five and you found a median of medians etc etc but we argued that the whole thing ran in Theta n time which is important and so now if you look at uh whathappens with quick sort and if I write the recurrence for quick sort thanks to selecting a median I effectively have balanced partitions so I have 2 TN /2 this is thanks to the median pivot median based pivoting that'simportant otherwise it won't work and then uh just to be very clear here um I got two Theta n terms okay uh the first Theta n term is the recursive median selection

17. Chapter 17 (65:59)

um and then the second Thea end term is of course the divide or thepartition but it's important to to realize that now I have a lot of work in the divide a lot of work I have to do an intelligence selection using this recursive median finding algorithm and I also have todo the moves comparing and then generate the L and the g arrays okay so those are the two Theta NS they obviously Theta n but but I wanted to make it clear that there's two things going on hereright and we all know that that is Theta n log n worst case all right so there is a way of using the quick sort structure template and getting a Theta n log n worst case algorithm which doesn'twork in practice uh because it's just too complicated right you know what's going on here is uh you have a recursion at every level of recursion you're going uh you know calling another recursive algorithm to find the median
right so if you go code this up um it loses to merge sword uh in practice you can do all of this in place but because of all these recursive calls it doesn't work well in practice right butit's good to know and so this is an good example I think uh which we don't do a lot off in 046 where you get a sense of the difference between asmic complexity and performance so while uh themedian finding algorithm has better asmic complexity where SK case it really loses in practice to the basic quick sort which essentially is a bit of a hack where you take an input and you randomize the input and youyou run it with A1 as the pivot or a n as the pivot right so um is there is there a different way that um you can actually get to a Las Vegas algorithm and it turns out randomized
quick sort is something that uh uh you can you can you can build and use which is a bit different from basic quick swort uh and certainly different from median finding but it kind of has a little bitin common with them and it's our uh example of a vas algorithm so what happens at randomized quick sort um X is chosen at random from the array a so you're not choosing A1 or a n you mightjust flip uh uh well effectively a an n-sided Dot and uh uh pick a particular index and then go grab the the pivot corresponding to the value at that index okay I mean you're not going to randomize overvalues you don't know what these values are but you can uh pick an a random index and then grab the pivot based on the value at that index um and so at each recursion a random choice is made
and um the expected time so now we're saying something different we're making um a a stronger theoretic statement that the expected time when you do this um for all inputs arrays a is order order n log n andso now this is not worst case time it's expected time right so this is going to be our analysis um uh in the last few minutes here to um analyze not randomized quick sort uh but a slight variantof randomized quick sort uh that is going to show you that you can run uh randomized quick sort and this variant in order n log end time right so um not quite sure what's going to happen in sectiontomorrow but the full analysis um is in the book um you should you you should read it uh it's as you can see it's a it's a couple of pages uh that includes the description of quick sort uh
that I have already uh but what we're going to do here is analyze uh s a variant quick sort which is a little bit easier to analyze and it gives you the sense of uh why in fact uhthe randomized quick sort is going to run in expected time and this analysis is easy to do in a in in a few minutes so uh we'll do that and tomorrow you'll see either a median finding analysis that'ssimilar to that analysis in clrs or precisely that analysis depending on what uh your ta want to do so this particular variant we're going to call paranoid quick sort right and so this quick sort um is U isparanoid in the sense that um it's going to be afraid of getting unbalanced partitions and it's going to keep trying to get balanced partitions so it's going to uh try to get a balanced partition it's going to check
and then if it fails it's going to try again and so at the end of it I there's obviously a an expect expectation associated with the number of tries that you need in order to get a balanced partitionbut it just sort of flips the problem on its head and says you know what um I'm just going to guarantee a balanced partition from a probabilistic standpoint and it might take me a little bit longer to getthere but that's what Las Vegas algorithms are all about uh it they're probably fast and once I get a balanced partition I'm in good shape because I can go do my recursion and I get uh my divide andconquer working properly right so what is paranoid quick sword absolutely straightforward you could probably guess given my description uh let's just choose uh a pivot uh to be a random element of a uh perform the

18. Chapter 18 (71:54)

partition and and then uh Val is a repeat uh so uh we're going to go off and we say until the resulting partition is such that the cardinality of L is less than or equal to 3/4 of acardinality of a and the cardinality of G is less than or equal to 3/4 the cardinality of a right so I'm allowing you a certain amount of imbalance but not a lot right um and and that's it that'srecursive U uh I'm sorry a paranoid quick sort uh you obviously are doing that in each level of the recursion and at each level of the recursion your L and G are going to be at most a factorof three apart so you might get one quarter and 3/4 if you're lucky you'll get a half and a half uh but the worst case given uh that you're going to be exiting out of this Loop is one
quarter and 3/ quter okay so uh as always you know you have a simple algorithm and it's uh not completely clear how you're going to get to um expected n log end time but uh it's not difficult uhbasically what we have to do is we have to try and figure out what the probability of a good call is over here uh a good pivot choice and what the probability of a of a of a badpivot choice is and we have to uh obviously given the uh potential imbalance we have to write the recurrence associated with that but let's take a look at uh the pivots here and um what can we say aboutuh the sizes of LG uh if you just did a random pivot well a bad call is when your you get uh uh something in L or G that is less than one quarter uh and a good call
is when you get 1/2 somewhere between 1 half and um uh uh uh well roughly if you look at uh the choice of the pivot so what I have up here is is the choice of the pivot ifmy Pivot is out here I have um I have a very small L and all of the thing on the right is G if the pivot is here I have a relatively small l and a large G ifthe pivot is over here I'm good um I got one quarter uh and three qus the pivot is over here I got 1/2 and one2 and so on and so forth and so this part is bad this partis bad and the middle part is good right so that's all that this picture shows um so a call is good with what probability given that picture a call is good with what probability it's greater than or equal
to 1/2 rightso um and so what you can now write simply is uh if TN uh is the time required to sort the array um essentially what you can write is uh you can say TN is T of n /4+ T of 3 n / 4 plus expected number of iterations in terms of getting a good partition time C * n and there's a reason why I'm putting C in here as oppos to Theta that will becomeclear in just a second because I can't really apply the Master theorem to this uh given what I have with respect to TN /4 and 3n /4 right so so what I have here is I'm looking at thecase where um I could get an imbalance partition but the imbalance is bounded so I could get I'd have n over4 on one side and 3 n over4 on the other side but I'm not going to have n
over 5 and you know 4 n over 5 or what have you um and so that's the two recursive calls so that's hopefully uh easy to see uh the part that is new here is simply the complexity ofthis code that you see here which is obviously a the randomized algorithm that's exactly where the randomness comes in because you're picking a random pivot and you're checking it and so um this is going to run a certainnumber of times and U we can figure out what the expectation is in just a minute but I have C * n because uh this is constant time to choose the random number whe assume that performing the partitionuh partition is is uh is C * n or Theta n and that's why I have this up there so this we're going to call as a CN um and so um expected number of iterations given what I
have what can I say about the expected number of iterations using U using uh simple probability rules what is that two right one / P all of them are independent so this is this is two and so umuh what I have here uh is uh something that I think you might have seen this before uh but it's worth drawing the tree out and seeing it one more time in case uh um it didn't U fullyregistered the first time or you didn't actually see it uh in6 or regation but um what I now have is T of N I want to solve T of n n = t of n / 4 + Tof 3n / 4 + 2 * CN right um and um again like I said I didn't put Theta n in here because as you'll see when I draw this uh this tree out uh because it's not a
master theorem uh invocation uh it's worth looking at it from a constant Factor standpoint to really get the sense of how all of this works out um and so if I draw the tree of execution I start countingum basically what I have is 2 CN up at the top I have 1 over4 * 2 CN over here I have 3 over4 * 2 CN over here and then this 1 over4 might go 1 over6 *2 CN over here and this might go 3 over 16 * 2 CN over here and and this would go um I guess it would be 3 over 16 * 2 CN and then uh 9 over 16 *2 CN Etc so this is an unbalanced tree because you have uh an unbalanced partition up on top and now you want to count up all the work uh that this uh uh this tree does if you collect
up all of the operations then that's going to tell you what T of n is because that's all the work that you have to do in order to uh finish up the top level of recursion and what youcan say is if you look at this side here all the way to the right hand side you're going to have log of to the base 4 over3 * 2 CN levels right so that's just simply every timeyou're multiplying by 3 over 4 when do you get down to uh the number one and that's log of 4 over3 and then over here it's a little bit easier to think about because it's a power of twoyou're going to have log of four to the base 4 * 2 CN levels and really it doesn't really matter honestly when we go to astics but it's worth seeing I think uh just to get a sense of
um why it all works out uh regardless of whether it's uh N4 or or or a different constant here or whether it's balanced or unbalanced the tree looks a little bit different um it it's sort of weird youknow it's got a uh uh fewer levels here and and more levels there so s of tilted this way uh but uh eventually you get down to uh Theta 1 constants down below um and basically what you cansee if you just add it up uh is 1 over4 + 3 over4 is 1 1 over 16 3 over 16 obviously those are all uh those are all uh end up end up being one so you have2 CN work at each level and uh if you just go ahead and be pessimistic about it uh there's maximum of log 4 over3 times 2 CN levels and that's pretty much it um obviously now you can start
ignoring the constants uh you just keep the log here you don't care what the Bas is you got an N here so drop the 2 C drop the 4 over3 drop the 2C and you get your end logapp okay so that's pretty much it uh I'll uh stick around here for questions uh but you got an example of um of a Monte Carlo algorithm you got an example of a Las Vegas algorithm and tomorrow insection you'll see a slightly more involved analysis for something that looks a lot closer to the randomized quick sort so see you next time