Algorithms and correctness
Algorithmic problem
An algorithmic problem is described by:
- a set of feasible inputs.
- for each input a set of feasible outputs;
- optionally, an objective function, which assigns a real number to each feasible solution (the quality of the feasible solution). If an objective function is specified, it is also specified whether the objective function is to be minimized or to be maximized.
Operations and instructions
A programming languages offers a possibility to specify instructions (a.k.a. statements). An instruction specifies a sequence of operations; executing the machine amounts to executing this sequence of operations.
Algorithm
- An algorithm is associated with an algorithmic problem.
- An algorithm is a sequence of instructions how to compute a feasible output for a given input.
- If an objective function is given, the objective function value of the generated solution is a criterion for the quality of an algorithm. More specifically:
- in case of minimization: a low objective function value is favored;
- in case of maximization: a high objective function value is favored.
Iterative and recursive algorithms
- Basically, a non-trivial algorithm is a loop or recursion plus some preprocessing and/or postprocessing.
- If an algorithm consists of several loops/recursions that are executed strictly after each other, it may be viewed as two algorithms that are executed after each other.
- An iteration of a loop may contain another loop or recursion. Analogously, a recursive call may contain another loop or recursion. We say that a loop/recurions inside a loop/recursions is nested or the inner loop/recursion. The nesting loop/recursion is the outer one.
Remark:
- Clearly, a loop may be transformed into a recursion and vice versa. So, every algorithm may be formulated either as a loop or as a recursion. However, in most cases, one of these two options looks simpler and "more natural" than the other one.
- Formulating an algorithm as a loop might be favorable in many cases because a loop allows more control than a recursion. More specifically, a loop may be implemented as an iterator whose method for going one step forward implements the execution of one iteration of the loop. Such an implementation allows to terminate the loop early, to suspend execution and resume execution later on, and to execute some additional instructions between two iterations (e.g. for visualization or for testing).
Correctness
An algorithm is correct if three conditions are fulfilled for every feasible input:
- All operations are well-defined. For example, access to an array component outside the array's index range and
- Each loop and recursion terminates regularly, that is, the break condition is fulfilled after a finite number of operations.
- The output delivered by the algorithm is a feasible output for the given input.
Invariant
- The invariant of a loop consists of all assertions that are true immediately before the first iteration, immediately after the last iteration, and between two iterations. These assertions are called the invariant assertions of this loop.
- The invariant of a recursion consists of all assertions that are fulfilled immediately after each recursive call. These assertions are called the invariant assertions of this recursion.
Remarks:
- The invariant of an algorithm has a specific function in the correctness proof for the algorithm. Typically, in a documentation, only those invariant assertions are stated that are relevant for this purpose.
- The invariant is also essential for testing purposes: a white-box test of a loop/recursion should definitely check maintenance of relevant invariant assertions.
Variant
Remarks:
- The invariant has a specific function in the correctness proof for the algorithm. It should contain all statements relevant for this purpose. It should contain no further statements unless there is a good reason for that.
- Important for tests
Correctness proofs
- The variant must be specified in a way that allows to conclude that the break condition will be fulfilled after a finite number of iterations.
- The invariant
'Remark: for complexity the number of iterations bounded.