Algorithms

Version 21

What is an algorithm?

In the beginning of the course, I explained the algorithm as meaning a plan or procedure for solving a problem. Now it is time to be a little more exact, to understand the term in the sense that computer scientists study algorithms.

  1. An algorithm is a step by step procedure. Howewver, we've learned in our study of scratch that sequence is only one of the three ways of putting steps together: we also have conditional and repetition control structures.

  2. The steps of an algorithm are so clear, definite, and precise that a machine could execute them: they are mechanical steps.

Example

The factorial function: fact(n) = n! = 1 × 2 × ... × n. For example, 5! = 1 × 2 × 3 × 4 × 5 = 120.

Here's an algorithm for computing n factorial:

  1. Input n
  2. Set product to 1
  3. Set m (the multiplier) to 2
  4. Repeat until m > n:
    1. set product to m × product
    2. change m by +1
  5. Output product

Comments

What do computer scientists want to know about algorithms?

Given an algorithm which is claimed to solve some kind of problem, a computer scientists will generally want to know two things about it:

  1. Is it correct? For obvious reasons. Lots of programs are buggy. Some computer programs are among the most complex systems made by human ingenuity. If we can prove an algorithm is correct, we eliminate one source of bugs.

  2. How efficient is it? How many processing steps, or how much memory, or now much network throughput, does it require?

Summary

An algorithm is a step-by-step process for solving a problem.

Incidentally

The word algorithm comes to us from the Persian mathematician al-Khwarizmi, who, around 825 A.D., wrote a book on Calculation with Hindu Numerals. It was translated into Latin in the 12th century, with the title Liber Algoritmi de numero Indorum. Algoritmi was the Latinized form of the author's name, but but came to mean the subject of his book.

Gregory Weber


  1. Revision log:
    • Version 2.1, 2010 Nov 16. Corrected course number I211.
    • Version 2, 2010 April 19. Shortened, simplified, and related to Scratch.
    • Version 1, 2008 March 18.