Lite / Discovering How the World Works / 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?
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.
Predict
You tell a robot: 'Go to the flag.' The robot only understands Forward 1, Turn left and Turn right. What does it do?
- Finds its way to the flag
- Nothing: 'go to the flag' isn't one of its instructions
- Moves forward until it hits something
Guess first, then reveal
Nothing at all. A machine can only do the exact instructions it understands. 'Go to the flag' has to be broken down into a list of steps it knows, in the right order. That list is an algorithm.
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.
What the interactive shows (text version)
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).
Watch (optional)
Watch for: 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.
“What's an algorithm? - David J. Malan” by TED-Ed (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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
Intuitive
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.
Technical
A computer has a fixed set of basic instructions (its instruction set). Every program, however clever, is ultimately a sequence of these. Algorithms describe how to combine them to solve a problem.
Applied
A dishwasher runs an algorithm: fill, heat to 55 °C, spray for 30 minutes, drain, rinse, dry. Engineers test each step precisely because a vague step, like 'heat until hot', can't be built.
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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?
Reflect
Where in your day do you follow an algorithm without thinking about it?
Write your answer on paper or in your own notes. Lite never stores what you write.