Salta al contenuto
Note per Studenti Computational Problems, Algorithms and Models of Computation Report

Computational Problems, Algorithms and Models of Computation

In questa pagina 7

From problem to program

The course separates four phases:

  1. Problem specification: say exactly what has to be computed.
  2. Algorithm design, written in PseudocodePseudocode is the rigorous, free-form way algorithms are specified in this course: a header with name, input and output, then a body that mixes programming constructs with natural language only where it helps.Pseudocode →.
  3. Algorithm analysis: correctness and complexity (see Analysis of Algorithms and Asymptotic NotationThe worst-case time complexity t_A(n) counts elementary RAM steps on the hardest instance of size n; since it is hard to compute exactly, it is bounded asymptotically with O, Omega, Theta and o, ignoring constants and lower-order terms.Analysis of Algorithms and Asymptotic Notation → and Correctness and Loop InvariantsCorrectness means termination plus solving the problem; it is proved with basic proof techniques (example, counterexample, contradiction, induction) and, for loops, with an invariant shown to hold initially, to be preserved, and to imply the final state.Correctness and Loop Invariants →).
  4. Program coding in a programming language (e.g. Java).

Why separate design from implementation?

  • The algorithmic strategy is chosen first: it has the major impact on efficiency.
  • Suitable data structures and constructs (for, while, ...) are selected.
  • Syntax and low-level details are avoided at the beginning.

Phases 1-3 look at the essence of the algorithm as a method for solving the problem. Coding translates it into a language and optimises for the characteristics and constraints of that language. In the degree: Foundations of Computer Science focuses on programming and implementation, this course on design and analysis.

Computational problem

A computational problem is a question that requires a solution expressed as an algorithm.

Formally, a problem Π\Pi is a set of pairs (i,s)(i, s) with i∈Ii \in \mathcal{I} (an instance, I\mathcal{I} = instance domain) and s∈Ss \in S (a solution, SS = solution domain):

Π⊆I×S\Pi \subseteq \mathcal{I} \times S

Π\Pi is a relation that associates with each instance one or more solutions. It is required that for every i∈Ii \in \mathcal{I} there is at least one s∈Ss \in S with (i,s)∈Π(i, s) \in \Pi.

Examples

Even or odd. Given a positive integer, decide whether it is even or odd.

  • I=Z+\mathcal{I} = \mathbb{Z}^+, S={even,odd}S = \{\text{even}, \text{odd}\}, Π\Pi pairs each integer with its parity.
  • 4↦even4 \mapsto \text{even}, 7↦odd7 \mapsto \text{odd}, 15↦odd15 \mapsto \text{odd}.

Sum of two integers.

  • I={(x,y):x,y∈Z}\mathcal{I} = \{(x, y) : x, y \in \mathbb{Z}\}, S=ZS = \mathbb{Z},
  • Π={((x,y),s):s=x+y}\Pi = \{((x,y), s) : s = x + y\}.
  • ((1,9),10)∈Π((1, 9), 10) \in \Pi; ((23,6),29)∈Π((23, 6), 29) \in \Pi; ((13,45),31)∉Π((13, 45), 31) \notin \Pi.

Sorting an array of integers.

  • I\mathcal{I} = arrays of integers, SS = sorted arrays of integers,
  • Π={(A,B):B contains the same integers of A and they are sorted}\Pi = \{(A, B) : B \text{ contains the same integers of } A \text{ and they are sorted}\}.
  • (⟨43,16,75,2⟩,⟨2,16,43,75⟩)∈Π(\langle 43, 16, 75, 2 \rangle, \langle 2, 16, 43, 75 \rangle) \in \Pi; (⟨7,1,7,3,3,5⟩,⟨1,3,3,5,7,7⟩)∈Π(\langle 7,1,7,3,3,5 \rangle, \langle 1,3,3,5,7,7 \rangle) \in \Pi; (⟨13,4,25,17⟩,⟨11,27,33,68⟩)∉Π(\langle 13, 4, 25, 17 \rangle, \langle 11, 27, 33, 68 \rangle) \notin \Pi (different integers).

Sorting, version 2 (output = a permutation of the positions 1,…,n1, \dots, n that orders the array):

  • SS = permutations, Π={(A,P):P orders the integers of A}\Pi = \{(A, P) : P \text{ orders the integers of } A\}.
  • (⟨43,16,75,2⟩,⟨4,2,1,3⟩)∈Π(\langle 43,16,75,2 \rangle, \langle 4,2,1,3 \rangle) \in \Pi.
  • (⟨7,1,7,3,3,5⟩,⟨2,4,5,6,1,3⟩)∈Π(\langle 7,1,7,3,3,5 \rangle, \langle 2,4,5,6,1,3 \rangle) \in \Pi and also (…,⟨2,5,4,6,1,3⟩)∈Π(\ldots, \langle 2,5,4,6,1,3 \rangle) \in \Pi: equal elements can be ordered in different ways.
  • (⟨13,4,25,17⟩,⟨1,2,4,3⟩)∉Π(\langle 13,4,25,17 \rangle, \langle 1,2,4,3 \rangle) \notin \Pi.

Transpose of a square matrix.

  • I\mathcal{I} = integer matrices n×nn \times n, n≥1n \ge 1 (positions 0,…,n−10, \dots, n-1); SS = same set (so I=S\mathcal{I} = S);
  • Π={(A,B):B=AT}\Pi = \{(A, B) : B = A^T\}.

Observations

  • A solution can be associated with several different instances (sum of integers: 1+91+9 and 2+82+8 both give 1010).
  • One instance can have several solutions (sorting version 2).
  • In the first sorting version and in the sum the solution of each instance is unique.

Algorithm

An algorithm is a well-defined computational procedure that transforms a given input into an output by executing a finite sequence of elementary steps. Key properties: input and output, finiteness, effectiveness, unambiguity.

An algorithm AA solves Π⊆I×S\Pi \subseteq \mathcal{I} \times S if:

  1. AA receives as input instances i∈Ii \in \mathcal{I};
  2. AA produces as output solutions s∈Ss \in S;
  3. for every input ii, the output ss satisfies (i,s)∈Π(i, s) \in \Pi.

So AA calculates a function from I\mathcal{I} to SS. If Π\Pi has several solutions for an instance, AA computes only one of them.

The problem says what has to be solved; the algorithm says how, step by step. For even/odd: "is this number even or odd?" is the problem, "divide by 2 and look at the remainder" is the algorithm.

Model of computation

An algorithm is defined with respect to a model of computation: an abstraction of a computer that specifies the set of elementary steps and describes how computation, memory and communication are organised.

The model used is the RAM (Random Access Machine, not Random Access Memory), made of:

  • Memory: stores inputs, outputs, intermediate data and programs;
  • CPU: follows the instructions and executes the elementary steps.

Elementary steps are primitive operations: assignment, logical operations (AND, OR, XOR), arithmetic operations (sum, division, product, ...), array indexing, returning a value from a method.

  • The exact set depends on the architecture.
  • Each step typically takes one CPU cycle.
  • In complexity analysis each step counts as one atomic operation, regardless of the real number of cycles.

Size of an instance

Instances are grouped by size: a function mapping each instance to one or more values measuring its magnitude.

  • Sorting an array: nn = number of elements.
  • Transposing a matrix: nn = dimension of the n×nn \times n matrix (for n×mn \times m: nn rows and mm columns).

Grouping by size lets us express complexity as a function of a parameter. Instances of the same size can still be easier or harder, but comparing very different sizes would be meaningless. Details in Analysis of Algorithms and Asymptotic NotationThe worst-case time complexity t_A(n) counts elementary RAM steps on the hardest instance of size n; since it is hard to compute exactly, it is bounded asymptotically with O, Omega, Theta and o, ignoring constants and lower-order terms.Analysis of Algorithms and Asymptotic Notation →.

Data structure

A data structure is a collection of objects together with methods for access and/or modification: a systematic way to organise and store information for efficient access and modification. It holds the input data and the data generated during execution.

Two levels of abstraction:

  1. Logical level: the logical organisation of the objects and the input-output behaviour of each method. These are the Abstract Data Types (ADT): lists, arrays, trees, ...
  2. Physical level: the physical layout of the data and the implementation of the methods by algorithms. Here the structure is engineered for the most effective implementation within the constraints of the language.

Java distinguishes the two levels explicitly: interfaces (hierarchy) for the logical level, classes (hierarchy) for the physical level.

Example: transpose

Problem: given AA (n×nn \times n), return B=ATB = A^T.

Idea (checked on n=4n = 4): the diagonal elements do not change. Traverse the upper triangle and, for each aija_{ij} with i<ji < j, swap it with ajia_{ji} in the lower triangle. Returning AA itself avoids initialising a new matrix and copying the diagonal. The pseudocode (for and while versions) is in PseudocodePseudocode is the rigorous, free-form way algorithms are specified in this course: a header with name, input and output, then a body that mixes programming constructs with natural language only where it helps.Pseudocode →.

Versione ripasso

Teoria collegata