GraphStream Tutorials

Let's get some coding done

cg "id":"GraphStream Team" 
an "Stefan Balev"
an "Antoine Dutot" 
an "Yoann Pigné"
an "Guilhelm Savin"


  1. Installation of GraphStream
  2. Tutorial 1: Basic tasks with GraphStream
  3. Tutorial 2: A first dynamic graph

Installation of GraphStream

Basic steps to install GraphStream

  1. Get a working java SDK installed
  2. Get Eclipse installed
  3. Get the GraphStream tutorial sources
  4. Get GraphStream binaries

Get Eclipse Installed

Get the tutorial workspace

Import into Eclipse

Get GraphStream binaries

Tutorial 1

Basic tasks with GraphStream

Create and display

Open src > org > graphstream > demo > tutorial1 >

import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;

public class Tutorial1 {
	public static void main(String args[]) {
		Graph graph = new SingleGraph("Tutorial 1");
		graph.addEdge("AB", "A", "B");
		graph.addEdge("BC", "B", "C", true); // Directed edge.
		graph.addEdge("CA", "C", "A");

Maven : mvn exec:java -Dexec.mainClass="org.graphstream.demo.tutorial1.Tutorial1"

Basic tasks with GS

We can improve the display with some CSS:


	"edge { fill-color: grey; }");


Basic tasks with GS

Basic tasks with GS

Basic tasks with GS

Edge ab = graph.getEdge("AB");
Edge bc = graph.getEdge("BC");
Edge ca = graph.getEdge("CA");

ab.setAttribute("ui.label", "AB");
bc.setAttribute("ui.label", "BC");
ca.setAttribute("ui.label", "CA");

Basic tasks with GS

ab.setAttribute("aNumber", 10);
bc.setAttribute("anObject", new Double(10));
ca.setAttribute("anArrayOfThings", 1, 2, 3);

Basic tasks with GS

int value1 = ((Number) ab.getAttribute("aNumber")).intValue();
double value2 = bc.getAttribute("anObject");
Object[] value3 = ca.getAttribute("anArrayOfThings");

double value4 = ab.getNumber("aNumber");
double value5 = bc.getNumber("anObject");

Basic tasks with GS

for(Node n: graph) {

for(Edge e: graph.getEachEdge()) {

Basic tasks with GS

Iterator<? extends Node> nodes = graph.getNodeIterator();

while(nodes.hasNext()) {

Iterator<? extends Edge> edges = graph.getEdgeIterator();

while(edges.hasNext()) {

Basic tasks with GS

int n = graph.getNodeCount();
for(int i=0; i<n; i++) {

int n = graph.getEdgeCount();
for(int i=0; i<n; i++) {

Be careful: indices remain the same as long as the graph is unchanged. But as soon as an addition or removal occurs, indices are no longer tied to their old node or edge.

Basic tasks with GS

import static org.graphstream.algorithm.Toolkit.*;
Node node = randomNode(graph);

for(Edge e: node.getEachEdge()) {
	System.out.printf("neighbor %s via %s%n",
		e.getId() );

Basic tasks with GS

Node node = getRandomNode(graph);
Iterator<? extends Edge> edges = node.getLeavingEdgeIterator();

Iterator<? extends Edge> edges = node.getEnteringEdgeIterator();

System.out.println(“Node degree %d (entering %d, leaving %d)%n”,

Tutorial 2

A first dynamic graph

A first dynamic graph

A first dynamic graph

An element sink must follow the interface:

public interface ElementSink {
	void nodeAdded( ... );
	void nodeRemoved( ... );
	void edgeAdded( ... );
	void edgeRemoved( ... );
	void graphCleared( ... );
	void stepBegins( ... );

A first dynamic graph

An attribute sink must follow the interface:

public interface AttributeSink {
	void graphAttributeAdded( ... );
	void graphAttributeChanged( ... );
	void graphAttributeRemoved( ... );

	void nodeAttributeAdded( ... );
	void nodeAttributeChanged( ... );
	void nodeAttributeRemoved( ... );

	void edgeAttributeAdded( ... );
	void edgeAttributeChanged( ... );
	void edgeAttributeRemoved( ... );

A first dynamic graph

A source is an interface that only defines methods to handle a set of sinks.

public interface Source {
	void addSink(Sink sink);
	void removeSink(Sink sink);
	void addAttributeSink(AttributeSink sink);
	void removeAttributeSink(AttributeSink sink);
	void addElementSink(ElementSink sink);
	void removeElementSink(ElementSink sink);
	void clearElementSinks();
	void clearAttributeSinks();
	void clearSinks();

A first dynamic graph

A first dynamic graph

A first dynamic graph

A DGS file looks like this. You can find it in the data > tutorial2.dgs

"Tutorial 2" 0 0
an "A"
an "B"
an "C"
ae "AB" "A" "B"
ae "BC" "B" "C"
ae "CA" "C" "A"
ce "AB" label="AB"
ce "BC" label="BC"
ce "CA" label="CA"
  • an add a node and ae an edge.
  • ae "AB" "A" > "B" adds a directed edge.
  • cn, ce and cg change or add one or more attributes on a node, edge or graph.
  • dn and de allow to remove nodes, edges.

A first dynamic graph

A first dynamic graph

st 2
an "D"
an "E"
ae "BD" "B" "D"
ae "CE" "C" "E"
ae "DE" "D" "E"
de "AB"
dn "A"
st 3

A first dynamic graph"tutorial2.dgs");

A first dynamic graph

public class Tutorial2 {
	public static void main(String args[]) {
		Graph graph = new SingleGraph("Tutorial2");
		try {
			FileSource source = FileSourceFactory.sourceFor(
		} catch(Exception e) { e.printStackTrace(); }

A first dynamic graph

		while(source.nextEvents()) { Thread.sleep(1000); }

  		while(source.nextStep()) { Thread.sleep(1000); }

A first dynamic graph

an "A" x=0 y=1
an "B" x=1 y=-1
an "C" x=-1 y=-1

an "D" x=1 y=1 
an "E" x=-1 y=1

A first dynamic graph


Slides and Material

Get the Slides and Materials online: