Sub topic 9
Tracing an Algorithm by Hand
This chapter assumes you can read the Euclid GCD pseudocode from the previous chapter and that you know what a mod b computes. Tracing means running an algorithm yourself, on paper, one step at a time. It is the quickest way to check that a description says what you meant it to say.
What a Trace Records
At any moment, an algorithm's state is the current value of every variable it uses. A trace records the state after each step, or after each pass through a loop. The usual format is a state table: one column per variable, and one row per moment you want to capture.
Before you start a trace, decide two things:
- Which variables to track. Track every variable that changes, plus any value a condition tests.
- When to write a row. For loops, write one row before the first iteration and one row after each iteration ends. Then the last row shows the state at the moment the loop exits.
Worked Example: GCD(48, 18)
Here is the pseudocode again:
function GCD(a, b)
while b != 0 do
r <- a mod b
a <- b
b <- r
end while
return a
end functionTrace it with a = 48, b = 18:
| Row | a | b | r | Test `b != 0` |
|---|---|---|---|---|
| start | 48 | 18 | – | true |
| after iteration 1 | 18 | 12 | 12 | true |
| after iteration 2 | 12 | 6 | 6 | true |
| after iteration 3 | 6 | 0 | 0 | false |
The loop exits when the test becomes false, and the algorithm returns a = 6. Since 48 = 6 × 8 and 18 = 6 × 3, and 8 and 3 have no common factor, 6 is correct.
Checking a Trace with Code
If you have an implementation, you can print the state at the same points and compare it with your table:
def gcd_traced(a, b):
print(f"start: a={a}, b={b}")
step = 0
while b != 0:
a, b = b, a % b
step += 1
print(f"after iteration {step}: a={a}, b={b}")
return a
print(gcd_traced(48, 18))If the printed rows don't match your hand trace, either the code or your reading of the pseudocode is wrong. Finding out which one is the point of the exercise.
Common Errors When Tracing
- Updating too early. Copy the old values before a step, compute every new value from them, and only then fill in the row.
- Skipping the exit test. Write down the test result for every row, including the one that ends the loop. Off-by-one errors show up there.
- Tracing only easy inputs. Also try edge cases:
b = 0at the start,a < b, ora = b. WithGCD(18, 48), the first iteration swaps the two values. A trace makes that visible.
Exercises
Exercise
Trace GCD(18, 48) using a state table with columns a, b and r. How many iterations run, and what happens in the first one?
Show answer
Rows: start (18, 48); after iteration 1 (48, 18, r=18); after iteration 2 (18, 12, r=12); after iteration 3 (12, 6, r=6); after iteration 4 (6, 0, r=0). Four iterations run, and it returns 6. In the first iteration 18 mod 48 = 18, so the step just swaps a and b.
Exercise
Trace linear_search([7, 3, 9, 3], 3), recording i, x and the result of x == target on each iteration. What does it return, and how many comparisons does it make?
Show answer
i=0, x=7, false; i=1, x=3, true, so it returns 1. It makes 2 comparisons and never looks at the second 3.