Introduction to Algorithms/What is an Algorithm

This is a lesson in the course, Introduction to Algorithms, which is a part of The School of Computer Science

Objective edit

Our main objective is to learn what an algorithm is and its basic components/concepts. By the end of this you should be able to recall what an algorithm is and what it does.

Definition of Algorithm edit

Merriam-Webster Online defines "Algorithm" as " a step-by-step procedure for solving a problem or accomplishing some end, especially by a computer" [1]. The dictionary definition is missing the component that the procedure must be finite, that is, there has to be a condition for the termination of the algorithm.

Examples:

1. One example would be knitting socks.

The first step is to cast on stitches. Step-by-step the rows are knitted, the heel turned, and finally the last row cast off. The algorithm specifies how to do the sock, but each sock knitted is different, as it uses different wool, but all are similar in structure to each other.

2. Another important example would be sorting a hand of cards.

There are multiple ways of doing this, but one method is to take the lowest card and move it to the leftmost (or rightmost) position. Next, find the second lowest card and move it to the second leftmost position. Repeat until the cards are sorted.

Everyday example:

Here are perhaps slightly whimsical examples of an algorithm.

Algorithm for making buttered toast (1.0)
  1. Get a loaf of bread.
  2. Cut one slice from end of the loaf of bread.
  3. Put slice of bread into toaster.
  4. Turn toaster on.
  5. Wait for toaster to finish.
  6. Put toasted bread onto a plate.
  7. Spread butter on toast.
Brushing teeth
  1. grab tooth paste
  2. take off the lid
  3. grab tooth brush
  4. put a dab of tooth paste onto the tooth brush
  5. put lid back onto tooth paste
  6. turn on the sink
  7. wet the tooth brush
  8. turn off the sink
  9. brush all tooth surfaces consistently with circular motion
  10. when complete, turn on sink
  11. fill a small cup with water
  12. turn off sink
  13. rinse mouth
  14. spit into sink
  15. turn on sink
  16. rinse tooth brush and sink
  17. turn off sink
  18. put toothbrush and tooth paste away
  19. throw away disposable cup
  20. shut all drawers/cabinets

As can be seen, the algorithm is a set of steps that can be followed in order to achieve a result.


Basic concepts edit

This simple example already contains many components commonly found in most algorithms:

  • Instructions. Instructions are the heart and soul of any algorithm. An algorithm will consist of a series of sub-algorithms, each performing a smaller task. Extracting individual digits from the numbers to be added may well be an entire task in itself. Some instructions, like digit addition, can be considered fundamental instructions which cannot be broken up further; all algorithms can eventually be broken down into these instructions, like objects being composed of atoms.
  • Variables. temporary and carry are both temporary values used to store additional information the algorithm needs to function correctly or effectively. Their values can vary as the algorithm progresses, hence their name.
  • Conditionals. Some algorithms may need to make a decision somewhere in it. If the sum of the current two digits is 10 or larger, 1 will be carried over to the next digit position, otherwise nothing (ie. zero) is carried. Conditionals allow an algorithm to selectively execute instructions based on certain conditions which must be satisfied.
  • Looping. The for each...end for instruction means to run certain instructions for each object in a set, in this case for each digit in a number. This is a special case of looping, the act of repeating a set of instructions while (or until) a goal is reached or a condition is satisfied; in this case while there are digits left (or until there are no digits left). Loops can contain any other instructions, including other loops!
  • Subalgorithm. An algorithm may not be able to do all the work on its own. Usually, a large, complex algorithm can be broken up into parts, each perfoming a specific task. Smaller algorithms are easier to reuse, for example the for each ... digit selection algorithm could be reused in subraction, multiplication and division algorithms, and so on. Breaking up algorithms into logical parts also make it easier to analyze their behaviours and properties from a mathematical point of view.
  • Performance. While initial algorithms may seem to function, you may discover that they may not be the best choice in the long term. For example, a simple bubble sort may work for a small amount of items, but if you try it on a large amount of data, it will take a massive amount of time. An algorithm with better performance would include the Merge Sort.


Assignments edit

Your assignment is to create a simple algorithm. If you need help then you can go back and look at the example above of an algorithm.

How to make a bowl of cereal 1. grab a bowl from the cupboard 2. grab a spoon 3. choose a cereal 4. grab a cereal 5. pour the cereal into the bowl 6. grab the milk 7. pour milk into the bowl 8. put cereal and milk away 9. Enjoy!

Completion status: this resource is a stub, which means that pretty much nothing has been done yet.