6.006 Problem Session
Transcript
[SQUEAKING] [RUSTLING] [CLICKING] PROFESSOR: All right, folks. I think it's about time to get started. Thank you guys for making it to our problem session and I guess talking to many of you guys virtually, given the number of people in the room. But that's perfectly fine. Right. So we're in problem session number two for 6.006. Hopefully you're all in the right place. I'm Justin. I was looking at a little spreadsheet of who's teaching what, and I think you guys are stuck with me
in a lot of these problem sessions, which actually, I think in terms of hours of video, means that you're stuck listening to my voice the most. So sorry in advance. But in any event, it's always fun to do this kind of thing. And it gave me the opportunity to do homework, which I have not done in a very long time. So last night, I worked through all these problems and came up with my own hacked-together answers for these, which thankfully more or less agreed with what was in the answer key, which I have and you don't. I think that we release on-- so there's a bit of a known bug in 6.006, which is that this instructor has horrible handwriting. I'm working on it. I went to the classroom and practiced the other day. But in the meantime, there are horrible handwritten notes that are posted in the learning module, at least for the duration of the talk today. And we'll decide whether we want to leave them up or not because they're not super exciting.
Just so you can make sure you can follow along with that, I'm writing on the board. And as with before, if there's ever anything that you can't read, just stop me. And I'll gladly fix. Cool. So right, so hopefully, does everybody have a copy of the problems? It'll look a little different from this. The green stuff is the answers. I get those, and you don't. Or sorry, I always ask this question when I'm teaching, and you never get an answer. Does anybody not have a copy of the problems and the problem session? Fabulous. OK. If you don't, there's a pile of handouts up here. Here, I'll grab one for you.
Yeah. Great. So the basic goal of the problem sessions is to go over a bunch of example problems, which I believe are mostly lifted from past years' homeworks and exams, and work through how we went about thinking about them and a solution or at least just kind of sketch out enough that we're confident you could fill in the blanks of a solution, given our limited time together. Right. And of course, one of the pleasures of teaching this problem session is this is not normally the class that I teach. So these really are problems I haven't seen before. And indeed, in the middle of the night last night, I woke up and realized my answer to the last problem was totally wrong. So if you feel that way sometimes, the feeling is mutual. So in any event, there are four problems on your handout that roughly, I think, are in correspondence
with the stuff we've taught over the last week or so. So the first problem involves recurrences. The second involves Infinity Stones. The third involves some kind of a queue stack structure. And the fourth involves all kinds of things talking to each other in a way that I got wrong. So I guess for lack of a more creative thing, we'll just go through these guys in order. And essentially, this is supposed to be an interactive session, although it doesn't really look that way. But since you guys are the people in the room, you have the advantage of being able to stop me the second that I, A, make a mistake or, B, say something that confuses you. And I'm relying on you to do that, OK? Nods. That's our deal today. Some kind of acknowledgment. Noses out of the laptops, maybe, for a second. Cool. Thank you. See, thumbs up. That's what I'm looking for. OK. All right, folks. Right, so let's start with the problem number one here.
So problem number one is solving recurrences, which is our favorite way to torture undergrads in algorithms classes in the first couple of weeks. And in particular, applying this master theorem. So I thought I'd spent about 82 seconds reviewing master theorem because this is sort of the giant sledgehammer for solving recurrences without understanding why you got the answer that you did. And then for each of these problems, conveniently, this homework problem asks you to do each big O thing twice, right? Once using master theorem to kind of get you used to just applying this little set of rules, which is sort of the most efficient way to solve recurrences. It just gives you a formula to look at. In addition to that, drawing a tree of computation and counting the number of computations you do. So the first two are basically straightforward applications of master theorem. The third one requires a little bit of thinking.
So naturally, I got stuck and wasted an hour yesterday trying to convince myself the answer was right. OK. So for a tiny bit of review, I know that you also did this in recitation. But it can't hurt to bring it up again. Remember that master theorem-- I believe that "master" really is just because it's general, right? There's not a guy named Master, but I notice that it was capitalized a lot in the course notes. But in any event, the basic idea is that it has some recurrence that looks like the following, which is T of n equals aT of n/b plus f of n. So for instance, in merge sort, for a tiny bit of review, remember that we kind of split into two pieces. So both a and b in merge sort would be 2. Yeah.
And the amount of work that I do in the merge step is kind of like n. So that's how to fill in these different constants there. And the question is, asymptotically, what does this function look like? And of course, it's not totally obvious from this formula because I've defined T in terms of itself, right? And that's the sort of annoying part of solving recurrences. And so master theorem, and I'll let you guys review your section notes about precisely how this gets proved, essentially divides into three cases. And the relevant numbers here are as follows, right? There's an a, a b, and an f. And so the a is kind of like a branching factor, right? So if you remember your computation tree, like in merge sort, you split into two pieces, right? So that number is kind of like a. It's the number of splits that you make, right? b is the amount that your problem size reduces when you go to each of those leaves. And then finally, f is the amount of work
that you do at each node. Yeah, so hopefully that basic language makes sense. And then essentially what you learn from master theorem is that there's three cases. The first is that f of n looks like-- by the way, in the recitation notes, for some reason, it's kind of written backward. They give the answer to master theorem and then the condition. I'm going to write in the other order. Right. Looks like the following, which is n to the log base b of a minus epsilon for some positive epsilon.
I'm going to try to be very conscientious about reading my writing out loud as I write it down. All right. And in that case, what we find is notice there's something kind of magic here, which is that this is just an upper bound. There's not a theta here. So it's OK if f is below this thing. But in any event, the conclusion then is that T of n is big theta of n to the log base b of a. It's kind of cool if you think about it. You only have an upper bound for f,
but you get a nice theta bound for T, which is pretty cool. The reason for that is essentially the f is sort of insignificant relative to the work of just traversing up and down the tree. That's case one. Case two-- I'm going to try and leave this on the board as we actually solve these problems-- is the following, which is f of n is big theta of n to the log base b of a multiplied by log to the k-th power of n. This is a super weird form, but for example, it's
perfectly kosher to take k equals 0, in which case it starts to look like case one, right? For some positive k or non-negative k, rather, in which case what we learn is that T of n is theta of n to the log base b of a times log to the k plus 1 of n. You can see why we don't love applying this theorem in this course because I feel just staring at these formulas is totally unenlightening. But it is a giant sledgehammer for solving recurrences quickly. And then finally, case three--
I'm going to slide the board up, and I'm going to slide it back down because I don't want to bend over. OK-- is the following, which is f of n is omega, right, meaning that it's lower bounded by n to the log base b of a plus epsilon for some epsilon greater than 0. And we needed a second case.
What's it called? af of n over b is less than cfn for some c between 0 and 1. And what's the conclusion there? Then it turns out that the f term sort of dominates, and what we get is that T of n is theta of f of n.
OK. So essentially, this covers three major cases that you see in recurrences. There are other more generic versions of the master theorem out there where essentially, rather than just having one term with the T in it, maybe use more than one term with a T in it. I don't think those are covered in this class. I always confuse the name of the more general one. I want to say it's Arzela-Ascoli, but I know that that's from functional analysis. [INAUDIBLE] theorem, if you want to Google that, learn more. But in any event, that's going to be enough for most of the recurrences we care about in 006. So I've at least written our conditions down. And now they're going to sit on the left-hand side while we do three example problems to show how they show up in practice. Are there any questions about what this theorem is telling us about life or how to apply it?
Don't all speak at once now. Yes. AUDIENCE: Sorry, I have a handwriting question. PROFESSOR: No problem. AUDIENCE: What's after "some," the last line? PROFESSOR: Oh, for some c in 0, 1, not inclusive. Fabulous question. Any others? OK. So let's do an example problem here. So let's do part a. So in part a, they give you a recurrence, which isn't all that different from merge sort or any of the other ones. It looks something like this. T of n is equal to 2T of n/2 plus-- and then they have some other term, which they don't tell you anything about beyond that it's big O of square root of n. By the way, this might mean that it actually does order 1 work, right? It just has to be upper bounded by a square root of n.
That's the only thing that problem's telling you. I'm going to keep kind of driving home that point because I think everybody, including myself, gets really sloppy about, like, when is it O? When is it theta? When's it omega? And so every single time you write one of those letters down, you should step back 50 feet and look at it and think, like, did I do that right? Or did I just write a Greek letter, right? And make sure that you're actually thinking through it logically. OK. So when I apply master theorem in my everyday life, what I like to do is to say, OK, somehow, the really key quantity in master theorem is this dude, right, n to the log base b of a. He just keeps showing up in all these different cases. So I might as well figure what that is for my recurrence, yeah, and then plug it in and check which case I'm in. Does that make sense? So let's do that. So first of all, what is a for this particular recurrence? AUDIENCE: 2. PROFESSOR: 2.
What's b? You're killing me, guys. On 3. 1, 2, 3. AUDIENCE: 2. PROFESSOR: OK. The enthusiasm is overwhelming. OK, and what do we know about the function f? AUDIENCE: [INAUDIBLE] PROFESSOR: Yeah, it's big O of root n. We don't know that it equals root n. But we do know that it's at most upper bounded by something that kind of looks like root n. Eric gave me a whole lesson on this chalk, and I'm still failing at it. OK. So now that gives us everything we need to compute n to the log base b of a. We're going to do this one real slowly, and then we're going to do the next one a little more fastly, quickly. So n to the log base b of a. Well, that's n to the log base 2 of 2. Anybody have an idea? What's the log base 2 of 2? AUDIENCE: 1. PROFESSOR: 1. OK, I'll take it this time. OK. Right. And so then what does master theorem tell you?
Well, in some sense, I want to know what f is, which in this case looks like square root of n, compared to what this is, n to the first. Yeah? Now, first of all, which of these two things grows faster? This guy, right? So what we know is that f is really upper bounded by this n here. "Upper bounded" is not quite the right term because big O allows for some wiggle room, but asymptotically. Hopefully you guys get this concept. OK, so in particular, let's see. So remember that f of--
let's maybe just write it again. So f of n is big O of square root of n. But let's write that in a suggestive way as n to the 1/2. Yeah. In particular, this is equal to big O of n to the 1 minus 1/2. It's the beautiful thing about 1/2. Yeah. But what is n to the 1? Well, that's n to the log base b of a, right? That's what we just showed here, right? So this is really O of n to the log b of a-- that's a complicated way of writing the number 1, right-- minus 1/2.
Let's give 1/2 a special name, too, while we're at it. Let's call him epsilon or her, yeah, where we take epsilon equals-- see what I did there? Now, what does it tell me? There are three different cases of master theorem. And take a look at what I just showed. I showed that f of n is equal to big O of n to the log base b of a minus epsilon for some epsilon that's equal to 1/2. The beautiful thing about 1/2 is that it is greater than 0. Yeah. So I think we have somewhat laboriously checked
that we are in case one. Any dissidents here? Fabulous. OK. So in that case, master theorem, basically, you're just done, right? So what is the conclusion? AUDIENCE: T of n is theta of n. PROFESSOR: Exactly. So T of n is theta of n to the log base b of a. But we already showed that's equal to n. So we're done. All right. Any questions about how we apply master theorem here? OK. So now here's the thing about master theorem. Did we learn anything about what this recursive function is doing?
No, we just did a bunch of work, plugged in some Greek symbols, and out came a big theta. Yeah. So what we might want to do instead is to use a method that I'm still learning myself because I'm not used to presenting it this way, but that's OK, which is to actually draw out the tree of computation. Yeah. So let's actually do that. So let's say that I call my function T of n. So what does T of n do? Well, in some sense, it does work that kind of looks like the square root of n, right, and then it makes two function calls, each of which has n/2 amount of data. Yeah. So let's draw what this looks like. So the first thing that my function might do is work square root of n much. Going to put a little square root of n there. And now it makes two function calls. And how much work does each of these guys do?
So when it calls T, what goes into T? n/2. AUDIENCE: So square root of n/2. PROFESSOR: Exactly. So it does square root of n/2 amount of work at each of these two nodes. That make sense? AUDIENCE: That's determined by the branching number, right? PROFESSOR: You have to be careful. So the branching number, in this case, it happened to be the same. The next problem we do, they'll be different. So they're both 2 here. The outside 2 refers to the fact that there's two children. AUDIENCE: Yeah, never mind. PROFESSOR: And inside 2 refers to the fact that I divided by 2. It's a fabulous question because it's exactly what I got wrong when I did this problem. OK.
So now each of these guys calls one of their children. Yeah. And so again, the data divides by 2. So now I have the square root of n/4, like this. Yeah. If you're wondering, inside of each of these circles, it says square root of n/4. OK.
So let's say that-- so now meanwhile, in our function call tree here, how many nodes are in just the first level at the very top? One, right? Just this dude. Yeah. One node. How many nodes are in the second level? Two. Or if we want, 2 to the 1 nodes. Here, there's four, yeah, which is 2 squared nodes, and so on. OK. So now we have some pictorial representation of what's going on inside of our recurrence. If you look at the course notes I wrote out, I wrote out one more layer just for fun and profit. So now let's see how this helps us actually solve our recurrence. So what do we know? How many levels are in this tree?
Well, our algorithm kind of stops when the input to T looks like 1, right? So since this divides by 2 each time, the tree has log base 2 of n levels in the tree. Yeah. And each level does how much work? So be careful. So each level-- let's call that level l, right? So we'll say l equals 0, l equals 1, and so on. So there's two different things we have to account for when we account for the work in this level. One is the square root of n/2 in a single node. And the other is the fact that there's two different nodes in the level. So in level l, so the amount of work in level l is equal to the product of those two things. So how many nodes are in level l? 2 to the l, yeah? OK.
And how much work does each one of those nodes do? It's like n/2 to the l. So a different way of writing that is the square root of n times 2 to the minus l, like that. This is just n/2 to the l. Fabulous. So now we have everything we need to actually work out our solution to the recurrence. OK. Yes? AUDIENCE: What does that say? PROFESSOR: This is n times 2 to the minus l. I appreciate the [INAUDIBLE]. I'm going to work on this more. And that's why I also share it online.
The handwritten notes is-- this is literally a scan of the page that I'm holding in front of me. OK. So if we want the total amount of work in our tree, right, if we want to account for all of T of n, then what are we going to do? Well, we have to sum starting at level 0. I'm using l instead of i because I'm an analysis person, and i is the square root of minus 1. And I don't like i's in my algorithms. But in any event, how many total levels are there in my tree? Well, we know that there's log base 2 of n, right? And now we have to sum this quantity for all those levels. 2 to the l, the square root of n times 2 to the minus l, like that. OK. And then what do we know secretly from the master theorem is that we're suspecting that this is going to be theta of n, right? So that's all we've got to check. OK, so let's do that real fast. So first of all, notice that this n term doesn't depend on the summand. So I can just pull them out, right?
So this is really the square root of n times the sum over l equals 0 to the log 2 of n of what? Oh, and there's a mistake in my notes. Oh, man. Oh, no, this is-- AUDIENCE: [INAUDIBLE] PROFESSOR: Oh, right, duh. OK. So you have 2 to the l, and you have a square root of 2 to the minus l. So this is really 2 to the minus l/2. So you have l minus l/2. And this quantity is really 2 to the l/2, like that. Does that make sense? So this is just properties of exponents from high school class. And in fact, I don't like this because it's l/2, and I want to blindly apply a formula without thinking about it. And so of course, this is really the same
as the square root of 2 to the l-th power. This is just properties of exponents. OK. So what is this sum called? Do you recognize this? So there's some constant here. You're taking it to the l-th power, and then you're summing it over l's. This is called a geometric series, right? So it would be like 1 plus x plus x squared plus x to the third and dot dot dot, x to the, I guess in this case, log base 2 of n. Yeah? So conveniently, there's a geometrics series formula out there.
I wrote it down in the handwritten notes, but maybe for now, because I'm moving slowly as always, so we'll skip that part. And what you could is as follows, that this is the square root of n, right-- that's just this outer part-- times-- and the inside are going to get the square root of 2 to the log base 2 of n plus 1 minus 1 divided by the square root of 2 minus 1. And this is the geometric series formula. So I encourage you guys to go back home and Google that one if you forgot. Yes.
AUDIENCE: Wait, why are we calculating the sum of the series? PROFESSOR: Because what we're trying to do is figure out the total amount of work in this tree, right? So what we've done so far, we know that a single level in this tree is this value. And now we have to sum over all the levels from 0 to log base 2 of n. AUDIENCE: OK, and what do we use this number for? PROFESSOR: Because we're trying to actually approximate or at least bound T of n, right? And T of n is the sum of all these values. AUDIENCE: Oh, OK. And theta of n is not a sufficient bound. PROFESSOR: It is a sufficient bound. We just got it from kind of a boring way. This is a second way that we could have proved the same formula. AUDIENCE: Oh, OK. PROFESSOR: Yeah. I see a question over here. AUDIENCE: Can you go over the root n, 2 minus l part? I understand the 2 to the l is the number of nodes second part. Does that work? PROFESSOR: Oh. OK, this is the number of nodes. This is the quantity inside of the circle.
So notice that it's square root of n and then square root of n/2 and then square root of n/4 and then square root of n/8, right? So 2 to the minus l here is like 1/2 and then 1/4 and then 1/8. Yeah. Fabulous. Any other questions? Yes. AUDIENCE: So if we have log base 2n levels and we started indexing at 0, doesn't it go from l equals 0 to l equals 2n minus 1? PROFESSOR: Oh, boy. I'm bad at this stuff. No, right? I think this is correct. Let's see. So let's say that I have n equals 2, right? Then what's going to happen?
Yeah, no, this is correct. So as a sanity check, think about n equals 2. So how many levels should it be? It should go from l equals 0 and then l equals 1. Then I'm done because I have T. And then I think-- yeah, so the log base 2 of 2 is 1. So yeah, this formula checks out. OK, any other questions? These are all great questions. You guys are keeping me honest. OK. So we have this giant ugly expression here. And so our final job here is just to simplify it. That's it.
And then what we're going to find is that this is secretly just theta of n. A little surprising, given that this is kind of ugly. OK. And by the way, this was just the constants. So this term on the denominator is not going to end up mattering for our asymptotic calculation. OK. Cool. So let's see here. So remember that the square root of 2 is 2 to the 1/2. Yeah. So I can do a little bit of reshuffling on our formula. And what I'm going to find is that this is exactly the square root of n times 1 over the square root of 2 minus 1. That's just this constant here. Now I just have to cope with this funny term inside of here. I'm going to do the following. I'm going to say, well, square root of 2 is the same as 2 to the 1/2.
So I'm going to write this by moving that 1/2 upstairs here, right? So what am I going to get at the end of the day is that this is the same as 2 to the log base 2 of n plus 1 and all of that-- oops, plus 1/2, right, because there's the 1/2 exponent that got multiplied by the 1-- minus 1. So this is just an identical expression to this one. All I've done is move the 1/2 upstairs.
And finally, now there's some order in the universe, because what is 2 to the log base 2 of n? AUDIENCE: n. PROFESSOR: n, exactly. That's a very complicated way of writing n. So this is equal to the square root of n times 1 over the square root of 2 minus 1, which is just a constant. I see your hand. I'm going to write, and then I'll catch up with you. And now this whole quantity is n times the square root of 2, right? Oops, wait.
AUDIENCE: [INAUDIBLE] PROFESSOR: Oh, I'm sorry. That should be-- right, because we moved the 1/2 into the exponent, and I didn't account for it. So this is the square root of n multiplied by the square root of 2. AUDIENCE: [INAUDIBLE] PROFESSOR: What's that? AUDIENCE: It'd be n/2 to the square root of n. PROFESSOR: n/2 to the square root of n. No, I think this is right. AUDIENCE: Oh, sorry. PROFESSOR: Yeah. You guys are making me nervous. OK, so finally, now we can start to see the big O come out, right? Because now we have the square root of n times itself plus some other stuff, which is obviously going to grow slower than the remaining terms here. So we're good. If you're wondering, the official solutions are actually incorrect. They, too, get this a little bit wrong. I'm going to fix that tonight, and then we'll post them for you guys. And this unfortunately is the kind of thing
you do a lot of, where you're going to take your tree. You write down this giant tree formula here. You write down a geometric series of some sort and then start bounding terms until you get to the expression you want. So now you can see why master theorem maybe is kind of valuable because it saves you a lot of headache. Yes. AUDIENCE: When we write up our piece sets and we have to write up trees like this, can we hand draw them and take pictures? PROFESSOR: Yeah, I don't see why not. They don't have to draw it in Dixie or something. AUDIENCE: You can take pictures of diagrams as long as the math doesn't [INAUDIBLE].. PROFESSOR: Yeah. I think if you're just writing out your math, taking a photo of it, and then, like, backslash include graphics of your photo-- yeah. But just for the figure, I think that's fine. AUDIENCE: [INAUDIBLE] PROFESSOR: Yeah. In fact, when I write research papers, that's how I do it until the final draft. Yeah. OK. So essentially, what did we just do
in this problem, which is part a of one problem, is essentially just showed two different ways of solving recurrence, right? One is the sledgehammer that's very efficient but not terribly illustrative. The other is really working out all of the work that happens at every level of our tree, doing a giant sum, and then just actually deriving the proper formula. And both of these are just different ways to skin the same cat. Is that a weird phrase? I don't know. OK. So right, so actually, part two, I believe, ends up being easier by a funny fluke even though it looks like it's harder. So let's do that next here. So I'm going to leave master theorem on the left-hand side. I really want to say "master's theorem," but I guess that's not right. I'm going to leave the master theorem on the left-hand side.
Just do the problem over here. I'm going to do this one a little bit quicker because I prefer not to spend the whole session on one problem. OK. But I do think it's worth spending a few minutes going over this theorem and how to apply it carefully because, well, otherwise, you won't. OK. All right, folks. It's a workout up here.
So in part b, we have a version where the branching factor and the amount that the work reduces are not the same. So now we have to be a little bit more careful in the way that we apply master theorem, right? So now in part b, we have that T of n here is equal to 8T of n/4 plus-- and now in the problem, they write big O of n times the square root of n. We're all grown-ups here. That's the same as big O of n to the 3/2.
Yeah. That's just arithmetic. OK. So first, let's apply the master theorem. Let's see. So what is n to the log base b of a, in this case? Well, what is b? 4. What is a? 8. Anybody know log base 4 of 8? AUDIENCE: 3/2. PROFESSOR: Wow, you're good. Yeah, so this is equal to n to the 3/2. And notice that these two things agree now. Yeah. So which of the three cases of the master theorem are we in? Are we in the first one, where one of these kind of dwarfs the other? No. We're in case two, right, where they both kind of behave similarly. Yeah. So this is case two.
And what is the value of k for case two that's relevant here? Do I need a log factor? No, these two terms are just the same, right? They're both n to the 3/2. So this is case two with k equals 0. And in particular, immediately, we get that T of n is-- let's see. We're in case two, so we have theta of n to the log base b of a times log to the k plus 1 of n. So you have to account for that term, so it's like that. That make sense, how we applied master theorem in that case? Yes.
AUDIENCE: So how can we apply case two if it's big O and not big theta? PROFESSOR: How can we apply case two if f of n is big O and not big theta? Well, that's coming from a grad student. So why don't I ping it back to the students in the class rather than answering myself? AUDIENCE: Can you repeat the question? PROFESSOR: Sure. So let's say that we just have a big O. We don't have a big theta here. Or at least a simple answer, which maybe is the one that you're looking for. So what do we know? In that case, we know that f of n might grow more slowly than this function. But it doesn't grow more quickly than this thing. Yeah. AUDIENCE: Would T of n just be big O instead of big theta? PROFESSOR: That's exactly right. So you can just replace big theta with big O, and that's perfectly fine. So I don't know if that's the answer you're looking for. But at least you get a loose bound this way.
Yeah. AUDIENCE: You should probably write it big O in the answer then. PROFESSOR: Oops. Oh, I'm sorry. I see. We're being pedantic. OK. You can just tell me if I made a mistake. It happens. I think it's correct in my notes. It is. OK. So the reason our colleague brings it up in the back is that I made a slight mistake, which is that this is a big O, not a big theta, right? In which case, the only thing I can draw down here is that this is also a big O. So I made exactly the mistake I told you guys to avoid before. Yes. AUDIENCE: So then over there, would those also be big O? PROFESSOR: No. Yeah, now I've managed to confuse you. This is correct. This entire statement of the master theorem, assuming I copied it correctly, is right. But there's a bit of a difference. Remember, we wanted to apply case two. And look at case two. So there's a theta here. But in the problem, in the homework problem, they were sneaky.
And they only said that f of T is big O of something. So remember, what's the difference between big O and big theta? Well, intuitively, big theta says that my function really does look like this guy. Somehow, it's bounded above and below as I go far enough out. In big O, there's just a bound above, right? So this is somehow looser. And so the way to apply master theorem in this case is say, well, at least f of n is upper bounded. It looks like this. So the best that I can do is to replace this guy also with an upper bound. Yeah. That's a great question. AUDIENCE: So the reason you chose two is that [INAUDIBLE] construction of the math is similar to [INAUDIBLE]? PROFESSOR: Exactly. You got to kind of stand back and squint at it a little bit, make sure that it fits. But certainly, it's the case that the other two parts of this theorem don't apply. So we might as well try to squeeze this part into the right form. All right.
So let's do the tree version of this. And then I think what we're going to do is skip part three for now of this problem because it's mostly a fun kind of vectorial problem rather than something that actually is going to help your understanding of algorithms. And then if we have time at the end, we'll come back to it. OK. All right. So that was the easy way to solve the problem. Now let's do the painful one where we draw the tree. I shouldn't say that because I think it's something we want to encourage-- the more enlightening version, yeah? So now at the top of my tree, I do n to the 3/2 work. And now how many children does this guy have? AUDIENCE: [INAUDIBLE] PROFESSOR: Careful. You have 1 in 2 chances, either 8 or 4. 8, right? So the outer coefficient is the number of children because it's like the number of function calls you make, right? That's the way to think about it, right? So there's eight of these guys.
OK. And each one of these guys-- I'm only going to do one level of these. Wait, that should not be connected-- does how much work? Well, remember that it's divided by 4 here, right? So it looks like n/4 to the 3/2. That make sense, how we got this picture from our recurrence? Let me switch to another piece of chalk. OK. Right. So in other words, if I look at level l of my tree, assuming we're going to index from 0 at the top, how many nodes are in level l? Remember, it branches by 8 each time. So it has 8 to the l nodes. Yeah. And how much work does each do?
Well, in each level, I divide by 4, right? So this is going to look like n times 1/4 to the l to the 3/2. Right. Where am I? There we are. So that's n times 4 to the minus l. That's fancy notation for 1/4 to the l to the 3/2. Notice that this is exactly the same pattern as the previous problem. I'm just plugging in some different constants. Yes. AUDIENCE: I have a question about the diagram. If you have a really large a and you're trying to do the recursion tree method, is this type of work OK with where you have-- PROFESSOR: If I were grading your papers, it would be.
But you should probably check with your TAs on Piazza on that one. AUDIENCE: All right. PROFESSOR: But I've learned my license. I'm not sure you guys have yet. And also, I am not sure I have, either. All right, folks. So now we need to do our total amount of work here, right? So how many levels are there in our tree here? Well, the amount of data divides by 4 each time. And when the amount of data is 1, then I'm done, right? So the total number of levels is equal to the log base 4 of n.
The basic thing to get right in this problem is just where there should be 4's and where there should be 8's. OK. So now what am I going to do? Well, again, as our colleague in the back points out, we only have big O. So we can only upper bound our work. But our work in the tree is less than or equal to-- really, there's a proportion here because this is just up to a multiple. I guess that's why, in the notes, they put a little c in front, which I can do. Let's do c like that.
Well, we're going to have the sum from l equals 0 to log base 4 of n of exactly the quantity, this guy. Multiply it by the number of nodes in the row. Yes. AUDIENCE: Is the reason why you made this work an inequality because of the-- PROFESSOR: Because of the big O. That's exactly right. Yeah. OK. All right. So what do I get here? Well, let's write it out the big ugly way first. So this is n times 4 to the minus l. This is to the 3/2. And then this is multiplied by 8 to the l.
But whoever designed this problem was really sneaky, right? So what's 4 to the minus l to the 3/2? Any guesses? AUDIENCE: 8 to the minus l. PROFESSOR: Yeah, this is exactly 8 to the minus l if we work through all of your exponent arithmetic. Yeah. Why does that matter? Well, it exactly cancels this 8 to the l term here. This is actually exactly what case two in master theorem is trying to capture. So this whole thing is equal to, really, just n to the 3/2 from l equals 0 to the log base 4 of n. All these constants go away. Isn't that beautiful? Yes.
AUDIENCE: Quick question about the tree again. PROFESSOR: No problem. Uh-huh. AUDIENCE: So do people ever use these for optimization in terms of trying to write a better algorithm, trying to minimize the work per level? Yeah, I'm just curious if that's an application that's-- PROFESSOR: Sure, I guess so. I mean, this is somehow a visualization of how your algorithm is making function costs, right? So every node here kind of looks like a call to your piece of code. So every time you make a new one, it's like making a call. And then the divide here is kind of like how much data goes into that function call. So I think this is a useful way to kind of visualize what's going on inside of your code. And then if you're trying to optimize your code, you're trying to basically reduce the number of nodes and/or the amount of work that each node does, right? Yeah. So this is just a nice way to kind of visualize what's going on in a recursive algorithm. Yeah.
Great question. All right. So here's a really nice thing. Does n to the 3/2 depend on l? No, right? So this whole thing is equal to c times n to the 3/2. And my colleague in the back is going to catch me if I don't make the off by 1 error here. So this is log base 4 of n plus 1 because my sum started at 0. Yeah. And notice that this is exactly big O of n to the 3/2 log n, which agrees with what we did in a much more easy case with the master theorem. Does that make sense? Yeah.
AUDIENCE: So if I'm doing the exact bound, can we say that it's theta of n to the [INAUDIBLE]?? PROFESSOR: Oh, great question. This is where this notation is going to be misleading. This expression in a vacuum is theta of this value. But the reason that I wrote big O is because I only have an inequality all the way up here. So if I'm worried about bounding this work, there's no reason to write a theta there because it's just telling me some kind of intermediate piece of information. Yeah. OK, fabulous. So yes. AUDIENCE: Does the base of the log matter? PROFESSOR: Does the base of the log matter? Ah. This is a nice formula here. So remember that log base b of a is the same as log a over log b, if I got that right. Yeah. So the base of the log when it comes to big O doesn't matter because it's a constant factor. Yeah. OK. So that's part two of this problem. Part three is a little bit annoying because it has two branches in it. I wrote out a careful solution.
It does not apply the master theorem because it's irrelevant, at least the version that we know in this class. So we're going to kind of skip that for now because I don't think it's terribly relevant to most of the algorithms that we'll see in 6.006. But we'll come back to it if we have time. Yes. AUDIENCE: Can we make that a lower bound and an upper bound using master theorem, knowing that T of n/4 is smaller than T of n/3? PROFESSOR: I'm going to refer you to the solutions rather than talking about that problem right now. Yeah. OK, cool. All right. So that's how you apply master theorem, which is mildly painful. The good news is the rest of this problem set, actually, I consider to be much easier than the first one, which is why I think it's OK to spend a little bit extra time here because I think this stuff is confusing to get right. Mostly for me, and hopefully I've conveyed my confusion to the rest of you. OK. So let's erase this.
And while we're doing that, why don't you guys all give problem number two a read? So one of the big skills that we need to cover in 6.83-- not 6.837, 6.006 and 6.837, but I'm not quite as evil about it in that class-- is as follows, which is you read a problem. And for some reason, your instructors have some sick sense of humor. And they encode it in this totally weird, goofy language which somehow, to a theoretician, makes your problem feel more practical.
So in any event, when you read all of this paragraph, the very first skill that you have to do is to figure out, OK, like, this is cute notation, and it's about Infinity Stones. And if I watch Star Wars or whatever, I would know what that meant. But in any event, what really matters is understanding, OK, but algorithmically, what are they asking? Yeah. So I'm going to try and talk about this problem as I erase the board. So I believe, what, like, Mickey Mouse or whatever has a bunch, has some planet that he's looking for. I'm going to open the problem now. Right, she's a super villain on a quest. She's looking for a stone on a planet, and the planet has some index k. And unfortunately for us, the number of planets is quite large. It's infinity, in fact, because it's the Infinity Stone. Or, sorry, did I say that? I think it's the Infinity Phone or something. I forget. But in any event, the only thing that you can do when you land on a planet is ask an oracle,
is the index of my planet bigger or smaller than the index of the planet that I'm standing on, right? And then the question is in log k time where k is the index of the planet. Notice that's already a little weird because it's not the size of your data, quite. Can you find the planet? Now, what does this kind of problem-- like, what is it screaming out for you to use? You see a log k. You're looking for something. AUDIENCE: Bisection. PROFESSOR: Bisection, right? AUDIENCE: Binary search. PROFESSOR: Or binary search. That's absolutely right. Those are both great answers. But there's a bit of a problem, which is that the number of planets is unbounded. We don't know how many planets there are in this little universe that problem 1.2 sets up. Yeah. So our intuition is that we want to use binary search. But in order to do binary search, I need to have a left- and a right-hand side and divide, and I have no right-hand side. So what can I do?

Ask anything
We've read this whole study set, so questions about it get the best answers.