Friday, August 24, 2012

Algorithms

The joke goes that when George W. Bush found out that colleges were teaching their Computer Science majors about Algorithms, he complained about unequal representation and asked that they also teach GeorgeBushisms as well. But what exactly are Algorithms? Why do computer scientists learn about them? What do they, in fact, do?

Imagine you are about to do something. This "something" could be a very simple task, like opening your internet browser, or something more complicated, like baking a cake. How do you go about doing it?
If you think about it, all processes have certain steps, which need to be carried out, usually in a precise sequence, in order to be completed. Opening your browser can, for example, be achieved by moving your mouse until the cursor is pointing to the relevant icon, then double-clicking. Performing these steps out of sequence probably won't get you very far, unless you're lucky.

Algorithms are nothing but step-by-step instructions for performing a task. We usually use the word to denote a mathematical or computational task, but, in fact, the instructions for opening your internet browser is also an algorithm.

Now, let's take a different task. Take a piece of paper and a pen. Draw a straight line. Now draw a second line, which starts wherever the first one ended and goes in a different direction. Draw a third line, starting from the same point, but in a different direction. Now connect the remaining three end points with a curve.
Did you get the Mercedes-Benz logo? Probably not. The reason for this was that my instructions were extremely imprecise. I did not tell you how to orient the lines. I did not tell you what angles to make between them. I did not tell you how long to make them. I did not tell you to make the final curve a circle. All these omissions most likely made your drawing something completely arbitrary, not resembling the mentioned logo at all. An algorithm cannot be vague like that. Each instruction must be precise enough, so that whenever someone attempts to perform the same task under the same conditions must get the same result. (With the exception of using randomness, like rolling a die).

When it comes to computers, every piece of software is a sequence of instructions to the computer, telling it what to do, from moving numbers around, to accepting some sort of input, to controlling your enemies in a video game. These instructions are in the language of the computer itself, so there is no longer a problem about vague instructions. So, why is there a need to study algorithms? There are two main reasons. The first one is that, even though a set of instructions can be precise, this doesn't mean that it will do what is intended. The study of algorithms helps us understand ideas like data flow, prerequisites and programme correctness. The second reason is that the same task can often be performed in many different ways. Instead of using a mouse to double-click on the correct icon, you may also be able to open your browser by going into a command prompt and type the appropriate commands to get to the correct folder and run the correct file and so on. Sometimes, one of the ways of performing a task is better (faster, more efficient, less prone to error and so on) than another. However, which method is better is not always clear. This is where algorithm analysis helps. We can analyse a given set of instructions in terms of the time it takes, the possible errors it may encounter and even its correctness, before even running the programme itself.

No comments:

Post a Comment

Keep it civil and keep it relevant.