AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Efficient algorithms

Week 8 · Unit 4: Be a designer · 3 lessons
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.

goal
Worked examples
Algorithm 1 (8 steps)Algorithm 2 (6 steps)Algorithm 3 (5 steps)
StartStartStart
Move 1Move 1Move 3
Move 1Move 2Turn right
Move 1Turn rightMove 2
Turn rightMove 2Stop
Move 1Stop
Move 1
Stop
The robot must go from the start to the flag: 3 forward, turn right, 2 forward. Here are three algorithms for the same route. All three work. Algorithm 3 is the most efficient, with 5 steps.
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.
star
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.
StepAlgorithm A
1Start
2Move 1
3Move 1
4Move 1
5Move 1
6Stop
2
The robot starts facing up and must reach the flag. Write the most efficient algorithm.
flag
3
Which is more efficient? P: Start, Move 2, Move 2, Move 2, Stop. Q: Start, Move 6, Stop. Both reach the same place.
4
Is the algorithm with the fewest steps always the best one? Why?