Algorithms and correctness: Difference between revisions
Jump to navigation
Jump to search
Line 33: | Line 33: | ||
== Invariant == | == Invariant == | ||
# The invariant of a loop | # 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 | # 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:''' | '''Remarks:''' | ||
# | # Specific invariant has a specific function in the [[#Correctness proofs|correctness proof]] for the algorithm. Typically, 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 selected invariant assertions. | |||
== Variant == | == Variant == |
Revision as of 11:51, 27 May 2015
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.
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:
- Specific invariant has a specific function in the correctness proof for the algorithm. Typically, 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 selected 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.