The Role of Algorithms and Machine Models

Algorithms · Advanced Placement

The Role of Algorithms and Machine Models: the big picture

An algorithm is a well-defined computational procedure that transforms an input into an output, and the central questions about one are whether it is correct and what resources it consumes. Correctness means that for every legitimate input the algorithm halts with the right output, which for an iterative algorithm is usually established through a loop invariant: a property shown to hold before the loop begins, to be preserved by each iteration, and to imply the desired result when the loop terminates. Resource consumption is measured against the input size, and because real machines differ enormously in constant factors, analysis is conducted in a machine-independent model. The random-access machine model assumes a sequential processor with unit-cost arithmetic, comparison and memory access on words of bounded size, which captures enough of real hardware to make predictions useful while remaining simple enough to reason about. Running time is counted as the number of primitive operations, expressed as a function of input size, and distinguished into best, worst and average cases; worst case is usually reported because it provides a guarantee and because for many algorithms the average case is asymptotically no better. Problem size must be defined carefully: for sorting it is the number of elements, but for numerical problems it is the number of bits, which is why some apparently fast number-theoretic algorithms are in fact exponential.

Algorithms, Correctness & Loop Invariants

An algorithm is a procedure that turns an input into an output, and there are only two questions worth asking about one: is it correct, and what does it cost? Correctness for a loop is almost always established the same way — find a property that holds before the loop, is preserved by every iteration, and tells you what you want when the loop stops. That is a loop invariant, and it is induction wearing different clothes. Cost is measured against input size in a machine-independent model, because real machines differ by constant factors that would otherwise swamp the comparison.

{{illustration:algos-l01-f1}}

{{illustration:algos-l01-f2}}

Key relations

- Running time as a function of input size. T(n) = ∑ᵢ cᵢ (times line i executes) ; T_worst(n) = max_(|x| = n) T(x)

Machine Models, Input Size & Problem Encoding

What counts as one operation, and what counts as the size of the input, are choices — and getting them wrong invalidates an analysis. The random-access machine gives unit cost to arithmetic and memory access on bounded words, which is reasonable for sorting but misleading for arbitrary-precision arithmetic. Input size is the length of the encoding, which for a graph is vertices plus edges and for an integer is its bit count. That last point explains why trial division, linear in the value being tested, is exponential in the input and why factoring is hard.

{{illustration:algos-l01-f3}}

{{illustration:algos-l01-f4}}

Key relations

- Encoding length and complexity. |n|_binary = lfloor log₂ n rfloor + 1 ; T(n) = O(√(n)) = O(2^(|n|/2))