Introduction to Algorithms
Transcript
- Welcome to this lecture on algorithms. My name is Richard Harvey and I am one of the professors here at Gresham College. I'm sponsored by the Worshipful Company of Information Technologists who have several missions. They're a charitable organization based here in the city of London, from where we're beaming this lecture. But among those missions is to promote the better understanding of IT. And hence the motivation for today's talk on algorithms, which are often thought of as the sort of fundamental heart of IT and computer science. So it's a strange word, isn't it, algorithm, algorithms. And Muhammed al Khwarizmi is generally credited with the word. A rather fascinating man. And as usual with ancient scientists, there's a little bit of controversy about him in the sense that lots of people sort of claim him as their own.
I've certainly heard the former President of Uzbekistan, Mr. Karimov wax lyrical on Muhammed al Khwarizmi. And I think he did that because he reputedly born in Uzbekistan. I have to say there's scant little evidence for this. People think he might've been, his name probably alludes to the city in which he was born, which was near Khiva in Uzbekistan. But if you live in Turkmenistan you might claim him as your own. And he didn't lots of his work in Baghdad, so he's Persian. Some people think he was a Muslim. Some people think he is Zoroastrian. None of those things matter very much.
And I think he was a brainy guy. If you're interested, there's a very nice documentary on him by Jim al-Khalili which takes you through some of his life and times. Anyway, one thing he certainly did do was introduce the idea of what is now called Arabic numbering. And I feel a little bit trepidatious about even calling it that, because what he did was he took Indian numbering and relabeled, rebadged it as it were as Arabic numbering and wrote it down.
And he also introduced methods for doing things in algebra. So he's now, let me check this, "Compendious Book on Calculation" and other things. It's a wonderful name of a book, isn't it? And he was chief at the House of Wisdom in Baghdad. So that's the origin of the name algorithm. It clearly, his name got Latinized to Mr. Algoritmi and then algorithm. If we go for a sort of dictionary definition of algorithm, you can see the history of the word here. So the first definition, which is an Arabic system of numbering, we don't use that anymore. It's a highly archaic algorithm is the alternative form of that. And in definition two, that's what we're interested in,
which is a procedure or a set of rules for doing something. And then number three, which is a set of rules for doing something in medicine, I think barely counts as a separate definition at all. And number one is definitely, quite okay. I would say however, that there is a close connection, and if we get time in this lecture, I'll try and draw it out. There's a close connection between numbering things, particularly formulae and algebraic equations, and algorithms and systems for doing things. Right at the very early sort of part of last century there was a lot of interest in formal methods for solving equations. And those formal methods turn out to be quite closely linked to some of the concepts that we're going to be looking at when we're looking at an algorithm. And actually that leads us to a sort of definition of an algorithm that we'll do.
I don't want to spend my whole lecture talking about definitions, but very early on in the sort of Hardy and Littlewood sort of era when people were thinking about what does it mean to prove something and what does algebra do for us, and all those sorts of things, there was interest in formal methods, as we would call them now for doing things. And so here is a definition of what one might call what is called an effective method.
And let's just have a quick look at it. So it says if we have a finite number of exact, finite instructions then when applied to a problem in its class an effective method always stops after a finite number of steps. It produces the correct answer. In principle can be done by any human, without aids except writing materials. And all you need to do is follow the instructions rigorously. So you might think of, back to your childhood when you think about that, if you were ever taught how to long division or solve a set of linear equations or quadratic equations using a method.
That was what people had in mind when they were talking about effective methods and proof. And there's this strong connection between proving things and algorithms. And one thing I think we should sort of deal with right at the beginning is that algorithms are, they're unambiguous. And unambiguous doesn't quite mean the same as deterministic. Deterministic means if you run it twice, you always get the same answer.
You are allowed algorithms that are non-deterministic. So if I gave you an example, I'm driving to Barnard's Inn from where we are beaming this lecture, which is in Central London, in the city of London from, say, Norwich, which is where I live. The route that you might take, particularly if you followed ways as I did here today will vary time on time but the end points are the same. So the algorithm for getting here might be a non-deterministic algorithm, in that it varies depending on conditions and other inputs but it has essentially the same structure. And that that is allowed within what we might call an algorithm. I would say that it's probably also worth saying, that algorithms originally were designed for humans.
And I talked about the early prehistory but very early on what we meant by computers was a bank of humans, human mathematicians, skilled mathematicians and even quite late in the last century computers were generally regarded as people. And they weren't sort of dumb automatisms, they were often critical to the success of a particular venture. So if you are a film fan, then you might've been, you might've watched "Hidden Figures." And "Hidden Figures" actually
is about the lives of some of these computers. And they were working in the NASA program, typically military and big prestige programs were the only programs basically rich enough to be able to afford computers. Obviously teams of human computers are an expensive thing to have. And here's a little clip of "Hidden Figures." - Maybe we've been thinking about this all wrong. - How's that? (phone ringing) - Maybe it's not new math at all.
- It could be old math. Something that looks at the problem numerically and not theoretically. Math is always dependable. - For you it is. (phone ringing) (suspenseful music) - Euler's Method. - Euler's Method. - Yes. - But that's ancient. - But it works. It works numerically. (suspenseful music) (papers flipping) (suspenseful music)
- That's it. - If only devising algorithms was really like that, strings and piano swelling in the background while Taraji P. Henson plays the life out of Katherine G. Johnson, who was one of the early mathematicians on the NASA program. Here's the original report that I found of her work dating from the 1960s. And the idea here was that there were these skilled mathematicians who are indeed going to, were developing some of the algorithms that we use today, in fact.
Films like "Hidden Figures" give rise to the idea that you need a lot of ingenuity to devise algorithms. And that's probably true but I don't want to emphasize, I mean, it's possible to overemphasize that fact particularly if you're torturing computer science undergraduates with algorithms courses. And algorithms courses usually are regarded as a bit of a battleground. I'll have to say for computer science undergraduates they're regard as very difficult and obscure. The truth of the matter is that a lot of problems turn out to be standard problems. And so there ought to be a book, isn't it? There ought to be a sort of there's a mental book, if you like of standard algorithms. And one of the most annoying things that computer scientists will say to you and indeed algorithmic specialist is, oh, well, this is a very well known algorithm, you know. Oh, well, that's just that problem. Ha ha ha, I solved that many years ago. And in a way that is, that's important
because without these standard algorithms, obviously we'd have to be reinventing everything from scratch. Now, slightly disappointingly, they're not as modular as they are in electronics. In electronics, as I'm sure you know a lot of components come in standard values. And the idea is that you plug them together and they make some standard hardware artifact. There's still quite a lot of customization, I'm afraid in computer science which allows for error and all sorts of trouble. But the fact remains is that there are some standard algorithms. And some of the algorithms are so standard that they're known to be very bad.
Let me give you an example. This is President Obama before he was president. And one of the things he did before he was president was rock around the tech companies in California, I think. And in this case, he decided to go to a relatively new company called Google. At this point, it was run by a guy called Eric Schmidt who's standing on the left, sitting on the left here. And what Eric decides to do is to ask one of the standard Google interview questions of President Obama to see if he knows his stuff. - What is the most efficient way to sort a million 32-bit integers? (crowd laughing) - Well-- (crowd laughing) - [Eric] Maybe, I'm sorry. Maybe I think that's not-- - No, no, no, I think, I think the bubble sort would be the wrong way to go.
(crowd laughing) - Okay. So that's a double joke because everybody in computer science knows that the bubble sort is a terrible way to sort a million 32-bit integers. And clearly Obama has either been prompted to say this or indeed, well he was a professor, so maybe he listened in in the computer science coffee room. And he knew that bubble sort was terrible. But let's just pick bubble sort. And I, as I pick it, I can hear, a hundred computer scientists on the line screaming do not talk about bubble sort.
There's a good reason for me. Please excuse me. There's a good reason for picking bubble sort. So let me explain how it works. And the principle behind the bubble sort, as you get your list of numbers and you go through them pairwise, and you say, are you bigger than this one next to it? And if you are, we'll swap you. And I was going to do some fancy animation to explain this, and then I discovered that a beautiful group of people based at, now, where were they? Yes. Sapientia University,
had created dances to explain the standard sorting algorithms. So just let me play you a clip from their YouTube channel which shows the dance for bubble sort. The things being sorted here are the players. And each one of them contains an integer that is stuck neatly, I think to their front and back. So you can see the value. And the idea of a sort is to put the small numbers over on the left and the large numbers over on the right. And you'll see above them somebody has super-imposed the array that contains these integers. So let's give it a go. (folk music)
So eight and seven are comparing. Seven needs to switch. (folk music) Okay. So you get the idea. You go through this thing pairwise and you swap their values. And on this slide, I've just shown these little pairwise swaps. So one of the questions that might arise is how long does this algorithm take to compute? Well, length is quite easy to compute. You just sit there with a stopwatch. What we might be interested in, however, is how does this algorithm scale as the problem gets bigger? Okay, now that's a critical question. So what we might ask is we say,
if we had N items in a list, so N begin numbers, how many steps would it take to complete, as a function of N? And we would call that the time complexity. It's usually the step complexity 'cause time, of course takes a different amount of time depending on the computer you're using. We might also ask, how much storage is required to do that? And that's called the space complexity. Now, generally speaking at the moment people are less interested in space complexity than time complexity.
So if a computer scientist said, he talks about complexity usually they're talking about time complexity by default. But if you want to catch them out, say, oh, you're talking about space or time complexity? That will show you to be at least a bit more of a wizard than most people. So the first thing that I hope is obvious from this example is in this example it would depend on the data, won't it? So if those numbers were already in order, zero one two three four five six seven eight nine then obviously we go through the list saying, do we need to swap? No. Do we need to swap? No no no no no no no no swaps, stop. That's how the algorithm works. So we went through the list N times. And so the best case, analysis in this case is N.
Now what about the worst case? Well, the worst case is when the data are in reverse order. Well, that case, so the nine is over here. So that has to swap with everything and that moves one position. And then we have to sweep through it again and again and again and again. So we have to do N sweeps through the data. And each one of those sweeps through the data takes N times. So that's N squared. So don't worry, I'm not gonna ask you to do a lot of these but you get the idea. The idea here is that this algorithm runs in N squared. So people would refer to that as order of complexity or time complexity N squared. Sometimes you'd hear people just refer to that as a polynomial complexity because of the N, as an example of a polynomial.
Okay. So that notation, the so-called Big-O notation can be used to show how the algorithm scales in time. So just to put some flesh on that I got out my little program and I wrote a program to take a thousand numbers. I took, sorry, I took however many numbers are listed on the bottom here. So maybe 50, a hundred, 150, 200, and so on. I created a thousand versions of that and sorted them using my very primitive bubble sort. And you can see here that this graph goes up with N squared. So it's got the worst case. I don't know if you can also see that there aren't many Ns. So this always seems to happen when you look at complexity,
which is the worst case. Well, that actually is most of the time, so most of the time this algorithm is N squared. There are better cases, but those better cases seem to arise infrequently. So this is a, what's called a Monte Carlo simulation where we try to run it many, many times and then see what the worst case is in this case. So the nice thing about order of complexity arguments is it sort of avoids computer top trumps as it were, where somebody says, well, if you ran it on this computer it would be so much quicker. It just reduces it to a scaling. I mean, there are plenty of criticisms of order of complexity, which I'll come back to. I mean, it's mainly that it's a bit primitive.
Now as I said, I chose bubble sort because it's quite easy to explain how the order of complexity works. You can sort of follow my argument. Nobody would use bubble sort for a real equation. That's why President Obama said, bubble sort will be the wrong way to go. What do people use? Well, Merge Sort is quite popular. This is, I think I've got a video here from the same guys on showing Merge Sort. (instrumental music) Now the idea behind Merge Sort is to split the data into smaller bits, sort the small bits, and then merge the sub lists into the big list. (folk music) (singing in foreign language) (folk music)
(singing in foreign language) (folk music) So here, we've got a list of two for ladies with four and two on there. (indistinct) and a list of three, for ladies with eight, six, and zero on their chest. And now having split it into lists, they're going to sort themselves by pairwise comparison. (singing in foreign language) And it turns out doing that is a lot quicker. And you avoid this problem or you mediate this problem of (singing in foreign language) mediate, you reduce this business of having to propagate the worst case. You know that number nine has to ripple all the way along the line. So that's how Merge Sort of works. One of the popular ones at the moment is called Timsort. So if you have an Android phone or you're using something programmed in Python or Octane or Swift or Rust, then Tim Peters Timsort is
the one for you. Recently found a small bug in it, actually. Not me, I mean, a small bug was found and fixed in the standard library. And it's most certainly not as bad as, order and you can get some of these things, they're N squared, you can get these things considerably below N squared. So there's a sort of, what's the word? A sort of hierarchy of complexity. Order N is marvelous. You just have to go through the data, once. Very few algorithms run or get order N. Good algorithms you tend to think Log N, N Log N, Polynomial. By the time you're going to the bottom of this list things are becoming, can become quite expensive. Now that informal understanding was put in one sort of neat package by what is called the Cobham Edmonds thesis.
And the Cobham Edmonds thesis basically says, there are two sorts of problems, really. There are nice problems and those are the problems that can be solved in polynomial time. And they sit over here in a set or actually just called P, and then there are nasty problems. And the nasty problems sit over here. Now Cobham Edmonds is, it's sort of become a bit misunderstood. What's it mean, it's talking about the scaling of the problem. How does it get worse as the problem gets bigger?
So it completely ignores the computer you're using, it completely ignores the, any constants in the order. So a million N squared is thought to be good even though two to the N, might in practice be better. Okay. It's all about the scaling. And of course it ignores the size of the input, but it's a very good rule of thumb that if something sits in P then it's an algorithm that you're not going to really have a lot of trouble with. And if it sits somewhere else, then there's a (indistinct) you have anxieties. So that's Cobham Edmonds, and you might say, well, what sits over here? You know, what sort of algorithms sit over here? The first thing to say, and I'll try and remember to say this again 'cause it's frequently misunderstood is many many practical algorithms do not fit in this easy, fast and practical subset called P.
They just don't. And we have to deal with them. It's a very common interview question, actually, undergraduates have it sort of drummed into them, that anything that isn't in P is a complete disaster, and the end of the known universe. And so what you do is you set someone to interview a sort of problem that is known not to be in P. And you hope that they, or well, you don't hope, if you're a sadist, you hope they crumble into a small box in a corner, and that sorts people out.
But lots of things are not in P. And I just thought I'd pick one, so we can understand a bit more about it. And the one I picked is TSP, The traveling Sales person Problem. And it's a problem that's quite easy to explain. It didn't use to be called TSP, it's been called a lot of things. Some people would call it a Hamiltonian graph. The idea is that you're a traveling sales person and you're in your Ford Mondeo and you have to visit these various sites, and you want to visit them only once. And you want to come back to where you started, that's the circular version of this, which I'm gonna talk about. And you want to absolutely go the minimum distance possible
in order to do that tour or circuit of all of the points. So the rules are you got to visit all the points. You're not allowed to visit them multiple times. And you want it to be short as possible. That's TSP. Well, the way computer scientists would always think about a problem that is, is to use a graph. And just because we're at Gresham College, I feel we can't use, you know, people often pick standard examples for this, the boring ones to do with Amazon deliveries, which are highly relevant, and so on. I've seen a very entertaining example based upon Abraham Lincoln's circuit when he was a Circuit judge in bits of the US,
which is highly appropriate, but we're at Gresham College and a former Gresham professor was Sir Christopher Wren. So I think we should pick the Wrenish example, which is what I've picked here. So I've picked 13 Churches. The 13 Churches that are in the city of London and are untouched from when the days of Wren. So Wren was a architect based in the city of London. He's a former Gresham professor and he had real good go at a great number of Churches in the city
but many of them got remodeled either by the planners or, by the (indistinct). So here's a set of 13 Churches and the closest are, if I could just check this, yes the closest are a staggering 104 meters apart. And the farthest are 2.9 kilometers apart which is pretty much the width of the city or diagonal along the city. So here's Sir Christopher, and he's sitting in the St Paul's Cathedral which is the most famous of his Churches. And he's saying,
well, I wonder what the best walk is around my Churches. And I'm only going to visit one at a time. Well, that's a reasonable sort of thing to ask. Now, those of you who are familiar with the city of London may indeed look at that map, as I looked at it this afternoon, when preparing my lecture and think, hang on, those are all in the wrong place. And there are in the wrong place because I realized I mixed up latitude and longitude. Sorry about that. So don't get obsessed about where these are. What I can tell you is that I did code. 'Cause I coded this myself as a wizard, disastrous of a mission, isn't it? I coded the distance between them as the Google Maps walking distance between the Churches and being sort of cautious sort of chap, knowing London, I took the average of going to and going from because if you walk to places in London there are usually different routes to get to places and different routes to get from places. Okay. So that's the basis of my description, right?
So let's think about this problem. Here's, Sir Christopher's starting at St Paul's. Let's just think about what we would call a Brute force search. So what he would do, he's at the 13th Church, St. Paul's and he says, well, I could go to either, or any one of these 12 Churches. There are 12 possibilities to go out that way and I'll get out my little pedometer and measure the number, the distance to all those 12. And then from each one of those 12, I could go to 11. And from each one of those 11, I could go to 10 and so on, right? So the number of possibilities, I think I wrote it down on this slide. Yes. Is 12 times 11, times 10 times da da da da da da. About 500 million possibilities.
Okay. Well, you could have that if you like, because I'm counting here sort of clockwise towards, which is different from anti-clockwise. If we said we don't mind, whether we go clockwise or anticlockwise, then it's half that, but it's still quite a lot. And it's too many for Sir Christopher with his rather primitive competing power to manage, but we can manage it, you know, half a million. It's not an amazingly difficult problem. So we could just crack on and find ourselves some sort of optimal route that would take us around these Churches. And this is, I hope it's the optimal route. Forgive me if it isn't, but my explanation survives whether it is the optimal route or not. And I found it to be about 4.4 kilometers. So it's a nice, nice, nice Sunday afternoon stroll around the city of London, looking at the the Wren Churches that are still surviving.
Okay. So far so good. I didn't tell you how I solved that, but just for the sake of argument let's assume I did it by brute force searching. I mean, I didn't, but let's just assume I did. Okay. But Wren produced, he did far more Churches than that, you know. So there are 52 in London at the moment. And so let's assume we took, what I'm I assuming here? Let's assume I took around a millisecond to search through, solve the problem with 13 Churches. How long is it gonna take me to solve the 52 Church problem? Well, 52 Churches that will be 52 times 51 times 50 times da da da da da da. And I'm putting a little exclamation mark next to it. 'Cause that's the symbol for factorial, which is the 52 times 51 times on, and the 12 factorial.
Rough stuff this. That ends up at around five times 10 to the 48 years using the same computer. So you might say, well, milliseconds is ridiculously slow. I mean, I could build some sort of super super hyper computer, sort of let's take that down to, I don't know, a femtosecond or attosecond or whatever, you know, some tiny tiny thing. It doesn't help very much, does it? It's still hideously impractical. And this is one of the problems with those non-polynomial problems. They go from being, I mean it seems completely unreasonable, doesn't it? The, what was it?
A small little problem, like going around 13 problems goes from manageable on my little PC to being hideously unmanageable ever on any PC. Okay. So what could we do? Well, one method is to use a sort of approximate algorithm. And this is very common when facing these very, very tricky, challenging complex problems like TSP. So here's one of the simplest I could I think of.
This is the Nearest neighbor algorithm. And what you do with Nearest neighbor is you're standing at some poles. You look around to the one that you think is closest. You move to that. And then you move to the next Nearest neighbor that you haven't visited already. And if you do that, I end up with about a five kilometer route. And there it is comparing it to the optimum route. Well, I could start anywhere and try that. And it turns out, now I think the worst case is if I start at St. James' and that gives me about 5.3 kilometers. So that's, and the best version is starting at St Peters, which gets down to 4.7 kilometers. Now, if you think about the optimal, which I think was 4.4 kilometers, these are generating what we would call bounds. Okay. And bounds are quite useful in mathematics and computer science. There are sort of indication
of the worst. What's the worst that could happen? So in this case, I'm saying that the length, if the length of a nearest neighbor solution I'm calling LNN and the optimum solution is L* then the optimum is bound here by the nearest neighbor. And this is an upper bound. An upper bound is what's the worst that can happen? And we also have lower bounds. Can't do better than, sort of bounds. And both of those concepts come up quite a lot in algorithms. What happens is mathematicians and complexity theorists will simplify the problem in a way that allows them to work out what's the best you could do, and what's the worst you can do?
And when you've got to deal with algorithms that you, things that you can't compute that's incredibly helpful because that tells you you say, well, I know I haven't got the optimum but I know I'm within this distance of it. And I can sort of illustrate that for us graphically, for TSP. So this is a sort of a graphical illustration of what's happening with TSP. And on the bottom here, on the x-axis, I've got computation steps. So as we move across to the right,
things are taking longer and longer and longer and longer, and probably should go off to something horrendously infinite. So brute force is indicated right on the far right here. That was what I was talking about at the beginning of this little segment. And then on the top left, I've got these very fast algorithms, which are not very optimal and they're the nearest neighbor algorithm. So that's where I just pick the nearest place and move to it. And now so far, I don't actually know what the optimum is. So if in reality, you know, I can't run, if I can't run the brute force algorithm I don't know what the optimum is. I'm in a pretty tricky position. I've got these nearest neighbor algorithms, which I know are bad, but I don't know how bad they are. Well, usually with a problem like this you've got a more sort of complex set of algorithms to have a go at. So let me just put some of them on. So there is in fact it turns out an exact algorithm.
It's the Held-Karp algorithm. It still has rather nasty complexity and squared two to the n. So that's not polynomial, it's a bad algorithm, in that sense but it does give you the exact answer. So if you've got a small toy problem you might be able to use Held-Karp to get the answer. And in fact that's what I did use with my small problem. I actually used, well, I think I used it. To be perfectly honest, I used Google OR-Tools, which are a public domain library that solve routing problems.
You can download it and try it yourself. And it uses a mixture of exact algorithms and approximate algorithms depending on how complex the problem is. I don't remember it telling me which algorithm it used which was annoying, but it didn't. So what about approximate algorithms? Well, the most famous one is the Christofides algorithm, recently renamed actually the Christofides Serdykov algorithm, because somebody else seemed to have discovered it at the same time.
And that algorithm doesn't give the optimum but it gives it close enough. So the Christofides algorithm is bound from above by one and a half times the optimum. So you can see where we're going with this, I'm sure. Which is, well, the good thing about Christofides algorithm is it's got good enough complexity that we can use it on real problems. Yeah. Big, many, many hundreds of nodes and so on. That's great. And it gives an answer that we know isn't worse than one and a half times the optimum. So you can look at the answer you've gotten to, and well, I can tell you the optimum isn't better than the scaling of the answer I just got. So that's quite handy. And that happens quite a lot in the al, in the space of the world of algorithms. So we can have approximate algorithms, preferably with known approximateness, or we can have exact algorithms which might be impossible to compute.
And the strange thing about a traveling sales person problem really is the dates. So in computer science, everything changes all the time. What's astonishing to me about this is that a Held-Karp algorithm dates from 1962. I mean, it's prehistoric. No one's found a better one since then. And the approximate algorithm, Christofides, it dates from 1976. You know what I mean? We were all wearing flare trousers in 1976 and didn't have mobile phones. It's amazing. Now that was true, actually. All of that was true. When I started writing this lecture about a month ago, and during this lecture, it became not true. So there's recently been a new algorithm published by Karlin, Klein and Gharan. And they have just snuck in under the Christofides algorithm.
It's a modification of the Christofides algorithm. So the question you're asking me, I haven't, I have to admit, I didn't check the complexity, but I, the paper looks pretty similar to modification to Christofides. So I suspect the complexity is very similar. Write it in the comments if you know what it is, because somebody might know but it is slightly better in terms of its approximation. Now then, how much better is it? You're asking me, you're craving to know the answer, aren't you?
It is that distance between 3L*/2. And that little dot is 10 to the minus 36. So it's ludicrously close. Nevertheless, we're all very excited about it because it does introduce a new approach. And maybe it's gonna crack open to give something a little bit more exciting than a 10 to the minus 36 improvement. So these algorithms, which are important, can be quite resistant. So I've introduced standard algorithms there. And I've talked about a couple standard algorithms and sorting,
which are attractable and an intractable algorithm, which is TSP. There are many intractable algorithms, I just picked one. I haven't really talked about what goes inside an algorithm. And that's partly because I'm gonna be talking about programming in a later lecture. And in there, I should talk about some of the standard control structures. One that you might be familiar with, I suspect is the idea of iteration. And computers do this thing, and they do it again and again and again. That's called iteration. One that we've come across already which I should've mentioned when we came across it was the idea of recursion. And recursion is a structure where the algorithm essentially calls itself with different parameters. And we met that already, when I talked about factorial which was the exclamation mark symbol. One way of defining factorial N is to define it in terms of factorial N minus one. So you could define factorial N as N times factorial N minus one, and so on.
A rather beautiful definition. One word of warning, early on in the literature, the word recursion which now has this technical meaning about calling itself. There lots of computer science recursion jokes, by the way, if you want to look them up. Recursion at some point in the literature meant just doing things in a computery way. Okay, so if you go back to the sort of 1920s and 30s, recursion was used to mean something differently.
I haven't talked about how we represent algorithms. I may have studiously avoided it, actually but let's quickly talk about that. It's a bit of a mess to be perfectly honest. I mean, in the early days people used a flowchart and I've showed one on the right hand side. The one on the right-hand side lots of people are credited with flowcharts. The Frank and Lillian Gilbreth were, developed probably the flowchart. They used it for modeling processes of human activity, time and motion studies from the 1920s, 1921. And then some people say, John von Neumann the great computer scientist who invented everything did flowcharts. I mean, I had a look at his flowcharts, and I think really they're much closer to what we would call now a signal-flow graph. So I think I'm gonna just rule him out court for that claim. Not because he's not the most brilliant man,
as I said, he invented almost everything else. The first example of the flowchart I can find that looks like a modern flowchart it is this example here, this is taken from the papers of Grace Hopper. Grace Hopper was a computer pioneer and venerated in the US quite rightly. And in her papers, there's this flowchart. And it's by a programmer called Margery, Margery K League, who was an interesting person. This is a flowchart for
comparing three numbers. And Margery League also worked on the first program to try and predict American elections, which has got a distinguished programming structure. Flowcharts have gone out of fashion. I think there isn't a good explanation why they've gone out of fashion, but here's my theory, my theory is that they were jolly difficult to draw on early computers. When we had it for early computers graphical stuff was really difficult to do so it was a real hassle to do flowcharts.
And we all had these little bits of plastic. If you were programming, you had these sort of little plastic stencils which had all the flowcharts symbols and we would spend ages drawing, and we didn't like it. So people moved away from flowcharts, which personally I regret because they were well-defined. There's a standard list of symbols and they sort of crossed languages and cultures very well. And now we've moved to what's called pseudocode or sometimes structured English algorithms. And it's just a list of do this, do that.
I'm not very keen on pseudocode myself, although we all use it. And because it's not defined, I mean, there isn't an international standard of pseudocode, well, there is a sort of structured English standard, but it's a bit of a muddle and no one uses it. So there's a sort of need I think, to be more formal about algorithms in that definition. And that is not to say that we're not formal about algorithms in how they connect to computer science and the sort of, the towering genius really of doing that was the guy on the right, Alan Turing. Alan Turing was a student of Alonzo Church on the left. Alonzo Church developed something called Lambda calculus which is an algebraic way of representing algebra about algebra. And so it's very useful for algorithms and functions because it's an algebra that describes functions. And what Turing did in this paper
was he wasn't interested in computers, specifically. Actually what he wanted to do was solve this decision problem, as it's called the Entscheidungsproblem. And the Entscheidungsproblem is a problem in mathematics, which is given a formula, can you show that it is derivable from the axioms? Okay. It sounds easy, doesn't it? But you know, so in principle, can you do that? So if I define a set of axioms, an algebra that works on those axioms can I show with a formula, that's a valid formula,
can I show how to derive it from the axioms? And the answer by the way turns out to be a very surprising one, which is no, you can't. And mathematics went into a great big crisis for a bit when they discovered this negative answer to Hilbert's problem. And we've seem to have forgotten about it for a bit. But that's what Turing was interested in. In order to do that, he developed a general model of a computer called a Turing machine, or a Universal Turing machine as we would call it now. So the surprising fact is he did all this in 1936 which is a sort of staggering thought because this was long before computers were a practicality. Well, the best way to explain a Turing machine really is people usually at this stage get out sort of arbitrary pencils and paper and start drawing it. But some dude on YouTube has built one.
So I'm sorry I've taken off the audio track. 'Cause it was a little bit, it takes a while to explain it but I think you can get the idea here. And what we've got here is a tape. And on the tape the machine is allowed to write either a one or a zero. It's allowed to rub off the digit, and it's allowed to move the tape to the left. The right will stay where it is. So it's a very, very simple sort of model for a Turing machine. And this one is programmable by an SD card.
You put an SD card in the side and it follows a set of programs. And there are various models for programming these things. There's a good model thought up by Tibor Rado where you have sort of ID cards which you program and their popular when you're teaching undergraduates about these things. In a Universal Turing machine the program is also on the tape. So you can program, and it can modify itself and you can do all sorts of things. The idea is that all algorithms that you could think of can be simulated by the Turing machine. And that idea that for every algorithm there is an equivalent Turing machine. And for every Turing machine there is an equivalent algorithm, is known as the Church-Turing thesis. And it's probably the basis of modern understanding of theoretical computer science. Okay. So that's one way of getting precise about algorithms which is this connection with Turing machines
and Turing machines are very easy to understand and define. This has no practical consequences whatsoever. I think, I mean, nobody apart from guys who, like me who are interested in Turing machines and undergraduates who are made to program Turing machines. They're not practical computers, they're sort of thought computers that allow you to think about what any computer does. And any computer can be modeled as a Turing machine. So the other thing you can do to formalize things is to formalize really the problem a bit. And computer scientists have been quite good at this, so they call them classes of problems. And the one that devotes people a little bit, a lot of attention, gets a lot of attention is called a Decision problem. A Decision problem is something that has a yes, no answer. So you might think of, well, not everything is a decision problem. Lots of things can be converted to decision problems.

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