Algorithms
Created | Updated Oct 27, 2006
An algorithm is a logical series of actions for performing a task. In life there are algorithms all around us; recipes, stereo manual, wardrobe assembly instructions, et. In fact, a computer program is simply an algorithm for a computer. In general, an Algorithm presents itself to us as a set of instructions which can be executed in order to achieve some goal.
So, you can say that the algorithm for washing your hands is:
- Go to sink
- Turn on water
- Run hands under water
- Pick up soap
- Rub soap on hands
- Put down soap
- Put hands underneath water
- Rub hands against each other
- Turn off water
Algorithms can also be transferable, in other words the same set of actions can be performed on different things, for example the action required to plug in and turn on a TV are technically the same actions required to plug in and turn on any electrical device. Therefore you can make an algorithm transferable by specifying a variable rather than a specific thing. So to make the last algorithm transferable to the washing of any article rather than just hands you specify a variable like this:
- Go to sink
- Turn on water
- Run [article to be washed] under water
- Pick up soap
- Rub soap on [article to be washed]
- Put down soap
- Put [article to be washed] underneath water
- Rub [article to be washed]
- Turn off water
How Do You Construct an Algorithm?
When you want to create an algorithm for a task you are basically wanting to note down the logically ordered series of actions and conditions that define how to perform the task. So to help, an algorithm for creating algorithms has been created.
One - Clearly Define the Task
If you are going to create an algorithm you have to have a clear understanding of what it is you have to define. So the best thing to do in this first stage is to note down what items are affected by this task and what areas this task covers.
Two - Define What Actions are Involved in the Task
Go through the task in your head and note down every action you can think of that's associated with the task. Try to define as many as you can think of, but don't worry if you miss any as we can pick them up later. Also don't worry about the order, we sort that next.
Three - Define the Order of These Actions
Go through your list of actions and see which actions have to be done before each action you look at, then make a note of it.
Four - Turn This into the Algorithm
Now take the list and construct an ordered list starting with the first action and building based upon which actions have to be done first. It is important to note that the more clear, simple, and unambiguous the instructions are, the better the algorithm. And it is always important to remember your audience, and what they are likely to understand (be the audience human or a computer).
Five - Are the Actions Clear?
It is always good to ask 'What knowledge would I need to understand this?' and 'Does the audience have this knowledge?' This is because quite often when we write instructions, we write as if we were explaining to someone with the same basic knowledge and understanding of concepts as ourselves; this can cause us to write very bad algorithms.
Six - Run Through the Algorithm
Now create an example case and run through your ordered list. You are trying to find actions that have to happen but aren't defined, no matter how small.
Seven - Note Things Missed and Rewrite Algorithm
This is important; in most cases the first version of an algorithm will omit something which you feel is obvious, but which may not be obvious to the reader.
Eight - If Corrections Have Been Made, Return to Point Five
By correcting and checking until you can see no more corrections is a very useful way to write an algorithm. This is because it allows you to pick up any errors, to make sure it is sufficiently clear, and be sure that your final version works to the best of your knowledge. This is known as iterative development.
Where Did it Get its Name?
The name 'algorithm' comes from the translation of the 9th Century Persian mathematician al-Khwarizmi's name1. He was in fact the mathematician who brought the Hindu decimal system and use of zero into Arabic mathematics amongst other things.