7.03e–7.03i - Efficiency and complexity
Two algorithms can reach the same answer with very different amounts of work. You will compare specified operation counts, calculate best, worst and average costs, recognise the hierarchy of growth orders, and use polynomial, logarithmic and exponential models to estimate running times.
Comparing a specified kind of work
Algorithm A needs operations for an input of size , while algorithm B needs . Can the quadratic algorithm be quicker? Yes: at , the counts are and . Later we will find exactly when that advantage disappears. First, make sure you are comparing the same kind of work.
You already know
An algorithm has precise instructions and a stopping condition. Its order describes dominant growth for large inputs; it does not give the exact operation count for a particular input.
A comparison tests two values against each other; a swap exchanges two stored values; a pass is one specified traversal of part or all of the data. These are different measures. Fewer passes need not mean fewer comparisons, and fewer comparisons need not mean fewer swaps.
Consider two algorithms searching for a target that appears exactly once. Algorithm L inspects a list from the left; algorithm R inspects it from the right. Each stops immediately on finding the target. Count one comparison for each entry tested against the target. These search instructions are not in the formula booklet.
Worked example 1: comparing searches on one input
Question. Algorithms L and R search the list for . Compare their efficiency using the number of comparisons.
Plan. Write the entries inspected in order, stopping at the first match in each direction.
Why this step? Entries beyond the match are not tested, so the list length alone does not determine this count.
Answer. L uses comparisons and R uses . R is more efficient by this measure for this input.
Check. The target is fourth from the left and third from the right; .
Worked example 2 changes only the target. The algorithms stay the same, but the better result can reverse.
Worked example 2: a different target
Question. Compare L and R when searching the same list for .
Plan. Count the entries inspected from each end until is reached.
Why this step? Efficiency in a particular case depends on where the required information lies, not just on the algorithm's name.
Answer. L is more efficient for this input: comparisons.
Check. The two positions satisfy , again one more than the list length.
This does not establish that either direction is better on every possible input. A claim about a specific list is narrower than a claim about general growth.
Exam tip
If a question specifies comparisons, give comparison counts and use them in the conclusion. A statement about passes or a general order does not answer that comparison.
The next list puts the target near one end, so count from the correct starting point in each algorithm.
Counting the worst case
A maximum cost must allow the input that makes the algorithm do the most work, even if that input is unusual.
Worst-case time complexity
For a specified problem size , is the maximum run-time, or maximum count of a specified basic operation, over permitted inputs of that size. An operation count can be used as a model of run-time when its cost is treated as constant.
A search for the largest entry by scanning a list always needs comparisons: the first entry starts as the current largest, and each of the remaining entries is compared with it. A search for a target might stop sooner, but its worst case is comparisons if the target is last or absent. Neither count is printed in the formula booklet; derive it from the instructions.
Nested loops need more care. “For each” below means carrying out the inner instruction for every stated integer value. Count only the stated comparison, not the loop administration.
Worked example 3: comparing distinct pairs
Question. An algorithm has these instructions: for , compare entry with each entry for . Find its comparison count and order.
Plan. Count comparisons for each value of , then sum the resulting integers.
Why this step? The inner loop does not run times for every ; its length grows from to .
Answer. , of order . Every input has this count, so it is also the worst-case count.
Check. For , .
Worked example 4 changes only the inner-loop endpoint. Including adds one comparison for each outer-loop value.
Worked example 4: including the diagonal
Question. The algorithm instead uses . Find its comparison count and order.
Plan. Replace the inner counts by .
Why this step? The additional comparisons change the exact count but do not change its dominant power.
Answer. , again of order .
Check. At , the new count is , exactly more than .
The sum formula is recalled from the standard-sums lesson. It is not a separate formula for nested loops: the instructions decide what must be summed.
Watch out
Wrong: two loops automatically mean exactly operations. Right: count the inner work for each outer value. Two full loops each of length give ; triangular ranges give a different exact count, though they can have the same order.
For the next algorithm, the extra work is specified explicitly rather than being hidden in the loop bounds.
Best, worst and typical cases
“Typical” needs a model for which inputs occur; averaging the best and worst counts works only when that averaging is justified.
For a target appearing once in a list of length , left-to-right search has best count , worst count , and count when the target is in position . If all positions are equally likely, each of those counts has probability .
Mean search cost for equally likely positions
If a target is present exactly once and is equally likely to be in any of positions, the mean comparison count is
This result depends on both the search rule and the stated distribution of target positions.
Not in the formula booklet: learn it.
The average is over inputs, not over repeated runs of a deterministic algorithm on the same input. The same input follows the same path each time.
Worked example 5: averaging all positions
Question. A list contains entries and one target. Its position is equally likely to be any of the six positions. A left-to-right search stops at the target. Find the best, worst and mean comparison counts.
Plan. List the possible counts and average them with equal weights.
Why this step? The target's position determines the number of comparisons, so the mean position is the mean cost.
Answer. Best , worst , mean comparisons.
Check. Pair the positions: , giving mean . A mean need not be an integer.
Worked example 6 changes only the assumption that the target is certainly present. An unsuccessful search must inspect every entry.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account