Pseudo Code Library
Pseudo Code Library
Pseudo Code Library
Luay Nakhleh
Pseudo-code: Syntax
Pseudo-code is a high-level, abstract description of an algorithm that is intended to be read by humans
and subsequently turned into a program in a programming language of choice. As such, pseudo-code
provides the details needed to understand the algorithm, but omits many details that are left to the programmer to implement, since those may vary depending on the programming language and the choice of
implementation.
General
v
v = w: testing whether v equals w (the value of which is True if v equals w, and False otherwise)
v 6= w: testing whether v is not equal to w (the value of which is True if v is not equal to w, an False
otherwise)
a or b: a or b (which is true if and only if at least one of a and b is true)
a and b: a and b (which is true if and only if both of a and b are true)
random(a, b): returns a random number in [a, b) (that is, a may be returned, but not b)
Break: breaks out of a loop
if c then
...
else
...
Loops
for i
a to b do: loop over the values of i that start at a (integer) and end at b (integer), while
incrementing the value by 1 after each iteration
while c: loop while condition c is true
Functions
return: returning a value from a function
out
foo(params): call function foo on its parameters params and store its output in out. For
example: distances
BFS(g, v).
Algorithmic Thinking
Pseudo-code
L[0..n
M [0..n 1, 0..m
0, 1, . . . , m 1
1
1
Set notation
B)
Algorithmic Thinking
Pseudo-code
Random(A, m, p): return a random set of m elements from A, where the probability of choosing element x 2 A is given by p (for example, Random(A, m, 1/|A|) returns a random set of m elements
from A assuming a uniform distribution. Another example: Random(V, m, deg(v)/(2|E|)) returns a
random set of m nodes from set V of nodes, where the probability of choosing a node is proportional
to the nodes degree, or, more precisely, it equals the degree of that node normalized by the sum of
the degrees of all nodes)
argminx2A wx : the element of set A whose weight is minimum among all elements of A (it is assumed
that each element x in set A has weight wx , and more than one element has the minimum weight,
then one of them is returned arbitrarily)
argmaxx2A wx : similar to argmin, but returns the element of A with maximum weight
Data structures
queue(): returns an empty queue
enqueue(Q, a): puts element a in the queue Q
dequeue(Q): returns the element at the head of the queue Q
Graph-theoretic notation
V (g): the set of nodes of graph g
E(g): the set of edges of graph g
ng (v): the set of neighbors of node v in undirected graph g. If g is clear from the context, we may drop
the subscript (that is, just use n(v))
degg (v): the quantity |ng (v)|. If g is clear from the context, we may drop the subscript
ing (v): the set of nodes from which there are edges to node v in directed graph g. If g is clear from the
context, we may drop the subscript (that is, just use in(v))
outg (v): the set of nodes to which there are edges from node v in directed graph g. If g is clear from
the context, we may drop the subscript (that is, just use out(v))
indegg (v): the quantity |ing (v)|. If g is clear from the context, we may drop the subscript
outdegg (v): the quantity |outg (v)|. If g is clear from the context, we may drop the subscript
wg (e): the weight of edge e in graph g. If g is clear from the context, we may drop the subscript (that
is, just use w(e))
argmine (A, B): the edge whose one endpoint is in set A of nodes and the other endpoint is in set B,
and has minimum weight of all such edges (it is assumed that A, B V (g) for some graph g, and
e 2 E(g))
nodesg (E 0 ): the set of nodes that are endpoints of at least one of the edges in E 0 in graph g. If g is clear
from the context, we may drop the subscript
edgesg (V 0 ): the set of edges each of which has at least one of its endpoints in set V 0 in graph g. If g is
clear from the context, we may drop the subscript
foreach v 2 V (g): looping over all nodes in graph g.
foreach e 2 E(g): looping over all edges in graph g.
3
Algorithmic Thinking
Pseudo-code