Strings - Programming Abstractions - 8, Study notes of Programming Abstractions

Introduction to Computer Science. Lectures of Programming Abstractions on Strings. Prof. Zelenski - Stanford University

Typology: Study notes

2010/2011

Uploaded on 10/02/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ProgrammingAbstractions-Lecture08
Instructor (Julie Zelenski):Good afternoon. You guys are talkative. That sounds like a
good weekend. I heard like some low noise on Assignment 2, and I thought we could
elevate that to a little bit broader level, so if there’s somebody who’s already gotten fairly
far on one or both of the problems on there, and has stumbled across some insight that
perhaps was a little bit painful to get through, but you’d like to provide the benefit to your
fellow classmates, now would be a great time to offer any bit of advice you think that
might make the world a happier place.
Anyone? No one? [Inaudible]. You’re sitting in the wrong seat. You don’t sit there.
Student:When you make an iterator of like a map or [inaudible] like that, capitalize the
word iterator.
Instructor (Julie Zelenski):Okay. So this insight is that the iterator class that is nested
within the map and nested within the set, its name really is capital I iterator, so it’s capital
M map of something you’re putting in there, int, whatever, and then colon colon capital I
iterator.
It’s pretty easy to look at the code and be a little bit – the lower case I and capital I often
in small fonts look about the same, too.
Student:[Inaudible].
Instructor (Julie Zelenski):I’ve no doubt, and that actually is a great bit of insight is that
you think the error would be like “Spell it the right way, doofus,” and C++ never gives
you the error and says here’s what you’d like to do to fix it. It says according to my
internal specs of which I have referenced point Article 72 of the C++ standard and all this
stuff that is just total gobbledygook.
So one of the things you get very good at is letting the compiler direct you to where to
look for the problem, but then ignoring what it told you was the problem because its
analysis of it was often not very helpful. You just get good at kind of looking at the line
yourself to figure it out. Okay?
Student:I keep forgetting to clear the classes or renew like objects and things like that.
Instructor (Julie Zelenski):Yeah. So all those pound includes, right? So C++, very fussy
about that. You start using set. You start using map. You need to make sure you’ve got
that pound include of the set and the map and whatnot, and that none of the code you
write that will use it will make any sense to the compiler until it’s been fully informed
about what map and set look like. So if you don’t do that – And same thing for like
iostream and all these other things you’re using the random library, when you start
making those calls, C++ wants to know what the interfaces are and how they’re set up.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Strings - Programming Abstractions - 8 and more Study notes Programming Abstractions in PDF only on Docsity!

ProgrammingAbstractions-Lecture

Instructor (Julie Zelenski) :Good afternoon. You guys are talkative. That sounds like a good weekend. I heard like some low noise on Assignment 2, and I thought we could elevate that to a little bit broader level, so if there’s somebody who’s already gotten fairly far on one or both of the problems on there, and has stumbled across some insight that perhaps was a little bit painful to get through, but you’d like to provide the benefit to your fellow classmates, now would be a great time to offer any bit of advice you think that might make the world a happier place.

Anyone? No one? [Inaudible]. You’re sitting in the wrong seat. You don’t sit there.

Student: When you make an iterator of like a map or [inaudible] like that, capitalize the word iterator.

Instructor (Julie Zelenski) :Okay. So this insight is that the iterator class that is nested within the map and nested within the set, its name really is capital I iterator, so it’s capital M map of something you’re putting in there, int, whatever, and then colon colon capital I iterator.

It’s pretty easy to look at the code and be a little bit – the lower case I and capital I often in small fonts look about the same, too.

Student: [Inaudible].

Instructor (Julie Zelenski) :I’ve no doubt, and that actually is a great bit of insight is that you think the error would be like “Spell it the right way, doofus,” and C++ never gives you the error and says here’s what you’d like to do to fix it. It says according to my internal specs of which I have referenced point Article 72 of the C++ standard and all this stuff that is just total gobbledygook.

So one of the things you get very good at is letting the compiler direct you to where to look for the problem, but then ignoring what it told you was the problem because its analysis of it was often not very helpful. You just get good at kind of looking at the line yourself to figure it out. Okay?

Student: I keep forgetting to clear the classes or renew like objects and things like that.

Instructor (Julie Zelenski) :Yeah. So all those pound includes, right? So C++, very fussy about that. You start using set. You start using map. You need to make sure you’ve got that pound include of the set and the map and whatnot, and that none of the code you write that will use it will make any sense to the compiler until it’s been fully informed about what map and set look like. So if you don’t do that – And same thing for like iostream and all these other things you’re using the random library, when you start making those calls, C++ wants to know what the interfaces are and how they’re set up.

And the way to tell it about it is to get the right pound includes in there. Without them, it won’t get you very far at all.

Student: I have a question. Why isn’t it possible to just give access to every [inaudible]?

Instructor (Julie Zelenski) :Well, you could certainly do that. We could make one sort of big master include, which is like include the whole world, and just grab everything and bring it on in. And what that will do is it will just slow down all your compiles because it will be looking through that thing again and again every single time. And even though you’re not using it, it will have to have kind of read past it and understood it. And then it turned out to be a wasted effort. In the case of our programs, the amount of headers we’re talking about is small enough that you could imagine doing that without really slowing your development down. But for the large-scale development, you will typically include exactly those headers you need to avoid slowing down all your compiles by rereading a lot of things that aren’t necessary for this unit.

Student: Careful when you concatenate characters to strings.

Instructor (Julie Zelenski) :Yes.

Student: Because it will give you – because if you do it a certain way, it will give you a bunch of junk.

Instructor (Julie Zelenski) :That is a great point. And we talked about that at strings, but it’s a fine time to reiterate it – is that when you’re using [cuts out] plus equal to take a string and extend it with some new characters or another string, one of those operands needs to be a C++ string, which means a variable, a parameter, something that was declared and created as a string.

And the things in double quotes remember are old style strings, and until they have been converted they are still old style. So if you have an expression that looks like this, and you try to add a character there, this compiles but it does not do what you want. It takes the old style string and uses this character kind of as an offset and causes the string basically to turn into garbage. So if you were to say something equals this, you will not get what you want. You’re expecting to get my string extended with that new character. So remember that one or both the arguments needs to be a string. If you ever need to force, you can introduce that typecast around it, and does the promotion. In most situations, the promotion will happen automatically and you won’t have to be involved, but this is one case where the legacy of C++ being derived from C means that the old language did have a meaning for this, and they couldn’t break that old meaning, so they left it in place for you to stumble across when you least expect. So that’s a great thing to always be keeping in mind when you’re doing that concatenation, looking at your arguments, making sure one of them at the very least [cuts out] C++ screen. Way over there.

Student: Why does the set pair operator need to have ordering?

sophisticated. But by the end of the week, I’m hoping that you’re gonna start to see these patterns, and realize that in some sense the recursive solutions tend to be more alike than different. Once you have your head around how to solve one type of problem, you may very well be able to take the exact same technique and solve several other problems that may sound different at first glance, but in the end, the recursive structure looks the same. So I’d say just hold the discomfort a little bit, and wait to see as we keep working which example may be the one that kind of sticks out for you to help you get it through. So we’re gonna start with things that fit in the category of functional recursion. The functional in this case just says that you’re writing functions that return some non-void thing, an integer, a string, some vector of results, or whatever that is, and that’s all it means to be a functional recursion. It’s a recursive function that has a result to it. And because of the recursive nature, it’s gonna say that the outer problem’s result, the answer to the larger problem is gonna be based on making calls, one or more calls to the function itself to get the answer to a smaller problem, and then adding them, multiplying them, comparing them to decide how to formulate the larger answer. All recursive code follows the same decomposition into two cases. Sometimes there’s some subdivisions within there, but two general cases. The first thing – this base case. That’s something that the recursion eventually has to stop. We keep saying take the task and break it down, make it a little smaller, but at some point we really have to stop doing that. We can’t go on infinitely. There has to be some base case, the simplest possible version of the problem that you can directly solve. You don’t need to make any further recursive calls. So the idea is that it kinda bottoms out there, and then allows the recursion to kind of unwind. The recursive cases, and there may be one or more of these, are the cases where it’s not that simple, that the answer isn’t directly solvable, but if you had the answer to a smaller, simpler version, you would be able to assemble that answer you’re looking for using that information from the recursive call. So that’s the kind of structure that they all look like. If I’m at the base case, then do the base case and return it. Otherwise, make some recursive calls, and use that to return a result from this iteration. So let’s first look at something that – the first couple examples that I’m gonna show you actually are gonna be so easy that in some sense they’re almost gonna be a little bit counterproductive because they’re gonna teach you that recursion lets you do things that you already know how to do. And then I’m gonna work my way up to ones that actually get beyond that. But let’s look at first the raise to power. C++ has no built-in exponentiation operator. There’s nothing that raises a base to a particular exponent in the operator set. So if you want it, you need to write it, or you can use – there’s a math library called pow for raise to power. We’re gonna run our own version of it because it’s gonna give us some practice thing about this. The first one I’m gonna show you is one that should feel very familiar and very intuitive, which is using an iterative formulation. If I’m trying to raise the base to the exponent, then that’s really simply multiplying base by itself exponent times. So this one uses a for loop and does so. It starts the result at one, and for iterations through the number of exponents keeps multiplying to get there. So that one’s fine, and it will perfectly work. I’m gonna show you this alternative way that starts you thinking about what it means to divide a problem up in a recursive strategy. Base to the exponent – I wanna raise five to the tenth power. If I had around some delegate, some clone of myself that I could dispatch to solve the slightly smaller problem of computing five to the ninth power, then all I would need to do is take that answer and multiply it by one more five,

and I’d get five to the tenth. Okay. If I write code that’s based on that, then I end up with something here – and I’m gonna let these two things go through to show us that to compute the answer to five to the tenth power, what I really need is the answer to five to the ninth power, which I do by making a recursive call to the same function I’m in the middle of writing. So this is raise that I’m defining, and in the body of it, it makes a call to raise. That’s what is the mark of a recursive function here. I pass slightly different arguments. In this case, one smaller exponent which is getting a little bit closer to that simplest possible case that we will eventually terminate at where we can say I don’t need to further dispatch any delegates and any clones out there to do the work, but if the exponent I’m raising it to is zero, by definition anything raised to the exponent of zero is one, I could just stop there. So when computing five to the tenth, we’re gonna see some recursion at work. Let me take this code into the compiler so that we can see a little bit about how this actually works in terms of that. So that’s exactly the code I have there, but I can say something that I know the answer to. How about that? First, we’ll take a look at it doing its work, so five times five times five should be 125. So we can test a couple little values while we’re at it. Two the sixth power, that should be 64, just to see a couple of the cases, just to feel good about what’s going on. And then raising say 23 to the zero power should be one as anything raised to the zero power should be. So a little bit of spot testing to feel good about what’s going on. Now I’m gonna go back to this idea like two to the sixth. And I’m gonna set a breakpoint here. Get my breakpoint out. And I’m gonna run this guy in the debugger. Takes a little bit longer to get the debugger up and running, so I’ll have to make a little padder up while we’re going here. And then it tells me right now it’s breaking on raise, and I can look around in the debugger. This is a – did not pick up my compilation? I think it did not. I must not have saved it right before I did it because it’s actually got the base is 23 and the exponent is zero. It turns out I don’t wanna see that case, so I’m gonna go back and try again. I wanna see it – no, I did not. And I’m just interested in knowing a little bit about the mechanics of what’s gonna happen in a recursive situation. If I look at the first time that I hit my breakpoint, then I’ll see that there’s a little bit of the beginnings of the student main, some stuff behind it. There’s a little bit of magic underneath your stack that you don’t really need to know about, but starting from main it went into raise, and the arguments it has there is the base is two, the exponent is six. If I continue from here, then you’ll notice the stack frame got one deeper. There’s actually another indication of raise, and in fact, they’re both active at the same time. The previous raise that was trying to compute two to the sixth is kind of in stasis back there waiting for the answer to come back from this version, which is looking to raise two to the fifth power. I continue again, I get two to the fourth. I keep doing this. I’m gonna see these guys kinda stack up, each one of those kind of waiting for the delegate or the clone to come back with that answer, so that then it can do its further work incorporating that result to compute the thing it needed to do. I get down here to raising two to the first power, and then finally I get to two to the zero, so now I’ve got these eight or so stacked frames, six up there. This one, if I step from here, it’s gonna hit the base case of returning one, and then we will end up kind of working our way back out. So now, we are at the end of the two to the one case that’s using the answer it got from the other one, multiplying it by two. Now I’m at the two to the two case, and so each of them’s kind of unfolding in the stack is what’s called unwinding. It’s popping back off the stack frames that are there and kind of revisiting them, each passing up the

exponent was even or odd, it decided whether to just square that number or whether to square it and tack in another power of the base while we’re at it. So this one is gonna be much more quick about getting down to it, whereas the one we saw was gonna put one stack frame down for every successive exponent power. So if you wanted to raise something to the 10th, or 20th, or 30th power, then you were giving yourself 10, 20, 30 stack frames. Something like 30 stack frames is not really something to be worried about, but if you were really trying to make this work on much larger numbers, which would require some other work because exponent is a very rapidly growing operator and would overflow your integers quickly, but this way very quickly divides in half. So it goes from a power of 100, to a power of 50, to 25, to 12, to 6, to 3, to 2, to 1, so that dividing in half is a much quicker way to work our way down to that base case and get our answer back, and we’re doing a lot fewer calculations than all those multiplies, one per base. So just a little diversion. So let me tell you something, just a little bit about kind of style as it applies to recursion. Recursion is really best when you can express it in the most kind of direct, clear, and simple code. It’s hard enough to get your head around a recursive formulation without complicating it by having a bunch of extraneous parts where you’re doing more work than necessary, or redundantly handling certain things. And one thing that’s actually very easy to fall in the trap of is thinking about there’s lots of other base cases you might be able to easily handle, so why not just go ahead and call out for them, test for – you’re at the base case. You’re close to the base case. Checking before you might make a recursive call, if you’re gonna hit the base case when you make that call, then why make the call. I’ll just anticipate and get the answer it would’ve returned anyway. It can lead to code that looks a little bit like this before you’re done. If the exponent’s zero, that’s one. If the exponent’s one, then I can just return the base. If it’s two, then I can just multiply the base by itself. If it’s three, I can start doing this. Certainly, you can follow this to the point of absurdity, and even for some of the simple cases, it might seem like you’re saving yourself a little bit of extra time. You’re like why go back around and let it make another recursive call. I could stop it right here. It’s easy to know that answer. But as you do this, it complicates the code. It’s a little harder to read. It’s a little harder to debug. Really, the expense of making that one extra call, two extra calls is not the thing to be worried about. What we really want is the cleanest code that expresses what we do, has the simplest cases to test, and to examine, and to follow, and not muck it up with things that in effect don’t change the computational power of this but just introduce the opportunity for error. Instead of a multiply here, I accidentally put a plus. It might be easy to overlook it and not realize what I’ve done, and then end up computing the wrong answer when it gets to the base case of two. In fact, if you do it this way, most things would stop at three, but these would suddenly become kind of their own special cases that were only hit if you directly called them with the two. The recursive cases will all stop earlier. It just complicates your testing pass now because not everything is going through the same code. So we call this arm’s length recursion. I put a big X on that looking ahead, testing before you get there. Just let the code fall through. So Dan, he’s not here today, but he talked to me at the end of Friday’s class, and it made me wanna actually just give you a little bit of insight into recursion as it relates to efficiency. Recursion by itself, just the idea of applying a recursive technique to solving a problem, does not give you any guarantee that it’s gonna be the best solution, the most efficient solution, or the fact that it’s gonna give you a very inefficient solution. Sometimes people

have kind of a bad rap on recursion. It’s like recursion will definitely be inefficient. That actually is not guaranteed. Recursion often requires exactly the same resources as the iterative approach. It takes the same amount of effort. Surveying the campus – if you’re gonna survey the 10,000 people on campus and get everybody’s information back, whether you’re doing it divide and conquer, or whether you’re sitting out there in White Plaza asking each person one by one, in the end 10,000 people got survey. The recursion is not part of what made that longer or shorter. It might actually depending on how you have resources work out better. Like if you could get a bunch of people in parallel surveying, you might end up completing the whole thing in less time, but it required more people, and more clipboards, and more paper while the process is ongoing than me standing there with one piece of paper and a clipboard. But then again, it took lots of my time to do it. So in many situations, it’s really no better or no worse than the alternative. It makes a little bit of some tradeoffs of where the time is spent. There are situations though where recursion can actually make something that was efficient inefficient. There are situations where it can take something that was inefficient and make it efficient. So it’s more of a case-by-case basis to decide whether recursion is the right tool if efficiency is one of your primary concerns. I will say that for problems with simple iterative solutions that operate relatively efficiently, iteration is probably the best way to solve it. So like the raise to power, yeah you could certainly do that iteratively. There’s not some huge advantage that recursion is offering. I’m using them here because they’re simple enough to get our head around. We’re gonna work our way toward problems where we’re gonna find things that require recursion, which is kind of one of the third points I put in. Why do we learn recursion? What’s recursion gonna be good for? First off, recursion is awesome. There are some problems that you just can’t solve using anything but recursion, so that the alternatives like I’ll just write it iteratively won’t work. You’ll try, but you’ll fail. They inherently have a recursive structure to them where recursion is the right tool for the job. Often, it produces the most beautiful, direct, and clear elegant code. The next assignment that will go out has these problems that you do in recursion, and they’re each about ten lines long. Some of them are like five lines long. They do incredible things in five lines because of the descriptive power of describing it as here’s a base case, and here’s a recursive case, and everything else just follows from applying this, and reducing the problem step by step. So you will see things where the recursive code is just beautiful, clean, elegant, easy to understand, easy to follow, easy to test, solves the recursive problem. And in those cases, it really is a much better answer than trying to hack up some other iterative form that may in the end be no more efficient. It may be even less efficient. So don’t let efficiency be kind of a big fear of what recursion is for you. So I’m gonna do more examples. I’ve got three more examples, or four I think today, so I will just keep showing you different things, and hopefully the patterns will start to kind of come out of the mist for you. A palindrome string is one that reads the same forwards and backwards when reversed. So was it a car or a cat I saw, if you read that backwards, it turns out it says the same thing. Go hang a salami, I’m a lasagna hog. Also handy when you need to have a bar bet over what the longest palindrome is to have these things around. There are certainly ways to do this iteratively. If you were given a string and you were interested to know is it a palindrome, you could kind of do this marching – you’re looking outside and kind of marching your way into the middle. But we’re gonna go ahead and let recursion kinda help us deal with the subproblem of this,

mystical at first. That feeling you feel a little bit discombobulated about this is probably pretty normal, but we’re gonna keep seeing examples, and hope that it starts to feel a little less unsettling. Anybody wanna ask a question about this so far? Yeah?

Student: So I guess create your base case first, then test it? [Inaudible].

Instructor (Julie Zelenski) :That’s a great question. So I would say typically that’s a great strategy. Get a base case and test against the base case, so the one character and the two character strings. And then imagine one layer out, things that will make one recursive call only. So in this case for the palindrome, it’s like what’s a two-character string? One has AB. One has AA. So one that is a palindrome, one that isn’t, and watch it go through. Then from there you have to almost take that leap and say it worked for the base case. It worked for one out. And then you have to start imagining if it worked for a string of length N, it’ll work for one of N plus one, and that in some sense testing it exhaustively across all strings is of course impossible, so you have to kind of move to a sort of larger case where you can’t just sit there and trace the whole thing. You’ll have to in some sense take a faith thing that says if it could have computed whether the interior’s a palindrome, then adding two characters on the outside and massaging that with that answer should produce the right thing. So some bigger tests that verify that the recursive case when exercised does the right thing, then the pair together – All your code is going through these same lines. The outer case going down to the recursive case, down to that base case, and that’s one of the beauty of writing it recursively is that in some sense once this piece of code works for some simple cases, the idea that setting it to larger cases is almost – I don’t wanna say guaranteed. That maybe makes it sound too easy, but in fact, if it works for cases of N and then N plus one, then it’ll work for 100, and 101, and 6,000, and 6,001, and all the way through all the numbers by induction. Question?

Student: You have to remove all the [inaudible], all the spaces?

Instructor (Julie Zelenski) :Yeah. So definitely like the was it a car to match I should definitely be taking my spaces out of there to make it right. You are totally correct on that. So let me show you one where recursion is definitely gonna buy us some real efficiency and some real clarity in solving a search problem. I’ve got a vector. Let’s say it’s a vector of strings. It’s a vector of numbers. A vector of anything, it doesn’t really matter. And I wanna see if I can find a particular entry in it. So unlike the set which can do a fast contains for you, in vector if I haven’t done anything special with it, then there’s no guarantee about where to find something. So if I wanna say did somebody score 75 on the exam, then I’m gonna have to just walk through the vector starting at Slot 0, walk my way to the end, and compare to see if I find a 75. If I get to the end and I haven’t found one, then I can say no. So that’s what’s called linear search. Linear kind of implies this left to right sequential processing. And linear search has the property that as the size of the input grows, the amount of time taken to search it grows in proportion. So if you have a 1000 number array, and you doubled its size, you would expect that doing a linear search on it should take twice as long for an array that’s twice as large. The strategy we’re gonna look at today is binary search which is gonna try to avoid this looking in every one of those boxes to find something. It’s gonna take a divide and conquer strategy,

and it’s gonna require a sorted vector. So in order to do an efficient lookup, it helps if we’ve done some pre-rearrangement of the data. In this case, putting it into sorted order is gonna make it much easier for us to be able to find something in it because we have better information about where to look, so much faster. So we’ll see that surely there was some cost to that, but typically binary search is gonna be used when you have an array where you don’t do a lot of changes to it so that putting it in a sorted order can be done once, and then from that point forward you can search it many times, getting the benefit of the work you did to put it in sorted order. If you plan to sort it just to search it, then in some sense all the time you spent sorting it would kind of count against you and unlikely to come out ahead. So the insight we’re gonna use is that if we have this in sorted order, and we’re trying to search the whole thing – we’re looking for let’s say the No. 75 – is that if we just look at the middlemost element, so we have this idea that we’re looking at the whole vector right now from the start to the end, and I look at the middle element, and I say it’s a 54. I can say if 75 is in this vector because it’s in sorted order, it can’t be anywhere over here. If 54’s right there, everything to the left of 54 must be less than that, and 75 wouldn’t be over there. So that means I can just discard that half of the vector from further consideration. So now I have this half to continue looking at which is the things that are the right of 54 all the way to the end. I use the same strategy again. I say if I’m searching a vector – so last time I was searching a vector that had 25 elements. Now I’ve got one that’s got just 12. Again, I use the same strategy. Look at the one in the middle. I say oh, it’s an 80, and then I say well the number I’m looking for is 75. It can’t be to the right of the 80. It must be to the left of it. And then that lets me get rid of another quarter of the vector. If I keep doing this, I get rid of half, and then a quarter, and then an eighth, and then a 16th. Very quickly, I will narrow in on the position where if 75 is in this vector, it has to be. Or I’ll be able to conclude it wasn’t there at all. Then I kind of work on my in, and I found a 74 and a 76 over there, then I’m done. That base case comes when I have such a small little vector there where my bounds have crossed in such a way that I can say I never found what I was looking for. So let’s walk through this bit of code that kind of puts into C++ the thing I just described here. I’ve got a vector. In this case, I’m using a vector that’s containing strings. It could be ints. It could be anything. It doesn’t really matter. I’ve got a start and a stop, which I identify the sub-portion of the vector that we’re interested in. So the start is the first index to consider. The stop is the last index to consider. So the very first call to this will have start set to zero and stop set to the vector’s size minus one. I compute the midpoint index which is just the sum of the start and stop divided by two, and then I compare the key that I’m looking for to the value at that index. I’m looking right in the middle. If it happens to match, then I return that. The goal of binary search in this case is to return the index of a matching element within the vector, or to return this not found negative one constant if it was unable to find any match anywhere. So when we do find it at whatever the level the recursion is, we can just immediately return that. We’re done. We found it. It’s good. Otherwise, we’re gonna make this recursive call that looks at either the left half or the right half based on if key is less than the value we found at the midpoint, then the place we’re searching is – still has the same start position, but is now capped by the element exactly to the left of the midpoint, and then the right one, the inversion of that, one to the right of the midpoint and the stop unchanged. So taking off half of the elements under consideration at each stage, eventually I will get down to the simplest possible case. And the simplest possible

machinations that could be present here, and counting them, it’s not quite obvious unless you kinda go back to start working on your real math skills how it is that you can write a formula for this. So what I’m gonna give you is a recursive way of thinking about this problem. So I drew a set of the things we’re looking at? So there are N things that we’re trying to choose K out of. So right now, I’ve got 12 or so people, or items, whatever it is. What I’m gonna do is I’m gonna imagine just designating one totally at random. So pick Bob. Bob is one of the people in the dorm. I’m gonna kind of separate him from everybody else mentally in my mind, and draw this line, and kinda mark him with a red flag that says that’s Bob. So Bob might go to Flicks or might not go to Flicks. Some of the subsets for going to Flicks will include Bob, and some will not. Okay. So what I’d like to think about is how many different subsets can I make that will include Bob, and how many different subsets can I make that don’t include Bob. And if I added those together, then that should be the total number of subsets I can make from this collection. Okay, so the subsets that include Bob – so once I’ve committed to Bob being in the set, and let’s say I’m trying to pick four members out of here, then I have Bob and I have to figure out how many ways can I pick three people to accompany Bob to the Flicks. So I’m picking from a slightly smaller population. The population went down by one, and the number I’m seeking went down by one, and that tells me all the ways I can pick three people to go with Bob. The ones that don’t include Bob, and Bob’s just out of the running, I look at the remaining population which is still one smaller, everybody but Bob, and I look for the ways I can still pick four people from there. So what I have here is trying to compute C of NK is trying to compute C of N minus one K minus one and add it to C of N minus one K. So this is picking friends to accompany Bob. This is picking people without Bob. Add those together, and I will have the total number of ways I can pick K things out of N. So we’re very much relying on this recursive idea of if I had the answer – I don’t feel smart enough to know the answer directly, but if I could defer it to someone who was actually willing to do more scrutiny into this thing, if you could tell me how many groups of three you can join with Bob, and how many groups of four you can pick without Bob, I can tell you what the total answer is. The simplest possible base case we’re gonna work our way down to are when there are just no choices remaining at all. So if you look back at my things that are here, in both cases the population is getting smaller, and in one of the recursive calls, the number of things I’m trying to pick is also getting smaller. So they’re both making progress toward kind of shrinking those down to where there’s kind of more and more constraints on what I have to choose. For example, on this one as I keep shrinking the population by one and trying to get the same number of people, eventually I’ll be trying to pick three people out of three, where I’m trying to pick of K of K remaining. Well, there’s only one way to do that which is to take everyone. On this one, I’ll eventually keep picking people, so the K is always less than the N in this case. The K will eventually kinda bottom out, or I’ll say I’ve already picked all the people. I’ve already picked four people. I need to pick zero more. And at that point, that’s also very easy, right? Picking zero out of a population, there’s only one way to do that. So what we end up with is a very simple base case of if K equals zero – so I’m not trying to choose anymore. I’ve already committed all the slots. Or if K is equal to N where I’ve discarded a whole bunch of people, and now I’m down to where I’m facing I’ve gotta get four, and I’ve got four left. Well, there’s only one way to do those things, and that’s to take everybody or to take nobody. And then otherwise, I make those two

recursive calls with Bob, without Bob, add them together to get my whole result. That’s wacky. I’m gonna read you something, and then we’ll call it a day. I brought a book with me. I stole this from my children.

[End of Audio]

Duration: 48 minutes