Computational Problems, Algorithms and Models of Computation
In questa pagina 7
From problem to program
The course separates four phases:
- Problem specification: say exactly what has to be computed.
- 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 →.
- 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 →).
- 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 is a set of pairs with (an instance, = instance domain) and (a solution, = solution domain):
is a relation that associates with each instance one or more solutions. It is required that for every there is at least one with .
Examples
Even or odd. Given a positive integer, decide whether it is even or odd.
- , , pairs each integer with its parity.
- , , .
Sum of two integers.
- , ,
- .
- ; ; .
Sorting an array of integers.
- = arrays of integers, = sorted arrays of integers,
- .
- ; ; (different integers).
Sorting, version 2 (output = a permutation of the positions that orders the array):
- = permutations, .
- .
- and also : equal elements can be ordered in different ways.
- .
Transpose of a square matrix.
- = integer matrices , (positions ); = same set (so );
- .
Observations
- A solution can be associated with several different instances (sum of integers: and both give ).
- 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 solves if:
- receives as input instances ;
- produces as output solutions ;
- for every input , the output satisfies .
So calculates a function from to . If has several solutions for an instance, 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: = number of elements.
- Transposing a matrix: = dimension of the matrix (for : rows and 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:
- 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, ...
- 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 (), return .
Idea (checked on ): the diagonal elements do not change. Traverse the upper triangle and, for each with , swap it with in the lower triangle. Returning 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
- Problem: relation (instances, solutions); every instance has at least one solution.
- Solution may be unique (sum, sorting v.1) or not (sorting v.2 with equal elements); one solution may serve many instances.
- Examples: even/odd, sum , sorting, transpose ().
- Algorithm: well-defined procedure, finite sequence of elementary steps. It solves if for each it outputs with ; it computes one solution.
- Problem = what, algorithm = how.
- RAM model: CPU + memory; elementary steps (assignment, logic, arithmetic, array indexing, return) each cost 1 atomic operation.
- Size of an instance: sorting elements, matrix gives . Complexity is expressed as a function of size, 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 →.
- Data structure: objects + access/modification methods. Logical level = ADT (Java interfaces); physical level = implementation (Java classes).
- Phases: specification, design 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 →, analysis (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 →, complexity), coding.
- Transpose: diagonal fixed, swap with for .
- Typical mistake: confusing the problem (a relation) with the algorithm (one way to compute a function inside it), or forgetting that equal instances may admit several correct outputs.