tags:

views:

522

answers:

6

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?

+3  A: 

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
It requires a great level of abstraction away from the real code, but yes - I guess this is about what I need. Thanks.
ferdystschenko
@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
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
+1  A: 

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
It's mostly procedural, but you're right about using UML for OO. Thanks for the hint.
ferdystschenko
+2  A: 

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!Fortress Code Example

Jörg W Mittag
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
+1  A: 

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
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
+1  A: 

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).

ldigas
+1  A: 

Answering my own question, I just wanted to draw attention to the following webpage about typesetting pseudo code in LaTeX: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms. They describe a number of different styles, listing advantages and drawbacks. Incidently, 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.

ferdystschenko