33

I need to translate some python and java routines into pseudo code for my master thesis but have trouble coming up with a syntax/style that is:

  • consistent
  • easy to understand
  • not too verbose
  • not too close to natural language
  • not too close to some concrete programming language.

How do you write pseudo code? Are there any standard recommendations?

ferdystschenko
  • 1,527
  • 1
  • 9
  • 18

7 Answers7

20

I recommend looking at the "Introduction to Algorithms" book (by Cormen, Leiserson and Rivest). I've always found its pseudo-code description of algorithms very clear and consistent.

An example:

DIJKSTRA(G, w, s)
1  INITIALIZE-SINGLE-SOURCE(G, s)
2  S ← Ø
3  Q ← V[G]
4  while Q ≠ Ø
5      do u ← EXTRACT-MIN(Q)
6         S ← S ∪{u}
7         for each vertex v ∈ Adj[u]
8             do RELAX(u, v, w)
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • It requires a great level of abstraction away from the real code, but yes - I guess this is about what I need. Thanks. – ferdystschenko Feb 20 '10 at 10:35
  • 2
    @ferdystschenko: yes, but pseudo code is all about abstraction - hiding the unnecessary details away. In the example above, line 6 says u will be unified onto S, what does it matter how it's implemented? – Eli Bendersky Feb 20 '10 at 10:59
  • 3
    To elaborate on Eli Bendersky: Not only do the details of how it's implemented not matter, but since this is pseudo code, you don't even know how it is implemented! – Peter Di Cecco Feb 25 '10 at 14:13
7

Answering my own question, I just wanted to draw attention to the TeX FAQ entry Typesetting pseudocode in LaTeX. It describes a number of different styles, listing advantages and drawbacks. Incidentally, there happen to exist two stylesheets for writing pseudo code in the manner used in "Introductin to Algorithms" by Cormen, as recommended above: newalg and clrscode. The latter was written by Cormen himself.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
ferdystschenko
  • 1,527
  • 1
  • 9
  • 18
  • personally this pseudocode is my favorite, it looks like it's based on predicate logic but with a very clean notation for code control. i love it and it looks neat. – Marnix v. R. Sep 12 '12 at 15:48
5

I suggest you take a look at the Fortress Programming Language.

This is an actual programming language, and not pseudocode, but it was designed to be as close to executable pseudocode as possible. In particular, for designing the syntax, they read and analyzed hundreds of CS and math papers, courses, books and journals to find common usage patterns for pseudocode and other computational/mathematical notations.

You can leverage all that research by just looking at Fortress source code and abstracting out the things you don't need, since your target audience is human, whereas Fortress's is a compiler.

Here is an actual example of running Fortress code from the NAS (NASA Advanced Supercomputing) Conjugate Gradient Parallel Benchmark. For a fun experience, compare the specification of the benchmark with the implementation in Fortress and notice how there is almost a 1:1 correspondence. Also compare the implementation in a couple of other languages, like C or Fortran, and notice how they have absolutely nothing to do with the specification (and are also often an order of magnitude longer than the spec).

I must stress: this is not pseudocode, this is actual working Fortress code! From https://umbilicus.wordpress.com/2009/10/16/fortress-parallel-by-default/

Fortress code example of conjugate gradient

Note that Fortress is written in ASCII characters; the special characters are rendered with a formatter.

mic
  • 1,190
  • 1
  • 17
  • 29
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 6
    I find it funny that you think this is a clear and simple syntax. What's the difference between := and = ? Does the subscript "max" act as an operator or is it just notation? Pseudo code should be something you can explain to a non-specialist. – Frank Krueger Feb 20 '10 at 15:36
4

If the code is procedural, normal pseudo-code is probably easy (Wikipedia has some examples).

Object-oriented pseudo-code might be more difficult. Consider:

  • using UML class diagrams to depict the classes/inheritence
  • using UML sequence diagrams to depict the sequence of code
Patrick
  • 23,217
  • 12
  • 67
  • 130
3

I don't understand your requirement of "not too close to some concrete programming language".

Python is generally considered as a good candidate for writing pseudo-code. Perhaps a slightly simplified version of python would work for you.

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
  • 1
    I generally agree, although I think that python does have some things that may not be immediatly intelligible for someone who has no knowledge of the language. One example is the notation of lists, dictionaries and tuples, i.e. '{}' might well be taken as an empty array and not a mapping structure. – ferdystschenko Feb 20 '10 at 15:00
2

Pascal has always been traditionally the most similar to pseudocode, when it comes to mathematical and technical fields. I don't know why, it was just always so.

I have some (oh, I don't know, 10 maybe books on a shelf, which concrete this theory).

Python as suggested, can be nice code, but it can be so unreadable as well, that it's a wonder by itself. Older languages are harder to make unreadable - them being "simpler" (take with caution) than today's ones. They'll maybe be harder to understand what's going on, but easier to read (less syntax/language features is needed for to understand what the program does).

Rook
  • 60,248
  • 49
  • 165
  • 242
2

This post is old, but hopefully this will help others.

"Introduction to Algorithms" book (by Cormen, Leiserson and Rivest) is a good book to read about algorithms, but the "pseudo-code" is terrible. Things like Q[1...n] is nonsense when one needs to understand what Q[1...n] is suppose to mean. Which will have to be noted outside of the "pseudo-code." Moreover, books like "Introduction to Algorithms" like to use a mathematical syntax, which is violating one purpose of pseudo-code.

Pseudo-code should do two things. Abstract away from syntax and be easy to read. If actual code is more descriptive than the pseudo-code, and actual code is more descriptive, then it is not pseudo-code.

Say you were writing a simple program.

Screen design:

Welcome to the Consumer Discount Program!
Please enter the customers subtotal: 9999.99
The customer receives a 10 percent discount
The customer receives a 20 percent discount
The customer does not receive a discount
The customer's total is: 9999.99

Variable List:

TOTAL:         double
SUB_TOTAL:     double
DISCOUNT:      double

Pseudo-code:

DISCOUNT_PROGRAM

    Print "Welcome to the Consumer Discount Program!"
    Print "Please enter the customers subtotal:"
    Input SUB_TOTAL

    Select the case for SUB_TOTAL
        SUB_TOTAL > 10000 AND SUB_TOTAL <= 50000
            DISCOUNT = 0.1
            Print "The customer receives a 10 percent discount"
        SUB_TOTAL > 50000
            DISCOUNT = 0.2
            Print "The customer receives a 20 percent discount"
        Otherwise
            DISCOUNT = 0
            Print "The customer does not a receive a discount"

    TOTAL = SUB_TOTAL - (SUB_TOTAL * DISCOUNT)
    Print "The customer's total is:", TOTAL

Notice that this is very easy to read and does not reference any syntax. This supports all three of Bohm and Jacopini's control structures.

Sequence:

Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)

Selection:

if condition
    Do one extra thing

if condition
    do one extra thing
else
    do one extra thing

if condition
    do one extra thing
else if condition
    do one extra thing
else
    do one extra thing

Select the case for SYSTEM_NAME
    condition 1
        statement 1
    condition 2
        statement 2
    condition 3
        statement 3
    otherwise
        statement 4

Repetition:

while condition
    do stuff

for SOME_VALUE TO ANOTHER_VALUE
    do stuff

compare that to this N-Queens "pseudo-code" (https://en.wikipedia.org/wiki/Eight_queens_puzzle):

PlaceQueens(Q[1 .. n],r)

    if r = n + 1
        print Q
    else
        for j ← 1 to n
            legal ← True
            for i ← 1 to r − 1
                if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
                    legal ← False
        if legal
            Q[r] ← j
            PlaceQueens(Q[1 .. n],r + 1) 

If you can't explain it simply, you don't understand it well enough. - Albert Einstein