Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standards for pseudo code? [closed]

Tags:

pseudocode

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?

like image 604
ferdystschenko Avatar asked Feb 20 '10 09:02

ferdystschenko


6 Answers

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)
like image 83
Eli Bendersky Avatar answered Oct 21 '22 23:10

Eli Bendersky


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.

like image 39
ferdystschenko Avatar answered Oct 21 '22 23:10

ferdystschenko


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.

like image 5
Jörg W Mittag Avatar answered Oct 22 '22 01:10

Jörg W Mittag


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
like image 4
Patrick Avatar answered Oct 22 '22 00:10

Patrick


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.

like image 3
Olivier Verdier Avatar answered Oct 22 '22 01:10

Olivier Verdier


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

like image 2
Rook Avatar answered Oct 21 '22 23:10

Rook