What is an algorithm?
I recently held a presentation covering why there is currently such a hype about artificial intelligence. I knew I was not going to present to technical experts so I prepared my slide deck as such. Yet, someone from the audience shot a question at me that stunned me:
What is an algorithm?
My answer to this was the standard reply, which is somewhat technically correct: It is a sequence of instructions telling a computer what to do. But already while saying this, I felt I had lost at least that audience member. It got me thinking afterwards: Why wasn’t that helpful? And what would have been a better (read: more helpful to someone without any technical experience) answer?
So here is my idea about how to explain this to someone, who has never seen a computer science lecture or anything related:
What is an algorithm?
Imagine you would like to bake a cake. How would you do that? I don’t bake cakes very often and prior results often were filed under the “failed” category.
But still, if I was about to start doing it, I would probably go and look for a recipe on Google. Since I have heard the name of Martha Stewart before, I consider her an expert in the cake business and will use her recipe for Chocolate Bundt Cake.
A recipe like this is usually split in two sections: ingredients and a list of steps that you need to do to receive your cake in the end.
Once you have gathered all the ingredients, you walk through the steps:
The steps come with detailed instructions that usually consist of telling you at what point to do with which ingredient in what amount. If you do everything in the correct order, with the correct amounts and the correct timing, you’ll end up with cake.
Well, now we have cake. Though this is nothing to complain about, what does all of that have to do with algorithms?
Algorithms are basically like recipes for cake. As with recipes, they usually take some kind of inputs, perform a set of operations on them and generate output.
There are certain key differences though:
The main difference is of course that here not a human is reading the instructions and carrying them out, but a computer.
Since a computer cannot read Martha’s beloved cake recipe easily, we need to make this a little easier. For that reason, programming languages were created. There is a whole bunch out there, with different uses. Prominent ones are Python, Java or C++.
Input to an algorithm is usually data. This can be anything that can be made digitally available (meaning a computer can somehow get access to it): camera images or video, text someone wrote into a box on a website, a Facebook post, information from a sensor, results from a prior algorithm, etc.
Operations are more basic than “butter a pan”. Programming languages are very precise and work differently than our human languages. On the one hand, this is very cool: it enables us to describe to a computer basically everything! On the other hand though, this means we also HAVE to describe everything we want a computer to do very clearly. So the operations look something like “multiply this input number by 2”, “output the text ‘GIVE ME CAKE’ into the textbox 3 on the website” or “move the robot arm to the position specific in the prior instruction”. Instructions in computing languages are called “source code” or simply “code”.
Cooking or baking recipes seldom have conditions. A condition is basically a an intersection, where the computer has to check something and then act based on the result. For example it can check what time it is and then act differently (e.g. to provide a dark background on your screen at night and a bright one during the day). Conditions are very important in algorithms and are usually written in an IF this THEN that style.
To give you a concrete example, of what an algorithm looks like, I am going to steal one from the great Pedro Domingos: it is an algorithm of how to play Tic-Tac-Toe. I am not going to write it in source code, because that would complicate things unnecessarily. If you have been living on a different planet than Earth up until now, Ti-Tac-Toe is a little game where you have 9 squares. 2 players alternate in marking an X and an O in an empty square with the goal to get 3 X’s or 3 O’s in a row.
To show you how the algorithm works, I made a short movie where you can watch it in action! The opponent plays X and is a little stupid (maybe his algorithm is not as smart as ours?). X goes first. Our algorithm then of course plays O and goes second. On the left side, you can see the instructions for the algorithm. Here, the conditions part is important, because for each instruction, the computer first checks whether the condition is met (e.g. whether a square is still free). If the condition is NOT met, the computer goes to the next instruction. Think about it as if the computer is trying to find the right instruction to execute before doing so. Once it found the right one, it makes it’s move.
If you think now that everything is an algorithm, then you might be right! Basically anything can be described in an algorithm, if you know how it works! There are in fact very important algorithms everywhere at work every day that have a bing influence on your life! Here are some examples to give you an idea:
Google Search: The algorithm to find websites relevant to what you entered and to present them in a sorted list that is the most useful to you!
Netflix Recommendations: Whenever Netflix shows you recommended shows or movies, an algorithm has looked through what you already watched and liked and selected something for you!
Encryption: Your bank transfers, important WhatsApp communications and many other things that need to be protected.
Facebook Automatic Image Tagging: If Facebook finds your face in pictures someone else uploaded, it can tag you automatically!
So, that’s it! I hope I was able to describe the idea and principle of algorithms a little to you. Feel free to have a look at the Technical Books Section here on my website where you can find books that go a lot deeper than this. And I’m off to make some cake now…