Computational Problems, Algorithms and Cost Model
This note fixes the language used for the whole course: what an algorithm must do (the problem), what it does (the algorithm), whether it does it right (correctness) and how much it costs (complexity). Pseudocode follows the usual RAM-style conventions; <- is assignment, // a comment, and a phrase between asterisks *...* is a meta-command that is not coded (to be used as little as possible).
Algorithm and computational problem
Definition (algorithm). An algorithmA finite, deterministic procedure: a sequence of elementary steps of a computational model. is a finite deterministic procedure, a sequence of elementary instructions of a given computational model, that maps an input to one and only one output. Set-theoretically it is a function
The computational model abstracts a computer and its instruction set (here: the RAM, described with pseudocode).
Definition (computational problem). A problem is an abstract specification of the relation between a set of instances and a set of solutions : The relation must be
- total: ;
- not necessarily single-valued: several may satisfy and .
A problem is a logical object (a relation); an algorithm is a function, so when an instance has several solutions the algorithm picks one.
Definition (algorithm solving a problem). solves if , and
In reality and are encodings of and compatible with the model (e.g. binary strings for integers); the distinction matters only when it affects size (see below).
Examples of problems
- Sum of two integers. , , . Here is a function.
- Reachability in a directed graph. ; a path is , , with (the degenerate path has no edges). , = sequences of nodes plus the empty sequence meaning "no path". contains for every path from to and if none exists: not single-valued.
- Sorting. With the Kleene star (all finite sequences, empty one included), and
- Square roots of a nonzero complex number. , solution = the ordered pair of roots, , . Not single-valued: for both and are solutions.
Analysis of an algorithm
Two questions: correctness and complexity (time, sometimes space).
Correctness. Prove , i.e. the input-output function agrees with the specification.
Main tool: induction on the size for recursive algorithms (see Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →), invariants for iterative ones.
Cost model. A cost model assigns each instruction of the model a non-negative cost . The cost of running on is .
In this course costs are unit or zero: only the instruction deemed dominant is counted, because only the order of growth matters. The choice must be meaningful:
- integer arithmetic in binary: elementary bit operations;
- arithmetic on matrices of complex numbers: arithmetic operations between complex numbers;
- sorting: comparisons between keys.
Size. The size of an instance is a non-negative integer reasonably representative of the size of its encoding. It is chosen case by case: total number of bits for two integers; for a sequence of items; or for an matrix (both describe it well).
Let . Since varies within , it is summarized by a function :
- worst case: (the default: it bounds every run of size );
- best case: ;
- average case: for a fixed distribution on .
Example (linear search, , unit cost per comparison). Best case , worst case (item last or absent), average if the item is uniformly placed.
Example (the size of an integer matters). Testing whether is prime by trial division takes about divisions. In terms of that is a square root, but the size of the binary encoding is bits, and the cost becomes : exponential in the input size.
What to specify
To analyse always state: (1) which instructions have unit cost; (2) what the size is; (3) which complexity is studied (default: time, worst case). Exact counts (e.g. comparisons for the maximum of items) hold only in the stated model. The next step is obtaining for recursive algorithms, i.e. writing and solving a recurrence: Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →.
Versione ripasso
- Algorithm = finite deterministic function . Problem : relation total () and not necessarily single-valued. solves if , and : it extracts a function from a relation. are encodings of .
- Examples: sum (a function); reachability (path from to or , several paths, so not single-valued); sorting (: same length, ordered, permutation); square roots of : , , solutions , not single-valued because the pair is ordered.
- Correctness: ; induction (recursive) or invariants (iterative).
- Cost: with costs or on dominant instructions (comparisons for sorting, complex operations for complex matrices, bit operations for integers). Size : for sequences, or for matrices, number of bits for integers.
- Cases: worst (default), best , average . Linear search on items: , , .
- Integer size: trial division steps for bits: exponential in the size.
- Always specify: unit-cost instructions, size, type of complexity.
- Common mistakes: calling a function; choosing for the two roots; counting every instruction instead of the dominant ones; confusing the value with its size .