1
Many ways, one goal
Computing 3: Unit 4, pp. 60–78
GoalCompare algorithms that solve the same problem, count their steps and say which is the most efficient.
New words
solution · a way of solving a problemefficient · using the fewest steps to do the jobcompare · to look at things and see how they are the same or different
Explanation
There is often more than one way to solve a problem. Three different algorithms can bring the same robot to the same place. They are all correct, but they are not all efficient. To compare them, count the steps of each one. The algorithm with the fewest steps that still does the job is the most efficient. An efficient algorithm is quicker to write and quicker to check. It must still lead to the goal, so check the end place first.
Worked examples
| Algorithm 1 (8 steps) | Algorithm 2 (6 steps) | Algorithm 3 (5 steps) |
|---|---|---|
| Start | Start | Start |
| Move 1 | Move 1 | Move 3 |
| Move 1 | Move 2 | Turn right |
| Move 1 | Turn right | Move 2 |
| Turn right | Move 2 | Stop |
| Move 1 | Stop | |
| Move 1 | ||
| Stop |
Efficient means few instructions, not a fast robot. Two algorithms bring a child to the door. “Walk 1 step”, written 10 times, has 12 steps with Start and Stop. “Walk 10 steps” has only 3 steps.
The robot must reach the star, but the square with the cross is blocked. It cannot go along the second row. So the algorithm goes along the bottom row: Turn right, Move 2, Turn left, Move 1. Always check that the path is clear before you count steps.
Class activity
Three ways (with a friend): choose a place in the room, such as a door. One of you walks there while the other writes three algorithms for the same journey: one step at a time, one with some steps joined, and the shortest you can. Count the steps in each. Follow each one to check that it really leads to the door.
Practice
1
Count the steps in Algorithm A. Then write a shorter one. A: Start, Move 1, Move 1, Move 1, Move 1, Stop.
| Step | Algorithm A |
|---|---|
| 1 | Start |
| 2 | Move 1 |
| 3 | Move 1 |
| 4 | Move 1 |
| 5 | Move 1 |
| 6 | Stop |
Algorithm A has 6 steps. A shorter one is: Start, Move 4, Stop (3 steps).
2
The robot starts facing up and must reach the flag. Write the most efficient algorithm.
Start. Turn right. Move 3. Stop. (4 steps.) The robot faces up, so it must turn right first.
3
Which is more efficient? P: Start, Move 2, Move 2, Move 2, Stop. Q: Start, Move 6, Stop. Both reach the same place.
Q. It has 3 steps, and P has 5.
4
Is the algorithm with the fewest steps always the best one? Why?
No. It must also reach the goal correctly. An efficient algorithm has the fewest steps that still do the job.