Skip to content

Observer Concepts

Alexandre Rabérin edited this page May 12, 2020 · 1 revision

Observers Concepts

Algorithms do not serialize any interesting data. They publish a set of standard events that an observer can listen to gather the data (algorithms and observers in QuikGraph implement the Observer Pattern). For example, one can attach an observer that records the vertex predecessors to algorithms that compute the shortest path.

  • VertexPredecessorRecorderObserver creates a dictionary that links vertices to their parent edge.
  • EdgePredecessorRecorderObserver creates a dictionary that links edges to their parent edge.
  • VertexDistanceRecorderObserver stores the distance of vertices from the root vertex.
  • VertexTimeStamperObserver stores the time instant where a vertex processing start and finishes.

Using observers

Observer instance are to be 'attached' to algorithms during the computation. All observers implement an Attach method that returns a disposable instance. When disposed it 'detached' the observer from the algorithm events.

IVertexListGraph<TVertex, TEdge> graph = ...; // Some graph instance
var dfs = new DepthFirstSearchAlgorithm<TVertex, TEdge>(graph);
var predecessorRecorder = new VertexPredecessorRecorderObserver<TVertex, TEdge>();

using (predecessorRecorder.Attach(dfs)) // Listen to DFS events, then unhook
{
    dfs.Compute();
}
Clone this wiki locally