Salta al contenuto
Note per Studenti Computational Problems, Algorithms and Cost Model Report

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 A:I→O.A : \mathcal{I} \to \mathcal{O}.

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 II and a set of solutions SS: Π⊆I×S,i Π s  ⟺  (i,s)∈Π.\Pi \subseteq I \times S, \qquad i \,\Pi\, s \iff (i,s) \in \Pi. The relation must be

  1. total: ∀i∈I, ∃s∈S:i Π s\forall i \in I,\ \exists s \in S : i \,\Pi\, s;
  2. not necessarily single-valued: several s1≠s2s_1 \ne s_2 may satisfy i Π s1i\,\Pi\,s_1 and i Π s2i\,\Pi\,s_2.

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). AΠ:I→OA_\Pi : \mathcal I \to \mathcal O solves Π\Pi if I=I\mathcal I = I, O=S\mathcal O = S and ∀i∈I:AΠ(i)=s ⟹ i Π s.\forall i \in I : \quad A_\Pi(i) = s \ \Longrightarrow\ i \,\Pi\, s.

In reality I\mathcal I and O\mathcal O are encodings of II and SS 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. I=Z×ZI = \mathbb Z \times \mathbb Z, S=ZS = \mathbb Z, Π={((a,b),c):c=a+b}\Pi = \{((a,b),c) : c = a+b\}. Here Π\Pi is a function.
  • Reachability in a directed graph. G=(V,E)G = (V,E); a path is ⟨v1,…,vk⟩\langle v_1,\dots,v_k\rangle, k≥1k\ge1, with (vj,vj+1)∈E(v_j,v_{j+1})\in E (the degenerate path ⟨v1⟩\langle v_1\rangle has no edges). I={⟨G,u,v⟩}I = \{\langle G,u,v\rangle\}, SS = sequences of nodes plus the empty sequence ϵ\epsilon meaning "no path". Π\Pi contains (⟨G,u,v⟩,π)(\langle G,u,v\rangle,\pi) for every path π\pi from uu to vv and (⟨G,u,v⟩,ϵ)(\langle G,u,v\rangle,\epsilon) if none exists: not single-valued.
  • Sorting. With A⋆A^\star the Kleene star (all finite sequences, empty one included), I=S=Z⋆I = S = \mathbb Z^\star and ⟨x1,…,xn⟩ Π ⟨y1,…,ym⟩  ⟺  n=m,  y1≤⋯≤yn,  ∃ φ:{1..n}→{1..n} bijective with yi=xφ(i).\langle x_1,\dots,x_n\rangle \,\Pi\, \langle y_1,\dots,y_m\rangle \iff n = m,\ \ y_1 \le \dots \le y_n,\ \ \exists\, \varphi : \{1..n\}\to\{1..n\} \text{ bijective with } y_i = x_{\varphi(i)}.
  • Square roots of a nonzero complex number. I=C−{0}I = \mathbb C - \{0\}, solution = the ordered pair of roots, S=(C−{0})2S = (\mathbb C - \{0\})^2, Π={(z,(w,−w)):w2=z}\Pi = \{(z,(w,-w)) : w^2 = z\}. Not single-valued: for z=−1z=-1 both (i,−i)(i,-i) and (−i,i)(-i,i) are solutions.

Analysis of an algorithm

Two questions: correctness and complexity (time, sometimes space).

Correctness. Prove ∀i∈I:AΠ(i)=s⇒i Π s\forall i \in I : A_\Pi(i) = s \Rightarrow i \,\Pi\, s, 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 cinstrc_{\text{instr}}. The cost of running AΠA_\Pi on ii is tAΠ,i=∑executed instructionscinstrt_{A_\Pi,i} = \sum_{\text{executed instructions}} c_{\text{instr}}.

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 ∣i∣∈N\lvert i \rvert \in \mathbb N 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; nn for a sequence of nn items; nn or n2n^2 for an n×nn\times n matrix (both describe it well).

Let In={i∈I:∣i∣=n}I_n = \{i \in I : \lvert i \rvert = n\}. Since tA,it_{A,i} varies within InI_n, it is summarized by a function TA:N→R+∪{0}T_A : \mathbb N \to \mathbb R^+\cup\{0\}:

  • worst case: TA(n)=sup⁡i∈IntA,iT_A(n) = \sup_{i \in I_n} t_{A,i} (the default: it bounds every run of size nn);
  • best case: TA(n)=inf⁡i∈IntA,iT_A(n) = \inf_{i \in I_n} t_{A,i};
  • average case: TA(n)=E[tA,i]T_A(n) = \mathbb E[t_{A,i}] for a fixed distribution on InI_n.

Example (linear search, n=10n=10, unit cost per comparison). Best case 11, worst case n=10n=10 (item last or absent), average 1n∑k=1nk=n+12=5.5\frac1n\sum_{k=1}^{n}k = \frac{n+1}{2} = 5.5 if the item is uniformly placed.

Example (the size of an integer matters). Testing whether mm is prime by trial division takes about m\sqrt m divisions. In terms of mm that is a square root, but the size of the binary encoding is b=⌈log⁡2m⌉b = \lceil\log_2 m\rceil bits, and the cost becomes ≈2b/2\approx 2^{b/2}: exponential in the input size.

What to specify

To analyse AΠA_\Pi always state: (1) which instructions have unit cost; (2) what the size ∣i∣\lvert i \rvert is; (3) which complexity is studied (default: time, worst case). Exact counts (e.g. n−1n-1 comparisons for the maximum of nn items) hold only in the stated model. The next step is obtaining T(n)T(n) 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 A:I→OA:\mathcal I\to\mathcal O. Problem Π⊆I×S\Pi\subseteq I\times S: relation total (∀i ∃s\forall i\,\exists s) and not necessarily single-valued. AΠA_\Pi solves Π\Pi if I=I\mathcal I=I, O=S\mathcal O=S and AΠ(i)=s⇒i Π sA_\Pi(i)=s\Rightarrow i\,\Pi\,s: it extracts a function from a relation. I,O\mathcal I,\mathcal O are encodings of I,SI,S.
  • Examples: sum (a function); reachability (path from uu to vv or ϵ\epsilon, several paths, so not single-valued); sorting (I=S=Z⋆I=S=\mathbb Z^\star: same length, ordered, permutation); square roots of z≠0z\ne0: I=C−{0}I=\mathbb C-\{0\}, S=(C−{0})2S=(\mathbb C-\{0\})^2, solutions (w,−w)(w,-w), not single-valued because the pair is ordered.
  • Correctness: ∀i:AΠ(i)=s⇒i Π s\forall i: A_\Pi(i)=s\Rightarrow i\,\Pi\,s; induction (recursive) or invariants (iterative).
  • Cost: tA,i=∑cinstrt_{A,i}=\sum c_{\text{instr}} with costs 00 or 11 on dominant instructions (comparisons for sorting, complex operations for complex matrices, bit operations for integers). Size ∣i∣\lvert i\rvert: nn for sequences, nn or n2n^2 for matrices, number of bits for integers.
  • Cases: worst sup⁡i∈IntA,i\sup_{i\in I_n}t_{A,i} (default), best inf⁡\inf, average E\mathbb E. Linear search on nn items: 11, nn, n+12\frac{n+1}2.
  • Integer size: trial division ≈m=2b/2\approx\sqrt m=2^{b/2} steps for bb bits: exponential in the size.
  • Always specify: unit-cost instructions, size, type of complexity.
  • Common mistakes: calling Π\Pi a function; choosing S=C−{0}S=\mathbb C-\{0\} for the two roots; counting every instruction instead of the dominant ones; confusing the value mm with its size log⁡2m\log_2 m.

Teoria collegata