📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Harvard Professor Explains Algorithms in 5 Levels of Difficulty | WIRED

WIRED25:47

Transcription

Hello world, my name is David J. Ma, and I'm a professor of computer science at Harvard University. Today, I've been asked to explain algorithms in five levels of increasing difficulty.

Algorithms are important because they really are everywhere, not only in the physical world but certainly in the virtual world as well. In fact, what excites me about algorithms is that they represent an opportunity to solve problems. I dare say, no matter what you do in life, all of us have problems to solve.

So, I'm a computer science professor, and I spend a lot of time with computers. How would you define a computer for them?

Well, a computer is electronic, like a phone, but it's a rectangle, and you can type, like tick tick tick, and you work on it.

Nice! Do you know any of the parts that are inside of a computer?

Um, no.

Can I explain a couple of them to you?

Yeah! So, like inside of every computer is some kind of brain, and the technical term for that is CPU, or Central Processing Unit. Those are the pieces of hardware that know how to respond to those instructions, like moving up or down or left or right. They know how to do math, like addition and subtraction.

Then there's at least one other type of hardware inside of a computer called memory, or RAM, if you've heard of this.

I know memory because you have to memorize stuff.

Yeah, exactly! Computers have different types of memory. They have what's called RAM, or Random Access Memory, which is where your games and programs are stored while they're being used. But then it also has a hard drive or a solid-state drive, which is where your data, your high scores, your documents—once you start writing essays and stories in the future—stays there permanently.

So even if the power goes out, the computer can still remember that information. It's still there because the computer can't just delete all the words itself. Your fingers can only do that; you have to use your finger to delete all the stuff.

Exactly! Have you heard of an algorithm before?

Um, yes. An algorithm is a list of instructions to tell people what to do, or like a robot what to do.

Yeah, exactly! It's just step-by-step instructions for doing something, for solving a problem.

For example, if you have a bedtime routine, then first you say, "I get dressed, I brush my teeth, I read a little story, and then I go to bed."

Alright, well how about another algorithm? Like, what do you tend to eat for lunch? Any types of sandwiches you like?

Uh, I eat peanut butter.

Let me get some supplies from the cupboard here.

So should we make an algorithm together? Why don't we do it this way: pretend like I'm a computer, or maybe I'm a robot, so I only understand your instructions. I want you to feed me—no pun intended—an algorithm, so step-by-step instructions for solving this problem.

But remember, algorithms—you have to be precise. You have to give the right instructions.

The right instructions! Just do it for me.

Okay, step one was what?

Open the bag.

Okay, opening the bag of bread.

Stop! Now grab the bread and put it on the plate.

Grab the bread and put it on the plate.

Take all the bread back and put it back in there.

So that's like an undo command, little control Z.

Okay, take one bread and put it on the plate.

Take the lid off the peanut butter.

Okay, take the lid off the peanut butter. Put the lid down.

Okay, take the knife.

Take the knife, put the blade inside the peanut butter, and spread the peanut butter on the bread.

I'm going to take out some peanut butter, and I'm going to spread the peanut butter on the bread. I put a lot of peanut butter on because I love peanut butter.

Apparently, I thought I was messing with you here, but I think you're happy with this.

Put the knife down, and then grab one bread and put it on top of the second bread sideways.

Sideways?

Sideways, like put it flat on.

Oh, flat ways!

Okay, and now you're done.

You're done with your sandwich! Should we take a delicious bite?

Yep, let's take a bite.

Okay, here we go. What would be the next step?

Clean all this mess up!

Clean all this mess up, right? We made an algorithm—step-by-step instructions for solving some problem.

If you think about how we made peanut butter and jelly sandwiches, sometimes we were imprecise. You didn't give me quite enough information to do the algorithm correctly, and that's why I took out so much bread.

Precision—being very correct with your instructions—is so important in the real world. For instance, when you're using the worldwide web and you're searching for something on Google or Bing, you want to do the right thing.

So like if you type in just "Google," then you won't find the answer to your question. Pretty much everything we do in life is an algorithm, even if we don't use that fancy word to describe it.

You and I are sort of following instructions, either that we came up with ourselves or maybe our parents told us how to do these things. Those are just algorithms.

But when you start using algorithms in computers, that's when you start writing code.

What do you know about algorithms?

Nothing really, um, at all honestly. I think it's just probably a way to store information in computers.

And I dare say, even though you might not have put this word on it, odds are you executed multiple algorithms today, even before you came here.

Like what were a few things that you did?

I got ready.

Okay, and getting ready—what does that mean?

Brushing my teeth, brushing my hair, getting dressed.

Okay, so all of those, frankly, if we really dove more deeply, could be broken down into step-by-step instructions. Presumably, your mom, your dad, someone in the past sort of programmed you as a human to know what to do.

Then after that, as a smart human, you can sort of take it from there, and you don't need their help anymore.

But that's kind of what we're doing when we program computers.

Something maybe even more familiar nowadays—like odds are you have a cell phone, your contacts, or your address book.

But let me ask you why that is. Why does Apple or Google or anyone else bother alphabetizing your contacts?

I just assumed it would be easier to navigate.

What if your friend happened to be at the very bottom of this randomly organized list?

Like why is that a problem?

Like he or she is still there.

I guess it would take a while to get to.

While you're scrolling, that in itself is kind of a problem, or it's an inefficient solution to the problem.

So it turns out that back in my day, before there were cell phones, everyone's numbers from high schools were literally printed in a book.

Everyone in my town and my city, my state, was printed in an actual phone book.

Even if you've never seen this technology before, how would you propose verbally to find John in this phone book?

I would just flip through and just look for the J, I guess.

Yeah, so let me propose that we start that way. I could just start at the beginning, and step by step, I could just look at each page looking for John.

Now, even if you've never seen this technology before, it turns out this is exactly what your phone could be doing in software.

Someone from Google or Apple or the like could write software that uses a technique in programming known as a loop.

And a loop, as the word implies, is just sort of do something again and again.

What if instead of starting from the beginning and going one page at a time, what if I—or what if your phone—goes like two pages or two names at a time?

Would this be correct, do you think?

Well, you could skip over John, I think.

In what sense?

If he's in one of the middle pages that you skipped over.

Yeah, so sort of accidentally, and frankly, with like 50/50 probability, John could get sandwiched in between two pages.

But does that mean I have to throw that algorithm out altogether?

Maybe you could use that strategy until you get close to the section and then switch to going one by one.

Okay, that's nice! So you could kind of like go twice as fast but then kind of pump the brakes as you near your exit on the highway, or in this case, near the J section of the book.

Exactly! And maybe alternatively, if I get to like A, B, C, D, E, F, G, H, I, J, K—if I get to the K section, then I could just double back like one page just to make sure John didn't get sandwiched between those pages.

So the nice thing about that second algorithm is that I'm flying through the phone book like two pages at a time.

So 2, 4, 6, 8, 10, 12—it's not perfect, it's not necessarily correct, but it is if I just take like one extra step.

So I think it's fixable.

But what your phone is probably doing, and frankly what I and like my parents and grandparents used to do back in the day, is we'd probably go roughly to the middle of the phone book here.

And just intuitively, if this is an alphabetized phone book in English, what section am I probably going to find myself in?

Roughly K.

Okay, so I'm in the K section. Is John going to be to the left or to the right?

To the left.

Yeah, so John is going to be to the left.

To the right.

What we can do here, though your phone does something smarter, is tear the problem in half, throw half of the problem away, being left with just 500 pages now.

But what might I next do?

I could sort of naively just start at the beginning again.

But we've learned to do better. I can go roughly to the middle here.

Do it again!

Yeah, exactly! So now maybe I'm in the E section, which is a little to the left, so John is clearly going to be to the right.

So I can again tear the problem partly in half, throw this half of the problem away.

And I claim now that if we started with a thousand pages, now we've gone to 500, 250—now we're really moving quick!

Not on that page, and I can call him.

Roughly how many steps might this third algorithm take?

If I started with a thousand pages, then went to 500, 250, 125—like how many times can you divide 1,000 in half?

Maybe 10.

That's roughly 10 because in the first algorithm, looking again for someone like Zoe, in the worst case, I might have to go all the way through a thousand pages.

But the second algorithm you said was 500—maybe 500 in one. Essentially the same thing, so twice as fast.

But this third and final algorithm is sort of fundamentally faster because you're sort of dividing and conquering it in half and half and half—not just taking one or two bites out of it at a time.

So this, of course, is not how we used to use phone books back in the day, since otherwise they'd be single-use only.

But it is how your phone is actually searching for Zoe, for John, for anyone else, but it's doing it in software.

Oh, that's cool!

So here we've happened to focus on searching algorithms, looking for John in the phone book.

But the technique we just used can indeed be called divide and conquer, where you take a big problem and you divide and conquer—that is, you try to chop it up into smaller, smaller, smaller pieces.

A more sophisticated type of algorithm, at least depending on how you implement it, is something known as a recursive algorithm.

A recursive algorithm is essentially an algorithm that uses itself to solve the exact same problem again and again, but chops it smaller and smaller and smaller.

[Music]

Ultimately, hi! My name is Patricia.

Patricia, nice to meet you! Where are you a student at?

I'm starting my senior year now at NYU.

Oh, nice! And what have you been studying the past few years?

I study computer science and data science.

If you were chatting with a non-CS, non-data science friend of yours, how would you explain to them what an algorithm is?

Some kind of systematic way of solving a problem, or like a set of steps to kind of solve a certain problem you have.

So you probably recall learning topics like binary search versus linear search and the like.

So I've come here complete with an actual chalkboard with some magnetic numbers on it.

How would you tell a friend to sort these?

I think one of the first things we learned was something called bubble sort.

It was kind of like focusing on smaller bubbles, I guess I would say, like looking at smaller segments rather than the whole thing at once.

What is very true about what you're hinting at is that bubble sort really focuses on local small problems rather than taking a step back, trying to fix the whole thing.

Let's just fix the obvious problem in front of us.

So for instance, when we're trying to get from smallest to largest, and the first two things we see are eight followed by one, this looks like a problem because it's out of order.

So what would be the simplest fix, the least amount of work we can do to at least fix one problem?

Just like switch those two numbers because one is obviously smaller than eight.

Perfect! So we just swap those two.

Then you would switch those again?

Yeah, so that further improves the situation, and you can kind of see that the one and the two are now in place.

How about eight and six?

Switch it again!

Switch those again!

Eight and three—switch it again!

And conversely, now the one and the two are closer to—and coincidentally are exactly where we want them to be.

So are we done?

No!

Okay, so obviously not. But what could we do now to further improve the situation?

Go through it again, but like you don't need to check the last one anymore because we know that number has bubbled up to the top.

Yeah, because it has indeed bubbled all the way to the top.

So one and two—keep it.

Two and six—keep it as is.

Okay, six and three—then you switch it.

Okay, we switch or swap those.

Six and four—swap it again!

Okay, so four and six and seven—keep it.

Okay, seven and five—swap it!

Okay, and then I think, per your point, we're pretty darn close.

Let's go through once more.

One and two—keep it.

Two and three—keep it.

Three and four—keep it.

Four and six—keep it.

Six and five—then switch it!

Alright, we'll switch this, and now to your point, we don't need to bother with the ones that already bubbled their way up.

Now we're 100% sure it's sorted!

Yeah, and certainly the search engines of the world—Google and Bing and so forth—they probably don't keep web pages in sorted order because that would be a crazy long list when you're just trying to search the data.

But there's probably some algorithm underlying what they do, and they probably similarly just like we do a bit of work upfront to get things organized, even if it's not strictly sorted in the same way.

So how about social media? Can you envision where the algorithms are in that world?

Like maybe for example TikTok, like the For You page?

It's kind of like recommendations, right?

It's like sort of like Netflix recommendations, except more constant because it's just like every video you scroll, it's like that's a new recommendation basically.

And it's based on like what you've liked previously, what you've saved previously, what you search up.

So I would assume there's some kind of algorithm there kind of figuring out what to put on your For You page.

Absolutely! Just trying to keep you presumably more engaged.

So the better the algorithm is, the better your engagement is.

Maybe the more money the company then makes on the platform and so forth.

So it all sort of feeds together.

But what you're describing really is more artificially intelligent, if I may, because presumably there's not someone at TikTok or any of these social media companies saying, "If Patricia likes this post, then show her this post. If she likes this post, then show her this other post."

Because the code would sort of grow infinitely long, and there's just way too much content for a programmer to be having those kinds of conditionals, those decisions being made behind the scenes.

So it's probably a little more artificially intelligent.

And in that sense, you have topics like neural networks and machine learning, which really describe taking as input things like what you watch, what you click on, what your friends watch, what they click on, and sort of trying to infer from that instead what should we show Patricia or her friends next.

Okay, yeah, yeah, that makes the distinction more sense now.

Yeah, I am currently a fourth-year PhD student at NYU. I do robot learning, so that's half and half robotics and machine learning.

Sounds like you've dabbled with quite a few algorithms!

So how does one actually research algorithms or invent algorithms?

The most important was just trying to think about inefficiencies and also think about connecting threads.

The way I think about it is that an algorithm for me is not just about the way of doing something, but it's about doing something efficiently.

Learning algorithms are practically everywhere now.

CU Google, I would say, for example, is learning every day about what articles with links might be better than others and reranking them.

There are recommender systems all around us, right?

Like content feeds and social media, or you know, like YouTube or Netflix.

What we see is in large part determined by this kind of learning algorithms.

Nowadays, there's a lot of concerns around some applications of machine learning and deep fakes, where it can kind of learn how I talk and learn how you talk and even how we look and generate videos of us.

We're doing this for real, but you could imagine a computer synthesizing this conversation eventually.

But how does it even know what I sound like and what I look like and how to replicate that?

All of this learning algorithms that we talk about, right?

A lot like what goes in there is just lots and lots of data.

So data goes in, something else comes out.

What comes out is whatever objective function that you optimize for.

Like where is the line between algorithms that play games with and without AI?

I think when I started off my undergrad, the current AI machine learning was not very much synonymous.

Okay, and even in my undergraduate AI class, they learned a lot of classical algorithms for game plays.

Like for example, the A* search, right?

That's a very simple example of how you can play a game without having anything learned.

This is very much, "Oh, you are at a game state, you just search down, see what are the possibilities, and then you pick the best possibility that it can see."

Versus what you think about when you think about AI gameplay, like AlphaZero for example or AlphaStar.

There are a lot of fancy new machine learning agents that are learning very difficult games like Go.

And those are learned agents, as in they are getting better as they play more and more games.

And as they get more games, they kind of refine their strategy based on the data that they've seen.

Once again, this high-level abstraction is still the same: you see a lot of data and you learn from that.

But the question is, what is the objective function that you're optimizing for?

Is it winning this game? Is it forcing a tie? Or is it, you know, like opening a door in a kitchen?

So if the world is very much focused on supervised, unsupervised, reinforcement learning now, like what comes next in 5 to 10 years? Where's the world going?

I think that this is just going to be more and more—I don't want to use the word encroachment, but that's what it feels like—of algorithms into our everyday life.

Like even when I was taking the train here, right? The trains are being routed with algorithms.

But this has existed for, you know, like 50 years probably.

But as I was coming here, as I was checking my phone, those are different algorithms.

And you know, they're kind of getting all around us, with us all the time.

They're making our life better in most places, most cases, and I think that's just going to be a continuation of all of those.

It feels like they're even in places you wouldn't expect, and there's just so much data about you and me and everyone else online.

This data is being mined and analyzed and influencing things we see and hear.

It would seem there is sort of a counterpoint, which might be good for the marketers but not necessarily good for you and me as individuals.

You know, like we're human beings, but for someone, we might be just a pair of eyes who are, you know, carrying a wallet and are there to buy things.

But there is so much more potential for these algorithms to just make our life better without, you know, changing much about our life.

I'm Chris Wiggins, an associate professor of applied mathematics at Columbia.

I'm also the chief data scientist of the New York Times.

The data science team at the New York Times develops and deploys machine learning for newsroom and business problems.

But I would say the things that we do mostly you don't see.

But it might be things like personalization algorithms recommending different content.

Do data scientists, which is rather distinct from the phrase computer scientist, do data scientists still think in terms of algorithms as driving a lot of it?

Oh, absolutely!

In fact, in data science and academia, often the role of the algorithm is the optimization algorithm that helps you find the best model or the best description of a data set.

Okay, in data science and industry, the goal often is centered around an algorithm, which becomes a data product.

So a data scientist in industry might be developing and deploying the algorithm, which means not only understanding the algorithm and its statistical performance but also all of the software engineering around systems integration.

Making sure that algorithm receives input that's reliable and has output that's useful, as well as I would say the organizational integration, which is how does a community of people, like the set of people working at the New York Times, integrate that algorithm into their process?

Interesting!

And I feel like AI-based startups are all the rage, and certainly within academia.

Are there connections between AI and the world of data science?

Absolutely! The algorithms that are there can connect those dots for you.

You're right that AI as a field has really exploded.

I would say particularly many people experienced a chatbot that was really, really good.

Today, when people say AI, they're often thinking about large language models or they're thinking about generative AI, or they might be thinking about a chatbot.

One thing to keep in mind is a chatbot is a special case of generative AI, which is a special case of using large language models, which is a special case of using machine learning generally, which is what most people mean by AI.

You may have moments that are what John Kythy called "look, no hands" results, where you do some fantastic trick and you're not quite sure how it worked.

I think it's still very much early days.

Large language models are still in the point of what might be called alchemy, that people are building large language models without a real clear a priori sense of what the right design is for a right problem.

Many people are trying different things out, often in large companies where they can afford to have many people trying things out, seeing what works, publishing that, and instantiating it as a product.

And that itself is part of the scientific process, I would think, too.

Yeah, very much!

Well, science and engineering, because often you're building a thing, and the thing does something amazing.

To a large extent, we are still looking for basic theoretical results around why deep neural networks generally work.

Why are they able to learn so well?

They're huge, billions of parameter models, and it's difficult for us to interpret how they are able to do what they do.

And is this a good thing, do you think, or an inevitable thing that we, the programmers, we, the computer scientists, the data scientists who are inventing these things, can't actually explain how they work?

Because I feel like friends of mine in industry, even when it's something simple and relatively familiar like autocomplete, they can't actually tell me why that name is appearing at the top of the list.

Whereas years ago, when these algorithms were more deterministic and more procedural, you could even point to the line that made that name bubble up to the top.

So is this a good thing, a bad thing that we're sort of losing control, perhaps in some sense, of the algorithm?

It has risks.

I don't know that I would say that it's good or bad, but I would say there's lots of scientific precedent.

There are times when an algorithm works really well, and we have finite understanding of why it works, or a model works really well, and sometimes we have very little understanding of why it works the way it does.

In classes I teach, I certainly spend a lot of time on fundamentals—algorithms that have been taught in classes for decades now, whether it's binary search, linear search, bubble sort, selection sort, or the like.

But if we're already at the point where I can pull up ChatGPT, copy-paste a whole bunch of numbers or words, and say, "Sort these for me," does it really matter how ChatGPT is sorting it?

Does it really matter to me as the user how the software is sorting it?

Like do the fundamentals become more dated and less important, do you think?

Now you're talking about the ways in which code and computation is a special case of technology.

Right?

So for driving a car, you may not necessarily need to know much about organic chemistry, even if organic chemistry is how the car works.

Right?

So you can drive the car and use it in different ways without understanding much about the fundamentals.

So similarly, with computation, we're at a point where the computation is so high level.

As you know, you can import PyTorch and you can go from zero to machine learning in 30 seconds.

It's depending on what level you want to understand the technology, where in the stack, so to speak.

It's possible to understand it and make wonderful things and advance the world without understanding it at the particular level of somebody who actually might have originally designed the actual optimization algorithm.

I should say, though, for many of the optimization algorithms, there are cases where an algorithm works really well, and we publish a paper, and there's a proof in the paper.

Then years later, people realize, actually, that proof was wrong, and we're really still not sure why that optimization works, but it works really well.

Or it inspires people to make new optimization algorithms.

So I do think that the goal of understanding algorithms is loosely coupled to our progress in advancing great algorithms, but they don't always necessarily have to require each other.

And for those students, especially, or even adults who are thinking of now steering into computer science, into programming, who were really jazzed about heading in that direction up until, for instance, November of 2022, when all of a sudden for many people it looked like the world was now changing, and now maybe this isn't such a promising path, this isn't such a lucrative path anymore—are LLMs or tools like ChatGPT reason not to perhaps steer into the field?

Large language models are a particular architecture for predicting, let's say, the next word or a set of tokens more generally.

The algorithm comes in when you think about how is that LLM to be trained or also how to be fine-tuned.

So the "P" of GPT is a pre-trained algorithm.

The idea is that you train a large language model on some corpus of text—could be encyclopedias or textbooks or what have you—and then you might want to fine-tune that model around some particular task or some particular subset of texts.

So both of those are examples of training algorithms.

I would say people's perception of artificial intelligence has really changed a lot in the last six months, particularly around November of 2022, when people experienced a really good chatbot.

The technology, though, had been around already before.

Academics had already been working with ChatGPT-3 before that, and GPT-2 and GPT-1.

For many people, it sort of opened up this conversation about what is artificial intelligence and what could we do with this and what are the possible good and bad.

Right?

Like any other piece of technology, Crensberg's first law of technology states that technology is neither good nor bad, nor is it neutral.

Every time we have some new technology, we should think about its capabilities and the good and the possible bad.

Right?

Like any other area of study, algorithms offer a spectrum from the most basic to the most advanced.

And even if right now the most advanced of those algorithms feels out of reach because you just don't have that background, with each lesson you learn, with each algorithm you study, that endgame becomes closer and closer.

Such that it will, before long, be accessible to you, and you will be at the end of that most advanced spectrum.

[Music]