GraphStream Base Tutorial 7 : The various Graph implementations

SourceForge.net Logo

In this quick tutorial we will learn the difference between the various implementations of the Graph interface.

Multiple Graph implementations

In GraphStream, the main class, the Graph class is in fact an interface.

This has been done to provide several implementations according to the needs and uses cases. The various implementations all respect the interface and are free to add features.

There are actually three graph implementations :

DefaultGraph

This is the oldest (and therefore maybe the more robust) implementation available. As its name suggests, it is the default implementation used.

It checks the graph structure at any time so that the graph is never inconsistent. For example, it ensures that edges connected to a removed node are also removed.

Its advantage or drawback, depending on the uses, is that it only allows one edge between two nodes (or one "loop" edge on the same node). It supports both directed and undirected edges. When adding an edge between two nodes, an error is generated if an edge already exists.

There is one exception to this : when there is already a directed edge between two nodes A and B for example, this edge going from A to B, it is possible to add a directed edge going from node B to A. This will in fact change the existing edge into an undirected edge. However this is considered bad usage, and the Edge interface provides methods to change the direction or to "undirect" an edge.

AdjacencyListGraph

This implementation works almost the same as DefaultGraph but uses almost twice less memory. The counterpart is that it may be slower on some operations. Use it when you have very large graphs to store in memory.

MultiGraph

This implementation provides exactly the same services as DefaultGraph but allows several edges between two nodes (and several "loop edges" on the same node). It has the same memory footprint as DefaultGraph.

The GraphViewer is able to represent such "multi-graphs". For example, you can obtain such a display :

A graph image

css xhtml