AA4–AA6 - Efficiency, correctness and heuristics
An algorithm needs more than a correct-looking answer on one input. You will count its work, compare how that work grows, distinguish a proof from a collection of tests, and explain why a useful heuristic need not guarantee an optimal solution.
Count the specified operation before giving an order
Can an algorithm pass a hundred tests and still be wrong? Yes: testing gives evidence about the cases tested. Before deciding what has been established, separate three questions: does the procedure work, how much work does it need, and does it find the best answer?
You already know
Algorithms use initial states, sequential updates, conditions and stopping rules. Trace the given instructions faithfully. A finite sequence of successful examples does not by itself prove a claim about every integer.
To compare efficiency, state what measures the problem size and what operation is being counted. For sorting, the size might be the number of items and the operation a comparison. For a nested loop, it might be an execution of the loop's main instruction.
Complexity and worst case
Algorithmic complexity describes how a measure of the work grows with the problem size . Worst-case complexity considers inputs of size requiring the greatest amount of that work.
Order notation such as describes a growth bound for large . In ordinary polynomial-order comparisons, report the dominant growth: constant factors and lower-power terms do not change that classification.
For example, has quadratic order . That does not mean it performs exactly operations, or that every quadratic algorithm is faster than every cubic algorithm for a small input. Constants and implementation details still affect actual timings.
Worked example 1: a triangular nested loop
Question. A counter starts at zero. For each , an inner loop runs for , increasing by one each time. The procedure outputs . Count these counter increases when , and give the order for general . Do not count loop-control tests.
Plan. Count the inner-loop executions for each value of , then sum.
Why this step? The inner loop has a different length for each . Multiplying by the first inner-loop length would miss most of the work.
Answer. counter increases for ; quadratic order .
Check. The exact count lies below , but its leading term is still proportional to .
Change only the inner-loop upper bound, from to . The order may stay the same even though the exact count changes.
Worked example 2: a rectangular nested loop
Question. Now the inner loop runs for for every . Count counter increases for , and give the general order.
Plan. Multiply the number of outer iterations by the fixed number of inner iterations.
Why this step? Both loop ranges grow with the input, so their work multiplies rather than adds.
Answer. counter increases for ; order .
Check. The rectangular loop performs more increases than the triangular loop, but both have quadratic dominant growth.
The arithmetic-series formula is supplied in the formula booklet; these algorithm-specific counts and complexity interpretations are not.
Watch out
Wrong: call two successive loops quadratic merely because there are two loops.
Right: successive loop counts add; nested loop counts may multiply. Count the specified operation using the actual ranges.
Change the triangular loop's inner range once more.
Scale the problem and count repeated runs
For large problems on the same machine, an operation count often supplies a useful timing model. State the model: an order classification alone is not a promise of an exact number of seconds.
Polynomial timing model
If the running time is modelled by , with the same constant , multiplying the size by multiplies the predicted time by :
If that procedure is run times on size- inputs, its total modelled time is proportional to .
Not in the formula booklet: learn it.
Use the size variable specified in the question. Doubling the number of vertices and doubling the number of edges are not interchangeable descriptions of a network problem.
[DIAGRAM: asset_name: Input and timing multipliers; asset_slug: h645_b110_polynomial_scaling; caption: The growth power applies to the input-size multiplier; description: Exact graphs on input-size multiplier k from one to four show linear time multiplier k in sage, quadratic multiplier k squared in blue and cubic multiplier k cubed in clay. At input multiplier four, the time multipliers are four, sixteen and sixty-four. The axes are labelled input-size multiplier and predicted time multiplier.]

Worked example 3: scaling one quadratic run
Question. A procedure has a quadratic timing model. One run on a problem of size takes seconds. Estimate the time for one run on size , on the same machine.
Plan. Find the size multiplier and square it.
Why this step? A quadratic model squares the size multiplier; multiplying the time merely by three would use a linear model.
Answer. Approximately seconds.
Check. The size is three times as large and the predicted time nine times as large.
Change only the task: instead of one run, apply the size- procedure once for each of starting positions.
Worked example 4: quadratic work repeated n times
Question. Using the same timing model, estimate the total time at size when the procedure is run times. State the total order as a function of .
Plan. First use the time for one run, then multiply by the number of runs.
Why this step? The number of repetitions grows with the problem size. It is not a fixed constant that can be ignored in the order.
Answer. Approximately seconds, with cubic total order .
Check. At size , the repeated task takes seconds. Increasing the size by three multiplies this total by , giving .
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account