Algorithms Problem-Solving
Transcript
JASON KU: I'm Jason. Hopefully you guys saw me on Tuesday. This is our first ever 6.006 problem session that we'll be having on Fridays this term. It's really an experiment. We've never done this before. But one of the things that we were discussed while preparing for this class is that we have two different methods of instruction, formally, usually in this class-- a lecture, which is there to present you with the fundamental material, the data structures,
and the algorithms that are the base, the foundation of what-- how you will be approaching problems in this class; and then the problem sets that you will work on, our applications of that material. But there's usually a much different feel between those problems that we'll give you then the underlying foundational material. So the application of that material will feel very different. And a lot of times, there are tricks to approaching the problems or ways of approaching
the problems that you kind of just have to figure out by working on the problem, sometimes going to office hours. But what we want to do this term was to-- since we had the opportunity to be recorded by OCW, was to record us going through some problems that we've had on problem sets in the past so you could see how we would approach working on these problems that you'll be working on, at least in a similar vein. So that's the goal of these problem sessions.
In the past, for OCW, we have recorded a recitation, but we felt that that was a little less useful to you guys, because recitation is meant for interaction, questions-- one-on-one questions. We want to be a safe space for you guys to interact with the material with-- in a smaller environment that might not be recorded. So that's the goal of this-- these sessions that we will be doing on Fridays. Any questions about what we're going to be doing today? OK.
So we do have a handout up here by the door, which we may or may not have in the future. This is all an experiment, so you'll have to work with us as we figure this stuff out. They were posted on LMOD about an hour before this session. We'll try to keep that as a standard. But it just shows you the questions we'll work on today in the session. It isn't just the problem set from last term. It's a selection of problems from previous terms, and some of them have been edited to be maybe a little shorter and things like that. So what we're going to do is just go through the problems one by one. I'll try to just show you how I'm approaching the problems, but at any point, if you want to, you can ask questions. That's fine. OK? All right, so the first question we have is-- has a setup that's very similar to what you will have on your Pset1.
It's essentially saying that it has usually many parts. This has two parts, an A part and a B part. I've omitted a B and a C that that was on last term's problem set. And it has five functions each, and you're trying to order them increasing as-- based on their asymptotic behavior. So here are the functions that we have. Maybe I'll stick it up instead.
All right, so we have a few sets of functions, and we just want to order them. And some of the functions may be asymptotically equivalent-- in which case, when we are ordering these things, we're going to put those numbers in a set. So what we have as an example are three functions-- n, root n, and n plus root n. This is one, two, three. And what we're going to ask you to do is order those functions based on their asymptotic complexity. So hopefully you guys can get this one.
Which one's the slowest growth in terms-- root n, so number two. So if we say f of 2 will be our first one. And then, how about the other two? STUDENT: [INAUDIBLE] JASON KU: They're the same. They're both order n. So we would put in set brackets f1 and f3. And on your problem set, if you put just 2, and 1, and 3 here, that would probably be fine. But if you were to put 2 over here or not have these curly braces around here, those would not be correct, and you'd get points marked off.
Does that make sense? OK, so we're going to approach the first set of problems-- first set of functions, which is a little different than the second set of functions. Hopefully this one's a little easier. One of the common approaches that I have in going through these things-- some of these are in a form that is hard for me to tell how they would compare to other things. Actually, most of these are fine, but in general-- can anyone, just by eyeballing, tell me an order that works for them?
Yeah? STUDENT: OK-- JASON KU: This is a little difficult to do on the spot with five functions. STUDENT: Yeah. A little iffy about f1. JASON KU: OK. STUDENT: But f5 is definitely smaller than f2, which is-- smaller than f2 which is smaller than f4. And f1 in iffy. JASON KU: OK, great. That's excellent. So what we've got here is, on f2, f3, and f5, you kind of have this n leading term. If we factor an n out of that, then we're comparing log with-- basically, we look over here at the polynomial function. This one's smallest out of them, and then the log factor is smaller than a polynomial factor. Log grows slower than linear. And so this guy is smaller than f2 is smaller than f3. That's great-- that your colleague said.
Hopefully proved in recitation today-- no, Wednesday-- this nice fact, I guess, that a is less than, asymptotically, this polynomial-- this log n to any power is asymptotically less than any polynomial for any positive a and b. And in particular, there's actually a stronger thing you can say, which is little o. Did you guys talk about little o in recitation at all? Probably not. It's kind of the same as big O, except is big O minus theta. So things that are asymptotically equivalent
are not going to be included in this set. So actually, these things are strictly asymptotic-- grow strictly asymptotically slower than any polynomial. Does that make sense? OK. So knowing this identity, or this relation, can we say anything about f1? Maybe someone else-- STUDENT: Any a and b, right? JASON KU: Any a and b-- any positive a and B-- anyone else have a guess? Yeah? STUDENT: That f1 is less than f5-- JASON KU: Yeah, f1 is less than f5, right? Because just using that identity, this-- sorry-- a here that erased, stupidly, is smaller than, say-- this is bigger than n, and n to the b, being 1, is bigger. And then, as your colleague pointed out before, this thing is exponential, so definitely higher than a polynomial.
OK, so that was very easy, right? So the answer here is, if I got-- if I remember correctly, f1, f5, f2, f3, and then f4-- great. So that one was pretty easy. How about b-- or d, I guess? Yeah? STUDENT: Can I just another question-- JASON KU: Sure. STUDENT: [INAUDIBLE] How would you go about proving that? JASON KU: How would you go about proving that? So there is a proof in your recitation handout there. The method in which they proved that in the recitation handout was putting the two--
taking a ratio of the two functions and taking their limit as n goes to infinity. And if the top one-- it grows arbitrarily, then the top one would be-- grow asymptotically faster. And if the it went to 0, the bottom one would grow asymptotically faster. And if it went to some constant, then that would be asymptotically equivalent. Does that make sense? In actuality, to make the limit easier to take, we take the limit of the logarithm of the ratio. It just made it easier. Does that makes sense?
OK, so let's move on to b. b-- we have a polynomial, and an exponential, and then we have these things down here. What do these things in parentheses mean? STUDENT: Choose. JASON KU: Choose, right. It's a binomial coefficient. Does anyone know the binomial coefficient is? Yeah? Hopefully from 6.042 or something like that, or whatever your competition math background is-- but in general, we have this definition. This thing is what? Does anyone remember? What does n choose k mean? Yeah?
STUDENT: The number of ways to choose k objects from n [INAUDIBLE] JASON KU: Yeah, the number of ways to choose k objects from n things-- I never remember this formula. And probably, a lot of you have memorized this formula. I'm not going to ask you to do it. I'm going to tell you how I think about this. What if I want to know the number of permutations of n choose k-- or I mean of n? Sorry. Just how many permutations are there of n items? What is that? That's just n factorial, right?
So what we do here is we'd want to choose some n number of things. I have n factorial different ways of choosing those, but then, essentially, in here and in here, k and n minus k-- I don't really care what their order is. So I'm going to divide out the permutations of this stuff and this stuff. Does that makes sense? So the formula here as I remember it, that hopefully is correct, is n minus k factorial. So I'm getting all of the permutations of the whole thing divided by their constituents. Does that make sense? Did I do that right?
OK, cool-- so that's a nice transformation. So the first step is writing these in terms of factorials. That doesn't really help me any, because I don't know how big factorial is based-- with respect to these other things. Does anyone know how big factorial is? Yeah? STUDENT: You can use Sterling's approximation. JASON KU: You can use Sterling's approximation. So that's nice. Does anyone remember what Sterling's approximation is? No. I don't remember either. I always have to look it up. Sterling's approximation says n factorial is approximately-- and this approximately is much stronger than an asymptotic behavior. It's actually, as these things-- as n approaches infinity, these things are equal. The limit is the identity. But the approximation is the square root of 2 pi n n/3 to the n. OK, that's fun.
So what kind of growth is this? Super bad, right? It's definitely exponential or higher than exponential. It's n to the n-- something like that-- dividing out an e-- this is e, the base of the natural logarithm. And this is a constant, and so is pi. Pi is a constant-- kind of interesting that we have two transcendental numbers here. That's kind of fun mathematics. I'm sure some-- one of your 6.042 instructors could tell you why. I can't right now off the top of my head. But this is an approximation that's very good.
And actually, sometimes other people will think-- this is what people call Sterling's approximation. One weaker notion that sometimes is useful for you is, if you take the logarithm of both sides, this is asymptotically what? If I took the log of this thing-- STUDENT: Polynomial-- JASON KU: It is a polynomial thing. STUDENT: [INAUDIBLE] JASON KU: It's basically n log n. If we take a log of this thing, it would be various things. All right, let's do it out. 2 pi n n/e to the n-- so when we're inside a logarithm, multiplication-- we can split it out-- becomes addition. Division becomes subtraction. And this thing grows faster than all these other things, so we can ignore them when we add them out asymptotically.
And so what we end up getting is this n to the n. The n comes out on the logarithm, and you get something that's theta n log n. Oh, that's fun. This is something we might use later on in the class. OK, but when we are comparing these functions, one of the nice things to do is convert them into something that's familiar to us so that we can compare them easily. So here, this thing is whatever that thing is, roughly square root n n/e to the n. This is, I'm going to say, theta.
That's a little bit more precise. All right, then what about these two things? Let's start with the bottom one. Can someone tell me what this is, asymptotically? Yeah? STUDENT: n cubed-- JASON KU: n cubed-- why is that? Well, if we plug this stuff into that definition here, we have n factorial over 3 factorial n minus 3 factorial. n factorial over n minus 3 factorial just leaves us with an n, an n minus 1, and an n minus 2 over 6. And if you multiply all that out,
the leading term is an n cubed, so this thing is asymptotically n cubed. I skipped some steps, but hopefully you could follow that. And then the last thing to remain is this one right there. That one's a little tricky. Anyone want to help me out here? What we can do is we can stick it into this formula, and then apply Sterling's approximation to replace the factorials. That makes sense? OK, so what I'm going to do is-- let's do this in two steps. This is going to be n factorial over--
what is this? n/2 factorial-- and then what is n minus n over 2? That's also n/2. So this is going to be n/2 factorial squared. Is that OK? Yeah? Now let's replace this stuff with Sterling's approximation and see if we can simplify. So on the top, we have 2 pi n n/e to the n over-- and then we've got a square here, pi n. I cancelled the 2-- n/2 over e to the n/2.
Did I do that right? OK. I can't spell, and a lot of times, I make arithmetic errors, so catch me if I am doing one. OK, so let's simplify this bottom here. I'm not going to rewrite the top. The bottom here-- we square this guy. It's the pi times n. And then this guy, n/2 squared-- that just stays is an n. Then we have n/2 to the n/e--
something like that. n/2 over e to the n-- that makes more-- me happier. OK, so now we have this over this. How do we simplify? Well, we can cancel out one of the root n's. So we've got square root of pi n down here and square root of 2 up top. And then what have we got? We've got n to the n down here and n to the n down-- up there, so those cancel. We got 1 over e to the n, 1 over e to the n up there. Those cancel. What's left in this term, when-- after we cancel?
1 over 2 to the n in the denominator, which is 2 to the n in the numerator-- so this thing is what this thing is right. Asymptote. We can get rid of these constants. And it's root n-- I mean, it's 2 to the n over root n, asymptotically. Does that makes sense, everybody? OK. With that knowledge-- and I'm sorry for my messy board work-- what is the ordering of these functions then? Can someone help me out? Someone else-- Eric, I'm sorry. You can't answer. Come on, guys. You followed what I said.
Start it out for me. STUDENT: I think [INAUDIBLE] f2 and f5 [INAUDIBLE] JASON KU: Right. So both of these things are asymptotically equivalent, so we should put those in brackets-- f2, f5. STUDENT: And then it would go f-- [INAUDIBLE] 3. JASON KU: f3. This one? Why this one? STUDENT: Nevermind. JASON KU: I'm just asking you to justify what you're saying. STUDENT: Well, because I feel like we have the [INAUDIBLE] JASON KU: Uh-huh. STUDENT: [INAUDIBLE] JASON KU: This one's the biggest?
STUDENT: [INAUDIBLE] JASON KU: f4 is the biggest, right? So this one's definitely bigger than this, because it's n to the n, as opposed to 2 to the n. So for any n larger than 2 plus e is fairly obvious that that's bigger. STUDENT: So wouldn't f3 be before f1? JASON KU: Why would f3 be before f1? STUDENT: [INAUDIBLE] 2 to the n divided by [INAUDIBLE] JASON KU: But we're dividing by a polynomial factor, right? So it's going to be slower asymptotically than the first one right there.
So you got it right. What is it? F3, f1, and f4-- OK? Cool-- so it's a little complicated, but just applying some logarithm an exponent rules, understanding that logarithmic factors grow slower than polynomial ones, and again, grow slower than exponential ones. And being able to do some transformations of some of these mathematical quantities to get them in a polynomial-like looking form is how you're going to approach these problems. Does that make sense?
All right, so we're going to move on to question 2 now. Yeah, you've got a question? STUDENT: Yeah, I have a question. What did you say the theta bound was for 4? JASON KU: Theta bound for 4? This guy? STUDENT: [INAUDIBLE] JASON KU: So it's just that. I don't know how to simplify that any further. You've got a polynomial factor here, and then this is an n to the n term divided by an exponential. Yeah? STUDENT: For f3, which one did you-- how did you reduce-- which one is f3 [INAUDIBLE]
JASON KU: f3 took this little cycle here. What we did was we expanded out the definition of the binomial coefficient here. Then we applied Sterling, and then we simplified and got back. Does that make sense? Yeah? STUDENT: [INAUDIBLE] JASON KU: Sure. STUDENT: Is there a reason that f3 is before f1? JASON KU: Why is f3 before f1? If I erase the 2 and the pi, this thing is theta of that-- 2 to the n over a polynomial factor. It's over n to the 1/2. n to the 1/2 grows non-trivially. Right?
And so this is going to decrease the running time of this thing by a polynomial factor. You could think about-- we're multiplying this by n to the minus 1/2 as well. That's another way of thinking about it. Any other questions? OK, so we're going to move on to problem 2, I guess. I need a eraser.
So problem 2 is kind of a funny looking problem. The point of this problem is to get you to think about using some of the things we're going to be using in this class as a black box. What using something as a black box means is that has a kind of a public interface that you're allowed to work with, but I'm not allowed to see what's inside of it. And a lot of times, what we'll do in this class is try to use a black box and just try to use the abstracted outer functions so that we can prove things about it. We can just accept those as true, and then use those to deal with our analysis. So what we're given in this problem is a data structure supporting a sequence interface that you heard about yesterday. What's a sequence interface again?
How does it store items? Anyone remember? Yeah? STUDENT: [INAUDIBLE] JASON KU: Well, it-- OK. STUDENT: [INAUDIBLE] JASON KU: All right. So what your colleague is saying here is we list them in a contiguous array. Does anyone have a problem with that definition? Yeah, up there-- STUDENT: [INAUDIBLE] JASON KU: OK. So one of the important things about this class is abstracting this idea of an interface versus an implementation. And so what this student down here was talking
to me about an array as an underlying implementation, and what the student back there was talking about is a linked list. These are both things that can implement that interface. But in reality, the interface is something abstracted outside of those ideas. We could implement with either of those data structures. So what makes the sequence interface a sequence interface? Yeah? STUDENT: It's in order, or at least it's indexed in a specific way that allows for [INAUDIBLE] JASON KU: So it's about the data that we're storing. We're storing some number of things, and the important thing is that the data structure is maintaining being able to find items in that set by maintaining an order on them. I usually like to call it an extrinsic order on these things. It has nothing to do with what the items are. It has to do with how I put them in order. There's a first thing, there's a 10th thing, there's a last thing. Right? That's what a sequence of items is right.
And so what this data structure is doing, that's the input that is what we have available to us in this problem, is some kind of data structure storing a sequence of things. And it can support these four operations-- an insert first, insert last, delete first, and delete last. And it supports each of those things in constant time. You don't a data structure that does that yet. You will on your problem set 1, and we'll talk about another way to do that today.
But we don't care how it's implemented. We just give you this black box that achieves these things. Yay-- awesome. And so what we're trying to do is, we have this thing, and I want to be able to manipulate the sequence stored inside, but all I have access to are these external operations. So the idea is going to be let's implement algorithms for these-- some higher level operations in terms of these lower level things that are given to us. Does that makes sense?
And this is actually a pretty easy question. Hopefully we'll have slightly more difficult ones for you on-- in a different context on problem set 1. OK, so the first operation we're going to support-- or try to support-- is an operation called swap_ends. And what this is going to do is take the data structure that we gave-- another way you could do this is put this as a method on that data structure, but let's do this separately-- it's going to take that data structure that we gave-- that I gave you, that's storing the sequence, as the only argument. And what we're asking you to do is describe an algorithm to swap the first and the last items.
I it was an array, I could just look at index 0, look at that, look at the last one look at that, and swap them. But I don't have access to what that underlying representation is, so how would I do that using the things that we have available to us? This is a pretty easy question. What have we got? STUDENT: Just a quick question. JASON KU: Yeah. STUDENT: Does the delete method also return whatever it deletes? JASON KU: Yes. Yes, it does. So in general, if you actually take a look at the session notes, it's giving you a nice little reminder-- recall, the delete operations return the deleted item. OK? It says it right there on the thing.
Yeah? STUDENT: Oh, I also had a question. JASON KU: Sure. STUDENT: And this actually [INAUDIBLE] JASON KU: Mm-hmm. STUDENT: It's not related [INAUDIBLE] if they don't specify a space [INAUDIBLE] does that mean [INAUDIBLE] JASON KU: Yeah, so one of the things that Eric talked about yesterday was generally, in this class, if you have-- usually what we'll give you is a running time bound on the things that you asked for. And because allocation of space by our model takes that amount of time, the amount of time-- the amount of space that we are using is going to be asymptotically upper bounded by the time that we're going to use for the algorithm. And so generally, we'll ask you to stay within a time bound, and not ask you to do something separate with space, but there are problems probably at the end of this unit where we might talk about space complexity. But usually, we will be very specific if we want you to think about space. Any other questions?
All right, so how do we implement this swap_ends thing? Yeah. This is a pretty easy one. Yeah? STUDENT: [INAUDIBLE] say first equals [INAUDIBLE] JASON KU: OK, so another thing about this class-- your colleague over here is trying to write code to me, which is great for a computer, and that's great if you're taking 6.009. It's not great if you're talking to your friends or if you're talking to me. I can't parse code in my head and compile it all the time. Sometimes I can, but not all the time,
especially when it gets to be a large program. So I want you to explain in words to me, and we want you to explain in words in your LaTeX submissions what it is the algorithm is doing. So can you start over with your description? STUDENT: Words are hard. JASON KU: Words are hard. I agree with you. STUDENT: This is a computer science class. STUDENT: I would delete the last [INAUDIBLE] and then take that value [INAUDIBLE] JASON KU: OK. So proposal-- we have a sequence of things.
Again, as Eric was doing in lecture yesterday, this isn't representing an array. It's representing a sequence. So this is the front first I guessed and last. And what your colleague was saying was to delete this guy and stick it on the front, maybe by using delete last and insert first. That sounds pretty good. Does that do what swap_ends is doing? Swap_ends-- swap the first and last items in the sequence.
STUDENT: It's probably better if we first store it in some other variable, so that way we can get the-- delete the n [INAUDIBLE] JASON KU: So what your colleague is saying is, well, we've done kind of half of our work. The first one's still over here. That's no good. Does someone have a way to modify this? Yeah? STUDENT: Before modifying, you can [INAUDIBLE] to modify. [INAUDIBLE] about the amount of things we're storing. JASON KU: Yeah. So how much extra space can we use, for example? STUDENT: The easiest way would be to delete the last [INAUDIBLE] the first, and then just, if we have them already kept [INAUDIBLE] but I don't know if I can keep two different variables at the same time. JASON KU: Good question-- so the question-- can I use additional space? And in general, if we don't give you any restrictions on what you can store, then you can go wild. Do whatever you want, outside of this data structure. One of the things you could do is remove first
all of these things, store it and some data structure you like, manipulate it as much as you want, and then insert first all the way back in and rewrite the thing. But that's not going to give us constant time, which is what we're asking for. But if we don't tell you otherwise, feel free to-- probably, you're only allowed to store a constant number of things, since we have constant time, but generally, unless we say, no, you can't use additional space, you can use additional space. OK?
So how would you do that? STUDENT: I would probably erase the last one and the first one, keep them both, then answer the first one, answer everything else, and then answer the last one. JASON KU: That's great. So what your colleague is saying-- we delete both of them, we store them in temporary variables, and then, one at a time, we insert each of them in their corresponding place using the functions that we have available. OK, so if I were to write little pseudocode for this, I might take the first one-- I'd delete first. I'm really abusing notation here. That's OK. You get what I'm saying. Delete the last, and then store them in their respective places. Insert at the front-- which one am I going to insert? What's up?
Speak up, guys. STUDENT: x2-- JASON KU: x2-- yes. Thank you. And insert last, x1-- OK, that's pretty easy. Yeah? STUDENT: In this case, what would [INAUDIBLE] I think this might be relevant. [INAUDIBLE] What would constitute a pseudocode versus [INAUDIBLE] accidentally writing Python syntax [INAUDIBLE] JASON KU: All right, so for this problem, you'll see the solutions posted to this later on. In that one, I wrote up a description of what I was going to do, and then I actually-- because this
was pretty easy, I actually wrote down some Python code to do whatever this thing was. But in general-- and it's actually OK to write Python or pseudocode of this form on your problem sets, or on an exam, or something like that. But if we can't understand what your variables mean, if we can't understand what your pseudocode is doing, then that's not sufficient. So the reason why we ask for words is so that you can communicate those ideas well. STUDENT: Just a follow-up on that-- so can you also have a combination of pseudocode and description? JASON KU: Sure. STUDENT: OK. JASON KU: Yeah-- including both of them can be clarifying for you, potentially. Any other questions?
This is not such an interesting question from an algorithm standpoint. This is a constant size problem kind of. I have this data structure. I do two operations. I need to do something. And this is so easy that I'm really not even going to argue correctness-- I'm not even going to have to argue correctness to you, because we're essentially just doing exactly what we asked for. Most of the time in this class, when you're doing something non-trivial-- especially when you're doing something that
has to recurse in some way-- we do want you to argue correctness. But in this case, for example, the time analysis is very easy. We do for operations. They each take constant time, so this operation takes constant time-- done. Yeah? All right, so how about the second operation? Second operation at least allows us to use a little bit more. So Shift_left D, K-- this is the operation we're supporting now is we are given this sequence, and what we want to do is take
the first K, here and stick it over here at the back so that the-- these K go here. So the K-th item ends up being the last item and the K plus 1th item now becomes the first item. Does that makes sense? OK. Again, this is actually not such an interesting algorithm from an algorithm standpoint, but it's hopefully helpful to talk about from an instructional point of view. OK? So how would I approach this problem? I need this operation to happen in order K time. Yeah? STUDENT: OK.
So first, go and delete-- set a variable x1 to be the d dot delete first element. Then do d dot insert at the last x1, write a for loop, and then do that K times. And that should take 2K steps, which is OK. JASON KU: OK. So what your colleague was saying is that we're just going to delete this guy, stick it on there, do it K times. That sound good? Yeah. One of the things in this class that you have, in terms of implementation-- usually there are two ways-- at least two ways you could do something that takes longer than constant time. You could write a four loop where you could use recursion.
Sometimes approaching a problem would be good one way, rather than another. Why is it that a lot of computer scientists, as opposed to coding engineers, prefer to think about an algorithm recursively? Does anyone know why? At least I do, when I'm explaining it from a theory standpoint. It actually might not be good from an implementation standpoint, because your computer can vectorized for loops and things like-- but that's not something we need to talk about. Why would we want to talk about a recursive algorithm maybe more? STUDENT: It lets you break up the problem into much smaller, more manageable pieces. JASON KU: OK, so recursion lets you break up the problem into small measurable pieces. That's actually true in some context. How I like to think about recursion a lot of times is, if I have a non-constant amount of work that I have to do, usually easy for me-- it's hard for me to hold a non-constant amount of information in my head.
What I want to do is think about a constant amount of information at any given point in time, because that's easier for me to argue on. It's easier for me to think about making arguments, case analysis on these small amount of things. And so one of the things you can do is, if you break it down so that I solve a slightly smaller problem recursively, and then do a constant amount of work and maintain some invariant, then it's very easy to argue things about it.
It's very easy for me to convince myself that this thing's correct. So I'm going to provide a recursive way of solving this problem. Can anyone set up maybe a recursive way of thinking about this problem, instead of putting this inside a for loop, like your colleague was saying? Yeah? STUDENT: [INAUDIBLE] people do is just consider what will happen when k equals 0 you're not [INAUDIBLE] it at all. JASON KU: OK. STUDENT: [INAUDIBLE] what it originally was. And then-- but if that k is greater than 0, you just [INAUDIBLE] and then [INAUDIBLE] k is 1 less [INAUDIBLE] JASON KU: So what your colleague is saying is setting up the-- a very nice thing. She's saying that, if we think about this recursively, we'll think about a base case--
which, your colleague was saying maybe K equals 0. And otherwise, if we're not at 0, what we'll do is we'll start it out, move one of these guys over, and then we have an instance where we want to shift K minus 1 things over. Want to do the same thing, but with K minus 1 things. And so we can just call this thing for a smaller value of K. Does that makes sense?
All right, so let's try to write that out. The first thing I'm going to write out is kind of a break. If I'm at a base case, let's not do anything to this thing. And maybe I also want some bounce checking to make sure that we're in range. OK, so I'm going to say, if our K is less than 1, I don't think we should be doing anything to this array. So let's just not do anything. If K is less than 1 or K is bigger than the length of D minus 1-- so I don't know what to do if you're asking me to shift more than the things I have, so let's not do that.
Yeah-- because if it was length of D, we would just not move anything anyway, because we'd shift the whole thing. So we don't have to do anything. All right. If we're in either of these cases, we're just going to return, because I either shouldn't do anything to the array or I have no idea what you're talking about, if it's negative or something like that. OK, so that's the first thing. Otherwise, what do we do? We shift one thing over and then we make a recursive call. Does that make sense? OK.
So we'll delete the first thing as a temporary variable-- delete first. And then we'll insert last, x. And then we need to do the recursive call. So what's a recursive call look like? Yeah? STUDENT: Shift_left D, K minus 1-- JASON KU: Yeah. So shift_left D, K minus 1-- OK? And then we can return. This thing doesn't need to return anything. It's just doing stuff to the thing. Right? And whenever we get this K, we make a call, that gets down to 0, we will terminate because we will return. We're in this range somewhere between we-- have an input after this line.
We know that K is somewhere between 1 and n minus 1. And what we'll do is, every time through this recursion, we will subtract 1 from K. So this is a nice, well-ordered sequence. We do the correct thing obviously in the base case, and as long as this thing was correct for a smaller value of K, this thing also does the correct thing, because we're shifting over one, as we are asked, and we're letting this do the work of the rest. I don't have to think about that.
I just have to think about this one loop, this one part of the thing that I'm doing. Constant amount of work is done in this section. And how many times do I call a function? STUDENT: [INAUDIBLE] JASON KU: Yeah. I think K minus 1 times, or-- I don't know. I forget. But it's ordered K for sure, right? And we do a constant amount of work per call, ignoring this extra call. Does that make sense? So this thing runs in order K, as desired. OK? Does that make sense? All right.
So now we will move on to question 3. Any questions about question 2? That one's really probably one of the easiest problems we've ever had on a problem set. Sorry to scare you. So problem 3-- OK, so this is a little block of text right here. A dynamic array can support a sequence interface supporting worst case constant time indexing as well as insertion and removal of items at the back of the array in amortized constant time. So this is what we did yesterday in lecture, right? We showed how a dynamic array--
it's fast to do dynamic operations at the end. OK. However, insertion and deletion at the front is not very efficient because, if you tried to do that, you'd have to shift everything over. That makes sense? All right, on the other hand, what we talked about yesterday was linked lists. They can be made to support insertion and deletion at both ends in constant time. OK, so that's a little foreshadowing of something you're going to do on Pset1. But in lecture, we talked about that operation--
that data structure, a singly linked list, being good at dynamic operations at the front of the list, because essentially, we could just remember where the front of the list was and swap things in as needed. That makes sense? So on your problem set, what you're going to do is make end operations good on the linked list as well, as well as supporting another operation. But what's the problem with linked lists, as compared to dynamic arrays? Yeah?
STUDENT: Linked list lookups can take up to linear time. JASON KU: Yeah, linked lists lookups can take linear time, because I have no-- I don't have the benefit of an array, where I can randomly access something in the middle by essentially just doing one arithmetic offset calculation from the front address and be able to find this thing further down in constant time using our model of computation of the random net access machine. In a linked list, these things could
be stored all over the place in memory, and I have to traverse those pointers until I get to the one that I'm looking for. That's a benefit of an array-based data structure versus a linked, pointer-based one. OK, so then we get to the meat of this question. Show that we can have the best of both worlds-- we can have a data structure that supports worst case constant time lookup, just like an array, but amortized constant time dynamic operations from the back and the front of the sequence.
Does that make sense? All right. Is this question or a-- OK. STUDENT: Can you define amortize one more time? JASON KU: Yes. Sorry about that. Can I define amortize one more time? OK, so this is a tough thing to define in general, but not that much. All right, so amortization usually you put in-- at least in this class, we're going to put in terms of a data structure. So you have this thing. It supports some operations, and you're going to do a bunch of operations on that thing.
There's not really a reason to have a data structure, unless you're going to do lots of things to it. Otherwise, you just write a single algorithm to do whatever it is that you want to do. The value of the data structure is that you can do some work up front by making this thing make some of these operations faster. OK? So what amortization means is, OK, if I have, say, a dynamic array, where I'm going to be inserting things at the end, sometimes, when I add something, I'm
going to spend a lot of time to add that thing. I'm going to spend linear time. But what's the point of this data structure in the first place? The point is that I want to be able to potentially add a lot of things to this thing. Does that make sense? Amortization is saying that, even though sometimes this operation will be bad, averaged over many operations, this is going to have a better running time. That's the amortization. So more formally, what that's going to say is, if I have an operation, the definition of it running in amortized some amount of time-- say K time or-- yeah, sure-- that means that, if I do n operations, generally for large N-- if I do that operation n times, the total time it takes me to do all of those operations is not going to be more than n times K. So on average, it's going to take me K time. Now, in O-4-6 you'll get a more formal definition of that and you'll get a lot of ways of analyzing things, like a potential function and--
we're going to use in what we call charging arguments even today. So it's a much broader analysis paradigm than what we're going to talk about. We're only going to talk about it for this material with dynamic arrays, and we'll just kind of-- it's just kind of an introduction to that. But does that make sense? STUDENT: Yes. JASON KU: Amortized as a financial term, if you know from financial term, means over the long term, this is what it is on average. You can think about--
but that's different than running time. That's average running time of an algorithm. It's a much different concept. What is an average running time? Well, that's hard to define, because it's talking about an average over all possible inputs, and then-- OK, so maybe some inputs are more likely than others, and so you've got a distribution on the inputs and you're trying to average the running time of-- this has nothing to do with that. Amortization means that you have a-- usually a data
structure that you're operating on, and you're doing an operation multiple times, and you're getting a benefit because you're doing that operation lots of times. And so when you are instantiating a Python list and you're doing push and pop operations on the back, that's-- or is it append-- STUDENT: [INAUDIBLE] JASON KU: Append and pop? OK. I've been writing JavaScript a little bit recently. But so append and pop-- those operations, while not cheap all the time,
are cheap well enough that, when we analyze an entire algorithm that might do a linear number of appends to this list, all of those appends added together will only take linear time, because I've done a linear number of them. Does that make sense? STUDENT: Yeah. Thank you. JASON KU: OK-- long-winded answer to your question. Sorry about that. Any other questions before we get going? All right. Anyone have any ideas of how we can use the ideas of a dynamic array and make it good for operations

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