Skip to content
Tactiverge

School/◇ Explorer/Week 20

What is an algorithm?

You could teach a friend to make a sandwich in ten words. Could you teach a robot in ten words?

About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator

1.Why should I care?

The 'think' part of every intelligent machine is an algorithm. Sat-navs, washing machines and traffic lights all run them. If you can write a clear algorithm, you can tell any machine what to do; if you can't, the most powerful computer in the world is useless.

  • ◇ Define an algorithm as an exact, ordered, finite list of steps.
  • ◇ Explain why machines need far more precise instructions than people.
  • ◇ Write a short algorithm that a literal robot can follow.

2.Predict

You tell a robot: 'Go to the flag.' The robot only understands Forward 1, Turn left and Turn right. What does it do?

Guess first. Being wrong here is how the surprise works.

3.Discover

Build a program from Forward, Turn left and Turn right that takes the robot to the flag without hitting a red wall. Press Step to watch it run one instruction at a time.

Instruction robot

Write a program of exact instructions to move the robot from the bottom-left square to the flag without touching a wall. The robot follows the list in order and never guesses what you meant.

    The program is empty. Add instructions, then press Step or Run all. The robot starts bottom-left, facing north.

    Can't use the interactive? Read a description instead

    The grid robot can be replaced by this description: a 6-by-6 grid, robot at the bottom-left square (0, 0) facing north, flag at (4, 4), walls at (1, 1), (2, 1), (3, 1), (3, 3) and (2, 4). One safe program is: Forward, Forward, Turn right, Forward, Forward, Forward, Forward, Turn left, Forward, Forward (10 instructions).

    4.Watch

    → Notice how each step is exact enough that anyone, or anything, would get the same answer by following it.

    Counting a room full of people two at a time shows how a clever, exact procedure beats a slow one.

    Length: see YouTube · captions on YouTube · source

    Nothing loads from YouTube until you press play.

    5.Understand

    An algorithm is a recipe for a machine

    An algorithm is a list of steps that solves a problem. To count as one, the steps must be:

    • Exact: each step means one thing only.
    • Ordered: you know which step comes next.
    • Finite: it finishes.

    Machines take you literally

    A person fills gaps with common sense: 'butter the bread' means use a knife, on one side. A machine has no common sense. If a step is vague, it either can't do it or does something surprising. Writing algorithms is the skill of leaving no gaps.

    Big steps are made of small ones

    'Go to the flag' is too big for the grid robot. 'Forward 1, Forward 1, Turn right, Forward 1…' uses only steps it knows. Breaking a big job into steps the machine already understands is called decomposition, and it is how all programs are built.

    Explain it another way

    Giving directions to a tourist works because they can ask questions and look around. Writing an algorithm is like writing directions for someone who can't ask, can't look around and follows every word exactly.

    Worked example 1

    Write an algorithm for the grid robot to go from (0, 0) facing north to (2, 1).

    One answer: Forward 1 (now at 0, 1), Turn right (facing east), Forward 1 (1, 1), Forward 1 (2, 1). Four exact steps, in order, and it finishes. Another correct answer goes east first: Turn right, Forward 1, Forward 1, Turn left, Forward 1. Several algorithms can solve the same problem.

    Common mix-ups

    • An algorithm has to be written in code.
      ✓ An algorithm is the list of steps. You can write it in plain words, a flowchart or code.
    • There is one correct algorithm for each problem.
      ✓ Usually there are many. Engineers choose between them.

    6.Practice

    Practice, not a test. Wrong answers get a hint and another try.

    Practice 1

    Which of these is NOT a good algorithm for a robot?

    Practice 2

    'Step 1: Forward 1. Step 2: go back to step 1.' What is wrong with this as an algorithm?

    Practice 3

    Two learners write different programs that both take the robot to the flag safely. Which is true?

    Practice 4 · a fresh version every time

    A robot starts at (0, 0) facing north on an open grid (east is +x, north is +y). It runs: turn left, turn left, turn right, forward 1, turn left, forward 1. What is its final x (east–west) coordinate? Use a negative number for west.

    7.Apply it

    Write an algorithm a robot could follow to tidy three books onto a shelf, using only these steps: move to (place), pick up, put down, turn. Swap with a friend and act it out exactly as written. Where did they have to guess?

    8.Reflect

    Concepts: Algorithms, searching and complexity