Data structures & Algorithms
Alexander Monzon
Specification of an algorithm in pseudocode
High-level specification of an algorithm is usually done through pseudocode, which providesa
clear and concise description of the elementary steps performed by the algorithm to solve a
given input instance. Pseudocode uses a mix of constructs typical of programming and natural
language languages [GTG14, Sect.1.8.2]. The pseudocode of an algorithm is structured as
follows:
Algorithm algorithm name(input parameters)
input description of the input instance
output description of the returned solution
Description of the elementary steps of the algorithm by a mix of constructs from
the languages programming and natural language
The pseudocode name doesn't have to contain a description of the computational problem
that the algorithm solves, but only the name of the algorithm and the input-output relationship.
Recall that a computational problem is defined by a set I of instances, a set S of solutions,
and a set Π ⊆ I × S that, for each input instance, defines the correct solution(s). An algorithm
that solves such a problem computes a function that associates with each instance i ∈ I a
solution s ∈ S, such that (i, s) ∈ Π. If multiple solutions exist in Π for the same instance, the
algorithm computes only one. The pseudocode header has only to highlight the function i → s
computed by the algorithm, specifying i and s.
The most frequently used programming language constructs are:
● Assigning a value to a variable, denoted by the symbol "←".
● Indexing of arrays (e.g., A[i])
● if (condition) then {- - - } else {- - - }. The "else" branch is optional.
● for variable ← initial value to/downto final value do {- - - }.
● foreach element of an iterator do {- - - }.
● while (condition) do {- - - }.
● repeat {- - - } until (condition).
● return, optionally followed by a value or set of values.
● Invocation of an algorithm, perhaps the algorithm itself, if recursive.
There are some common sense rules and conventions that are good to follow.
● Meaningful names must be given to algorithms.
● One must avoid using Italian translations of programming language constructs.
● The use of natural language should be limited to those cases in which it simplifies the
description and helps the understanding of the algorithm, but without overshadowing
the sequence of operations. In other cases, the use of the constructs listed above
turns out to be more standard and effective.
● In for loops, the increment of the loop variable is automatic and does not have to be
explicitly stated, unlike while and repeat loops, where the variables that determine the
succession of iterations must be explicitly updated.
● There are three types of variables used by an algorithm:
○ Local variables, which must be defined and initialized by the algorithm and
which do not survive the execution of the algorithm.
○ Global variables, which are assumed to be defined in the calling process and
which survive the execution of the algorithm.
○ Input parameters given in parentheses next to the name of the algorithm. Usually,
the initial values of these parameters are provided to the algorithm by the calling
process. In some cases, the input parameters are global variables that survive
the execution of the algorithm. (This choice is more flexible than in Java, where
the input parameters are passed by value, that is, they become local variables
that do not survive the execution of the algorithm. However, even in Java, in the
case of input parameters that contain references to objects, changes to those
objects survive the execution of the algorithm.)
● If an algorithm does not explicitly return values but has an effect on global data, the
output description line in the header describes that effect (e.g., think of sorting a
sequence S).
● A recursive algorithm contains within it one or more calls to itself on smaller
instances. It is important that the calls are made using the name of the algorithm, and
that the choice of input parameters to be given in parentheses next to the algorithm
name unambiguously defines the instance on which the algorithm is invoked.
References
[1] M.T. Goodrich, R. Tamassia, M.H. Goldwasser. Data Structures and Algorithms in Java,
Sixth Edition. John Wiley and Sons, 2014