Development

Working with algorithms and generators

Algorithms

We see algorithms like something which can be initialized on a graph and then
computed. For example, an algorithm can colorize nodes or edges of a graph ; an
other can compute a spanned tree …

The org.graphstream.algorithm.Algorithm interface defines the structure of
an algorithm. It contains two methods :

init( Graph g ), which is the initialization step of the algorithm ;

compute(), which launches the algorithm.

Why does we not just use a compute(Graph) method ?

The initialization step and computing step are located in different methods
because you may have to make a new computation of your algorithm without
calling the initialization step again. This initialization step must contain code
which is unique for a graph and it has to be called again only if you want to reset
data linked to the algorithm or to change the graph.

Dynamicity

On GraphStream, dynamicity is at the foreground so it is normal that algorithms
can be dynamic to. The interface org.graphstream.algorithm.DynamicAlgorithm
extends Algorithm and introduces a new method terminate() which defines
the end of the algorithm.

Use of this kind of algorithms could be:

Graphg=...;// Creation of the graphDynamicAlgorithmda=...;da.init(g);while(...)// something to doda.compute();da.terminate();

A basic dynamic algorithm example

In this example, computation is done continuously in a loop. One may want to
make the computation when receiving events. This can be done with a Sink that
will trigger the computation. For example, this is a dynamic algorithm where
computation is done when a node is added: