User Tools

Site Tools

This is an old revision of the document!

Creating algorithms

Including an algorithm in a page

An algorithm is stored in a single page in the Algorithm namespace. Different variants of the same algorithm are stored on different pages. For example Factorial recursive and Factorial iterative.

You include an algorithm on a different page using the algorithm tag. The last part of the tag is the title of the algorithm page.

[algorithm Fisher-Yates shuffle]

This transcludes the algorithm from Fisher-Yates shuffle.

Creating an algorithm page

An algorithm page is split into four sections: algorithm, support, options, and visualisation. Each of these sections contains code wrapped in <syntax> tags.

The basic outline of an algorithm page should look like this:

[algorithm my-algorithm]

====== Algorithm ======
<syntax js>

====== Support ======
<syntax js>

====== Options ======
<syntax js>

====== Visualisation ======
<syntax html>

The algorithm page usually includes itself, so that you can easily preview how your changes will work.


The 1st section contains the algorithm. This is the code that the user sees in the interactive environment.


The 2nd section contains the support code. This is support code, such as functions that the main algorithm uses. If it also includes a run() function this function is called when running the algorithm.


The 3rd section contains options in JSON format. Available options are

  • height – The height of the transcluded algorithm.
  • preRunSource – If true, the algorithm source will be run at load time and the debugger buttons will initially be disabled. The debug controls enable when runScript(“…”) is called from the visualisation. For an example of this in use see linked-list.


The 4th section contains the visualisation code. This code is a self contained webpage, it is embedded in an iframe when displayed alongside the code.

The webpage must provide a global update() function, which is called at each new line when running the algorithm. The update function takes two arguments,

  • n the current node in the AST (This should be used very rarely)
  • x the execution context.
  • isRunning is the debugger in the middle of running code?
  • duration if the visualisation animates, how long it should take. If duration is below 0 then visualising can be skipped if the update function is stateless.
  • prev the prev continuation, if the visualisation is undo-able it should return a new prev continuation that calls this.

Two other functions are optional: globals() and args(). If the visualisation contains a global function args() then the return value of calling that function will be passed into the run support function. This allows the visualisation to supply the input to the algorithm. If a globals() function is provided then it returns an object whose keys are added to the globals of the interpreter.

creating_an_algorithm.1367710667.txt.gz · Last modified: 2015/02/02 08:24 (external edit)