AA4–AA6 - Efficiency, correctness and heuristics

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 nn. Worst-case complexity considers inputs of size nn requiring the greatest amount of that work.

Order notation such as O(n2)O(n^2) describes a growth bound for large nn. In ordinary polynomial-order comparisons, report the dominant growth: constant factors and lower-power terms do not change that classification.

For example, 3n2+7n+23n^2+7n+2 has quadratic order O(n2)O(n^2). That does not mean it performs exactly n2n^2 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 CC starts at zero. For each i=1,…,ni=1,\ldots,n, an inner loop runs for j=1,…,ij=1,\ldots,i, increasing CC by one each time. The procedure outputs CC. Count these counter increases when n=5n=5, and give the order for general nn. Do not count loop-control tests.

Plan. Count the inner-loop executions for each value of ii, then sum.

C(5)=1+2+3+4+5=15C(n)=1+2+⋯+n=n(n+1)2=12n2+12norder=O(n2)\begin{aligned} C(5)&=1+2+3+4+5=15\\ C(n)&=1+2+\cdots+n=\frac{n(n+1)}{2}\\ &=\frac12n^2+\frac12n\\ \text{order}&=O(n^2) \end{aligned}

Why this step? The inner loop has a different length for each ii. Multiplying nn by the first inner-loop length would miss most of the work.

Answer. 1515 counter increases for n=5n=5; quadratic order O(n2)O(n^2).

Check. The exact count lies below n2=25n^2=25, but its leading term is still proportional to n2n^2.

Change only the inner-loop upper bound, from ii to nn. 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 j=1,…,nj=1,\ldots,n for every i=1,…,ni=1,\ldots,n. Count counter increases for n=5n=5, and give the general order.

Plan. Multiply the number of outer iterations by the fixed number of inner iterations.

C(5)=5(5)=25C(n)=n+⋯+n⏟n terms=n2order=O(n2)\begin{aligned} C(5)&=5(5)=25\\ C(n)&=\underbrace{n+\cdots+n}_{n\text{ terms}}=n^2\\ \text{order}&=O(n^2) \end{aligned}

Why this step? Both loop ranges grow with the input, so their work multiplies rather than adds.

Answer. 2525 counter increases for n=5n=5; order O(n2)O(n^2).

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 T(n)≈cnpT(n)\approx cn^p, with the same constant cc, multiplying the size by kk multiplies the predicted time by kpk^p:

T(kn)T(n)≈kp\frac{T(kn)}{T(n)}\approx k^p

If that procedure is run nn times on size-nn inputs, its total modelled time is proportional to np+1n^{p+1}.

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.]
The growth power applies to the input-size multiplier

Worked example 3: scaling one quadratic run

Question. A procedure has a quadratic timing model. One run on a problem of size 5050 takes 0.20.2 seconds. Estimate the time for one run on size 150150, on the same machine.

Plan. Find the size multiplier and square it.

k=15050=3T(150)≈0.2(32)=1.8 s\begin{aligned} k&=\frac{150}{50}=3\\ T(150)&\approx0.2(3^2)\\ &=1.8\,\mathrm s \end{aligned}

Why this step? A quadratic model squares the size multiplier; multiplying the time merely by three would use a linear model.

Answer. Approximately 1.81.8 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-nn procedure once for each of nn starting positions.

Worked example 4: quadratic work repeated n times

Question. Using the same timing model, estimate the total time at size 150150 when the procedure is run 150150 times. State the total order as a function of nn.

Plan. First use the time for one run, then multiply by the number of runs.

Ttotal(150)≈150(1.8)=270 sTtotal(n)≈n(cn2)=cn3total order=O(n3)\begin{aligned} T_{\mathrm{total}}(150)&\approx150(1.8)=270\,\mathrm s\\ T_{\mathrm{total}}(n)&\approx n(cn^2)=cn^3\\ \text{total order}&=O(n^3) \end{aligned}

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 270270 seconds, with cubic total order O(n3)O(n^3).

Check. At size 5050, the repeated task takes 50(0.2)=1050(0.2)=10 seconds. Increasing the size by three multiplies this total by 33=273^3=27, giving 270270.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account