What is algorithm analysis?
An algorithm is a list of steps that solves a task. Analysing an algorithm means studying it carefully: what does it do, and what can happen?
The data you give at the start is the input. What comes out at the end is the result (also called output). To see what an algorithm does, you trace it: you do each step by hand and write down the value after each step.
From input to result: tracing one case
Take the algorithm: read x, multiply it by 2, add 3, give the result.
- Input x = 4
- After step 1: 4 ร 2 = 8
- After step 2: 8 + 3 = 11
- Result: 11
Write the values in a small table. A table shows quickly where something went wrong. If the algorithm has a choice (if x is bigger than 10 ...), say which branch you took.
All possible results
Often we ask: what results can this algorithm give? First find out which inputs are allowed (for example whole numbers from 0 to 9). Then trace every input and collect the results.
For x ร 2 + 3 the results are 3, 5, 7, 9 ... 21. Every one is odd, because x ร 2 is always even and adding 3 makes it odd. So you can say an even result is impossible without testing every input. Spotting such a pattern is the real skill of analysis.
Which input gave this result? Going backwards
To find the input from a result, undo the steps in reverse order. The forward steps were ร 2, then + 3. So backwards: first โ 3, then รท 2.
Result 11: 11 โ 3 = 8, 8 รท 2 = 4. Input was 4. Check by going forward again: 4 ร 2 + 3 = 11. Always check.
If going backwards gives a number that is not allowed (a fraction, a negative number, a number outside the range), then that result is impossible. Result 10: 10 โ 3 = 7, 7 รท 2 = 3.5, not a whole number, so impossible.
When two inputs give one result
Algorithms with a choice can send two different inputs to the same result. Take: if n is more than 10, result = n โ 10, otherwise result = n ร 2. Input 2 gives 4. Input 14 gives 4 too. So from the result 4 alone you cannot tell the input. You must list all inputs that could give it.
Key formulas and definitions
- Forward: input โ step 1 โ step 2 โ result
- Backward: result โ undo last step โ undo first step โ input
- Opposites: + and โ, ร and รท
- Always check an answer by running the algorithm forward
Worked examples
1. Algorithm: take x, multiply by 2, add 3. Find the result for x = 7.
7 ร 2 = 14, then 14 + 3 = 17. The result is 17.
2. The same algorithm gave the result 21. Which input was used?
Go backwards. 21 โ 3 = 18, and 18 รท 2 = 9. Check: 9 ร 2 + 3 = 21. The input was 9.
3. Can the same algorithm give the result 12 for a whole-number input?
12 โ 3 = 9, and 9 รท 2 = 4.5. That is not a whole number. So 12 is impossible. In fact every result is odd, so no even number is possible.
4. Algorithm: if n > 10 then result = n โ 10, else result = n ร 2. Find the result for n = 7 and n = 25.
n = 7 is not more than 10, so result = 7 ร 2 = 14. n = 25 is more than 10, so result = 25 โ 10 = 15.
5. For the algorithm in the last example, find every input that gives the result 4.
Branch 1 (n โค 10): n ร 2 = 4, so n = 2. This is โค 10, so it is valid. Branch 2 (n > 10): n โ 10 = 4, so n = 14. This is > 10, so it is valid. The inputs are 2 and 14.
Common mistakes
- Undoing the steps in the same order. Backwards means the last step is undone first.
- Forgetting to use the opposite operation, for example subtracting when the step was also a subtraction.
- Not checking whether the input found is allowed (whole number, in range). If it is not, the result is impossible.
- Thinking one result means one input. With branches, two or more inputs can give the same result.