What is an algorithm?

Michael walks a student through what an algorithm is: a set of finite rules for solving problems, whether numerical or not. They use a Pygame bug where an actor walks off screen and a peanut-butter sandwich recipe to show how algorithms describe both code and physical tasks.
Chapters
Um, yeah, no, I think it's, it's so cool. Good for Shai for posting that logo. Um, yeah, how's— how was school last week?
It was great. Um, we had a lot of fun. I think Thursday and Friday Karen was traveling, so we had Alex as a sub. I like Alex a lot. He's really cool. And he's in California too.
He is. He's great. What were you reading with Alex?
We read The Yellow Wallpaper. It was, I think, a more harder text for us just because of the amount of words that we didn't know.
Yeah.
But it was really interesting. I think at the very end, we were all very intrigued because of the plot.
Excellent. And then do you look up words you don't know in class? Do other students know them? How do you, how do you figure out words you don't know when you're going through it?
Well, if other students know them, then they tell us. But if we all don't know, and that's kind of common, then we just, uh, or yeah, somebody looks it up.
Got it.
And then post it in the chat.
Yeah, no, it's terrific to be developing your vocabulary like that. And then how about Harvard CS50? How is that going?
It's going good. I'm moving on, not to the next week, but to another project. I tried working out a solution for one of the problems we had. I fixed one. I didn't fix another. So I'm just kind of on that track.
And are you still on Lesson 1?
I will be on 10 weeks soon. Too soon. I'm kind of going through it slowly just so I can kind of understand everything more clearly.
Got it. Um, do you know what an algorithm is?
I know programmers use the term a lot, um, and I think I don't know in depth what it is.
Well, this is time to learn a new word. So I just put in the chat, algorithm, a set of finite rules or instructions to be followed in calculations or other problem-solving operations. Does that make sense to you?
I think so.
So do you think that you are using algorithms when you are programming?
Yes.
Would you say that everything you do is an algorithm, or specific things you're doing within a program or a problem are algorithms?
Let's see, specific things, because not everything includes numbers, or that you can tell.
Well, where did— where do you get numbers out of this definition?
It says, um, I guess I just assumed because it said calculations and solving operations.
Excuse me, yes. And when I read it and I see calculations or other problem-solving operations, Do you think that would mean it could be non-numerical problem solving?
Oh, yes.
Got it. So are, are all problems numerical problems in computer science, or some problems not numerical problems?
Some problems are not.
Got it. Can you think of a specific problem that you solved that was not numerical? While programming?
I mean, while programming, there's definitely a few instances. Maybe if just one specific, really important part of the code isn't working, just like printing something in other languages or in other senses. Like showing a character on the screen, or it's moving wrong, it's not moving, or something's wrong with the code, but it doesn't— or the code itself is just like a function and I do something wrong with it.
I'm vaguely getting a sense of what you're talking about. It looks like you're almost visualizing a particular situation you've been in. But I'm not clear on the example of a non-numerical problem that you solved. Can you try to explain it a little bit more clearly so that I can visualize it?
Yes, for— I'm just going to give an actual example. When I was doing Pygame with one of my instructors, a frequent— or Pygame is sort of making or it's still Python, but you're making games with Python. A frequent problem that came up a lot is where I'm moving an actor or a character we had on the screen, but it— and then I set boundaries for it to stop moving if and when it touched the boundaries, and then they just went through the boundaries, and maybe something was wrong with the function. I guess that would still include numbers because of the coordinates, But the actual problem itself doesn't necessarily include numbers, although that may not be the best example now that I think of it.
Well, what I'm getting— and thank you for having such a specific example— I often find that when we're communicating about abstract ideas, it's helpful to be very specific. And so not knowing the code that you were actually writing, what I'm getting is that there was a problem. The specific problem you had to solve was that the actor was kind of walking through the boundary. Is that correct, today?
Yes.
And you're right, the numerical coordinates were part of the solution, but the problem was with how— what was going on. And so let's look at algorithm in light of that. So A set of finite rules. First of all, do you know what finite means?
Yes, it's not infinite, so it ends.
Excellent. Yeah, a set of finite rules or instructions. Is there a difference between rule or instruction, or do you think they're kind of the same?
I think there could be a difference. A rule is like a rule that's set in stone, or It is what I think of like a rule that's set in stone and you can't disobey it. And then an instruction is like a recipe. It tells you to get a few ingredients, but under some circumstances you can change it if you'd like. And instructions are just for that time being.
Okay, those are— that's plausible interpretation based on your your knowledge of those words, a finite, a set of finite rules or instructions to be followed in calculations or other problem-solving operations. I guess when we look at your solution to this problem of the, the problem of the agent walking through the barrier, did your solution include just changing the coordinates, that is the calculations, or did your solution also change other parts of the code? I can't hear you. You're muted.
My bad. It's not necessarily just coordinates. There are different functions and things where the code are that I need to fix, maybe different variables inside the function.
Got it.
I think it depends.
So the thing with variables is I would assume the variables are— have numerical values. Is that correct?
Yes.
So I'm still trying to get at what the non-calculation, non-numerical part of solving this problem was. And maybe there is. Do you— And maybe this is hard to do in the abstract. If we had the code in front of us, it would be easier. But is your sense that there was the part of solving that problem including non-numerical problem solving, or was it, now that you reflect on it, was it in fact strictly a numerical problem, a calculation problem?
Well, it may not have been strictly a numerical solution, but now that I do think of it, it was, in the most part mainly numerical.
Yeah, that makes sense because, right, coordinates, once you talk about coordinates, then basically you're talking about calculations. So that makes sense. In which case, can you think of any other problem solving while coding that did not involve calculations or involved something in addition to calculations? Or do you think all of your coding is ultimately a matter of calculations.
Um, I'm trying to think of another example. Well, I mean, I do think that most of the problems and/or solutions, many of them you typically face, do include numbers and digits and calculations and where you put different numbers and stuff. There are a few cases where there aren't any numbers in your code at all yet, like when you just start, and then there's something immediately wrong with your code, which is kind of annoying. But it doesn't necessarily take numbers to— you just have to change the actual code or the written form of code. But other than that, it's mostly that I can think of right now. It's mostly calculations and digits.
Interesting. And ideally we would be looking at code so we can sort of dig into that more clearly. I want to give you a completely different example outside of coding. And I think we talked about something like this a long time ago. Sometimes I hear people talk about things like an algorithm for making a peanut butter sandwich. Does it make sense? Yeah. Do you think we— is it fair to talk about your algorithm for making a peanut butter sandwich?
Yes.
Would you say that's a numerical or a non-numerical problem to solve?
I mean, I assume it's a non-numerical, but I guess if you're counting steps you're taking, then it's numerical. But I think the actual process of it is Physical, because it's in the real world.
Exactly. And it would be an interesting thing, you know, if you're explaining to another human how to make a peanut butter sandwich. Well, I'm thinking of all the steps. Part of it is with a human, humans know so much. I think, you know, get a loaf of bread, take 2 slices out, get peanut butter, open the peanut butter, yada yada yada. That as humans understand it so quickly, it's not too so hard. But if you had to explain it to a computer, or you have— if you had to get a computer to make a peanut butter sandwich, can you even imagine writing code that would— if you had robotic arms, could you imagine writing code that would allow a computer to make a peanut butter sandwich?
I mean, I think I could. Imagine people doing that, but I just have no idea what that code is.
No, I— it would actually be, I think, very hard. And by the way, I think there would be a lot of calculations because just like coordinates, just like in your game, the coordinates, you know, the robotic arms would have to figure out where is the peanut butter, where's the bread, how do I hold the knife. Um, you've got coordinates all over the place. But yeah, at a higher level, the big, big actions themselves don't seem numerical, but when you get down into the details, it seems much more coordinate-driven. Does that sound accurate with the code you're writing, that at kind of at a high level, there's sort of more of ideas about what happens, and then once you get down into the details, it's usually numerical?
Yes.
OK. Well, at some point, I am going to have to start digging into your code so we can talk very specific about the code you're writing. And at some point, I would actually love to see code you're writing. So if you ever solve a problem in Harvard CS50 and are proud of it, please send it over so I can see what you're doing. Again, I can't code in Python, but I think I can read Python code reasonably well. So I can probably keep up with understanding most of what you're doing. But congrats, it sounds like you're doing great work, and I will let you have a good week. Take care.
Thank you, Michael. See you next week.
Bye.










