Video lecture
Watch on YouTube(opens in a new tab)Sub topic 15
Choosing the Right Level of Detail
This chapter assumes you have read the earlier chapters. You should be comfortable with Euclid's GCD algorithm, its pseudocode, and the idea of a basic step. Every description you have written so far made a choice about how much to say. This chapter makes that choice deliberate.
One Algorithm, Several Descriptions
A level of detail is the size of the steps a description treats as basic. A step is basic when the reader can carry it out without further explanation. Here is Euclid's algorithm written at three levels:
| Level | Description of Euclid's algorithm | Suited to |
|---|---|---|
| High | Repeatedly replace (a, b) with (b, a mod b) until b is 0; the answer is a. | A colleague who knows GCD |
| Middle | The `while` loop pseudocode with `r <- a mod b` | A programmer about to implement it |
| Low | Every step spelled out, including how to compute a remainder | A reader or machine with no `mod` operation |
None of these is the "correct" one. Each is correct for a particular reader.
Detail Depends on What the Reader Can Execute
The effectiveness property says every step must be something the executor can actually perform. That makes the right level of detail a question about the executor. If your target language has no remainder operator, a mod b is not a basic step, and you have to expand it:
def gcd_no_mod(a, b):
# Precondition: a >= 0, b >= 0, not both zero
while b != 0:
r = a
while r >= b: # compute a mod b by repeated subtraction
r -= b
a, b = b, r
return a
print(gcd_no_mod(48, 18)) # 6The algorithm has not changed. One step was refined into smaller steps. Stepwise refinement is this process of replacing a step with a more detailed sub-description until every step is basic for the intended executor.
Detail Also Changes Cost Analysis
The level of detail affects how many steps you count. At the middle level, each loop iteration costs a constant number of steps. At the low level, computing a mod b takes up to a/b subtractions, so the same iteration is no longer constant. When you state a Big-O bound, say which operations you counted as basic.
Common Errors
- Too vague. "Find the GCD somehow" hides the algorithm completely. If two readers could do different things, the description fails definiteness.
- Too detailed. Writing out memory addresses or loop counters for an audience that only needs the idea buries the key insight, which is that the pair (a, b) shrinks to (b, a mod b).
- Mixed levels. One step says "sort the list" and the next spells out an index increment. Keep the steps of a description at roughly the same size, and refine them in a separate sub-description.
- Language leakage. Pseudocode that depends on Python features such as
enumerateor negative indexing ties the description to one program, which defeats the point of separating algorithm from program.
Exercise
You are describing linear search for two audiences: a product manager reviewing a feature, and a student writing it in assembly language. Give a one-line description for the manager, and name one step the student's description must expand that the manager's does not.
Show answer
For the manager: "Check each item in order and report the position of the first one equal to the target, or report that none matches." The student's description must expand steps such as "check each item in order" into explicit index initialization, increment, and a bounds comparison against the list length, because assembly has no built-in loop over a list.
Exercise
Under the middle-level description, Euclid's loop body counts as O(1) steps. Why might an analyst using gcdnomod reach a different running-time bound?
Show answer
In gcdnomod, computing the remainder takes about a/b subtractions instead of one basic step, so each iteration's cost depends on the values. The basic operation changed, so the step count and the Big-O bound change with it.