CS 627 – Advanced Algorithms (Winter 2026-27) · unit page · YouTube
2026-11-26, 17:37

← Video 7-1: Randomization, Selection Lower BoundsVideo 7-3: Probability Theory Recap →

Click any sentence to play from there.

The transcript was generated automatically by YouTube's speech recognition and is offered as is. It may contain errors, in particular concerning technical terms, names and formulas.

0:03 We left off with the proof of this uh slightly weaker deterministic lower bound for finding the median of n elements. And um I've already explained that the information theoretic part doesn't quite cut it. Uh information theoretically you can say what is the minimal amount of order that I have to establish in among my n elements so that I can for sure conclude that the element I'm outputting is the true median. And for that essentially what you need is to identify out of uh all the n factorial possible orderings um establish that your input has one of those that are compatible with m being the median and only those. And if you think about it, the ones that are compatible with M being the median are precisely those where there's n half elements larger and n half elements smaller than the median and you know that they are smaller respectively larger. And different ways to compute this give you essentially one comparison per element just to certify that a given element is the median. If I tell you this is the median with n minus one comparisons, you can check that that's true. Now, that's great, but um not strong enough.

1:18 So, what we'll um what we'll need is a a slightly stronger argument. And here take this as an excursion that almost doesn't have to do with randomness is a deterministic uh lower bound. We'll construct an adversary, which is an algorithm of itself that hence our poor selection algorithm, our hypothetical selection algorithm. um answers to its queries, to its comparisons in such a way that it makes its life as hard as possible. That's the worst case flavor. And deterministic is important because the adversary can uh can can conspire to create one specific bad input for our algorithm without having to live with this fact that the algorithm might uh might have different ways how to continue. And so this really just works for the deterministic case. So the adversary will um maintain two sets. Uh first of all say M is the true median. This is from the analysis perspective. And uh we'll maintain these two sets. Um the elements that are smaller or the elements that are larger than the median. We've already established there's the essential comparisons. Those that are part of the certificate of correctness. I need to somehow what this what this weird insect-l like picture is just trying to show you it may be an indirect comparison that tells you this guy is larger than this guy which is larger than the median so they're both larger that is fine of course I need to have the same number at the top and at the bottom and I somehow learn these comparisons that transitively tell me something is above or below um because this has the shape of a tree every element essentially contributes one comparison. That's what we've that's the information theory part.

3:14 So what's left is uh somehow non-essential comparisons from the perspective of the certificate. Uh but we somehow um still need to count those. Okay, first of all uh why do you need these? You can also see that from the adversary perspective. uh suppose there's some element x here in the picture that is not in some in any way linked to m then I can move it up or below and that's the argument I gave last time uh either way the the deterministic algorithm crucially uh gives the same answer in both cases because it doesn't check what x uh does and so I can move it slightly above or slightly below the median different inputs same result there's going to be a the non-essential comparisons will be I mean there's only three things involved so far there's the large elements the small elements and M itself if a comparison with M is a useful comparison I mean um if I did another comparison between these two that would be a useless comparison not a non-essential one it's one that I I could have answered from just the knowledge I gained already. An algorithm may do these redundant comparisons, but it could have already concluded this is above this, this is above this. So clearly by transitivity, I could have answered that. So these we don't count.

4:44 They're not part of any optimal algorithm. But I might have comparisons between an element in L and an element in S. And the point is my algorithm doesn't know M from the start. So it's going to have to start somewhere. These are the non-essential comparisons. They're not part of the certificate. So they're not counted among these n minus one that we definitely need. But we'll show that you can also not avoid having some of these comparisons via building an adversary that makes any algorithm pay some comparisons there.

5:21 So suppose there's a deterministic algorithm given and now uh our adversary is answering the comparison queries whenever a asks for a comparison. The adversary will maintain these two sets S and L. But initially these are empty. It will also maintain a set of undecided elements where the the algorithm cannot decide yet whether this these elements are larger or smaller than the median. [snorts] I we'll use them in a slightly different way. We're not going to fix the median even we'll um think of the n elements being identified by their index in the input. This is a name. It doesn't tell you how they compare. And we'll construct with this uh algorithm that is constructing the input. We're going to take a fixed deterministic algorithm and the adversary while simulating the algorithm and tweaking its answers will modify the input towards a bad case input. So it'll eventually find once one bad input, but it's a bit of a um a dynamic procedure. So hard to tell um up front what this input would look like but we'll show that it exists. That's enough. Okay. So we start with these sets. Initially everything is undecided.

6:44 Now we'll do a case distinction depending on what the algorithm does next. And all we care about is comparisons. So it will always ask some two elements which one is smaller. If these two are not in the same set of these S, L and U, then well one is in S, one is in U or one is in S and one is in L any of these combinations will answer consistent to everything in S being smaller than everything in U being smaller than everything in L. So there we're kind of forced to give that answer. uh if both are in S or both are in L then um any answer that's consistent with whatever we answered earlier is fine and we won't care because these are essentially things we cannot bounce how many the algorithm does. The interesting ones are when they're both undecided. That's elements that the algorithm hasn't really learned much about. So we have a choice.

7:48 What we'll do is we'll have to answer one is smaller. Um it's rather arbitrary in this case. But then for us, for our bookkeeping, we put X in the small set and Y in the large set. So we'll eventually make it so that the median is between these two. Uh that is one non-essential comparison. By definition, we've made it one because the elements are in at the end in the two sets S and L. So, we paid a non-essential comparison or the algorithm paid a non-essential comparisons and two elements at this stage leave the set U. Okay.

8:29 And you see if everything starts in U, we can now make the algorithm pay a certain number of non-essential comparisons. [snorts] uh we'll always maintain that this these two sets S and L are equally large because this is the only place where we uh move elements between sets. Um initially they are all in U. And as long as we have elements in U, we'll always equally distribute them to S and L. And so as long as uh there's at least two elements left in the undecided part, the algorithm can't stop because it doesn't know which one is is the median. Once there's a single element in U left, then that guy is the median. And let's assume it's an odd number of elements here.

9:14 So because u initially is n and has to shrink to one, we'll have to repeat this process n minus one half times. And each time the algorithm pays a non-essential comparison. And these have to be on top of the essential comparisons. By the way, we count the way to think about this. I mean this is this is the end. Uh the way to think about this is you first run this adversary on your specific algorithm and it interactively constructs a specific bad input on this input node. You now run the algorithm and analyze what it does. The n half comparisons we counted here are all between elements for this input between elements in S and L. So they're non-essential. they don't help you identify the median and then the algorithm makes some output. So it will also have to have some elements compared in this first and second case which we didn't count here but these will have to pay at least these n comparisons for the essential ones. So in total n uh 3 n half or one and a half comparison per element is needed.

10:31 uh as I said this is um a weaker lower bound than what is possible to prove. You can bump up the argument to show uh 2n.

10:41 Uh for that you have to work a little harder on the argument and it's a it's a clever and cool proof but it kind of leads a bit far from where I want us to go. Uh at the end we don't want to talk about uh selection all all day.

11:01 uh you can prove that quick select is way above this bound. Um it's another uh interesting maybe for for another module at some point. Um how to do this proof with the right constant. Um we can actually do better than quick select. That's what I hinted at before by picking our pivots um more cleverly. Uh that algorithm is called the Floyd revest algorithm. This is older than the deterministic media and selection and you can you can look at the code in a couple minutes. Essentially it is quick select but we pick a huge sample of the input that's this s here. So we take n to the 2/3 many elements that will be a mostly representative sample of the overall inputs but it's still sublinear. So it's still cheap. With this sample, we can um uh do many things. In particular, we can select elements from uh you could even just sort it. I mean it's um a little a lower order term difference. So suppose you just sort this large sample and pick now not the median um but rather an element that is close to where you want your rank to be. So let let me draw a picture of this instead. Suppose you're looking for this rank. That's K.

12:28 Now you pick your random sample and you have to scale it to the sample size. That's why K hat has this annoying meaning. But think about it as a relative rank. I want to pick my pivots to be a little left and a little right of this K. So essentially this K head as a relative rank is is the same as K. But then I want to add one standard deviation of randomness. Uh I'm picking random samples. So I expect the median of this sample or the element at at relative rank k hat to be not exactly where I would have wanted it. It might deviate a little bit because I'm only taking a sample. So I leave some buffer or error an error margin. But usually if unless I'm extremely unlucky, my two pivots will surround this rank K. And so by doing um doing two recursive calls or by doing a partition around two pivots, you can essentially get rid of the elements that are outside.

13:39 And so this this range if you look at what SD is the standard deviation you can look this up from from statistics essentially it's the standard deviation of taking a uh an order statistic from a sample. Um by and large it's square root of the sample size times login. And so uh this is also sublinear. is is even smaller than the sample size. So if if that works out then this red range that you still have to recurse on is a little O of N term or I mean this is by definition little O of N. With good probability your actual element that you're looking for falls in this in this red range. So essentially you're doing um one round of partitioning if you're looking for the median. That one round of partitioning costs 1.5 comparisons. Why? Because you have two pivots now. So every element needs to be compared with the first pivot. And then maybe uh for the median case, right, you're looking at this is your target K and your pivots are hopefully just a bit left and right of that.

14:58 You compare every element with P and only those that are larger than P you compare with Q as well. And how many are larger than P? Roughly a half. That's why you have this 1.5.

15:16 Okay, so long story short, if if you um account for all the rest, it's all just a lower order term, it's only the first compar the first round of partitioning that costs you in the leading term and that is the 1.5N. So we can randomized we can give an algorithm that beats this lower bound of 2n right. So randomize selection can find the median in uh 1.5N comparisons. Now here um I fudged exactly stating what this statement means because the algorithm is random. So what does this mean that I'm doing selection with that many comparisons? Let's say expected value for now the expected number of comparisons because that's what we proved. Uh um well we didn't prove anything. So say in in expectation as this is the easier statement the algorithm is stronger. It actually achieves this with very good probability but the deterministic case set if you want a deterministic algorithm you can never bet do better than two end comparisons.

16:29 So in this case uh that's my my uh conclusion here. um we uh we can do something with randomness that is ar is utterly impossible without uh it's also a bit simpler. The algorithm doesn't need to do this median of medians business. Um if you look behind the much of this is just picking certain constants. The algorithm is just still quick select.

17:03 Okay. Um the algorithm shows two things. the algorith the randomized version is a bit simpler and it protects against adversarial inputs by picking this random sample. That's that's pretty much it. Okay, now back to uh the theme of of this module is hard problems. So what if I have an NP hard problem? Is this also the case that I can with randomization do something that I wasn't able to do deterministically? Uh, it's been it's going to be a slightly long-winded story to get there.