public interface DynamicAlgorithm extends Algorithm
Algorithm
so dynamic algorithms are
composed of an initialization step followed by several computation steps. A
last step, specific to dynamic algorithms, marks the termination of the
algorithm and has to be used to close the computation.
The following example computes the amount of apparitions of each node and the
average value of apparitions for nodes :
public class ApparitionAlgorithm extends SinkAdapter implements DynamicAlgorithm { Graph theGraph; HashMap<String, Integer> apparitions; double avg; public void init(Graph graph) { theGraph = graph; avg = 0; graph.addSink(this); } public void compute() { avg = 0; for (int a : apparitions.values()) avg += a; avg /= apparitions.size(); } public void terminate() { graph.removeSink(this); } public double getAverageApparitions() { return avg; } public int getApparitions(String nodeId) { return apparitions.get(nodeId); } public void nodeAdded(String sourceId, long timeId, String nodeId) { int a = 0; if (apparitions.containsKey(nodeId)) a = apparitions.get(nodeId); apparitions.put(nodeId, a + 1); } public void stepBegins(String sourceId, long timeId, double step) { compute(); } }Note that in the example, the #terminate() method is used to remove the link between graph and the algorithm. The computation here is done at every step but can be done anywhere else, according to the algorithm requirements.
Copyright © 2015. All rights reserved.