Menu

Using

Tim Schäfer Philipp Hülsdunk

Using

The first step to be is to create a graph. The graph classes are included in the [datastructues] package. To create an undirected graph that uses an adjacency list you should write:

UAdjListGraph g = new UAdjListGraph();

Vertices and edges can be constructed using the graph:

UAdjListGraph.Vertice v1 = g.createVertex();
//...
UAdjListGraph.Edge e1 = g.createEdge(v1, v2);
//...

To make things interesting, we can assign values to vertices and edges. For example, we can add weights to the edges. This is done by creating an edge map, which will work for us as a property map having improved runtime accessing edges as keys:

Map<UAdjListGraph.Edge, Double> weightMap = g.createEdgeMap();
weightMap.put(e1, 2.4);

We can also do the same thing with vertices by calling g.createVertexMap().

Running [algorithms] is easy, they are methods with the graph and transformers as parameters. Transformers will form a vertex or edge to a certain value, for example, Dijkstra's algorithm will be passed a transformer forming from an edge a value for its weight. Since we have saved our weights into a map we can wrap this instance into a MapTransformer to get the desired transformer:

DistanceVector paths = SingleSourceShortestPath.Dijkstra(
        g, new MapTransformer<>(weightMap), v1);
List<E> pathToV2 = paths.getPathTo(v2);
List<E> pathToV3 = paths.getPathTo(v3);

Related

Wiki: Home
Wiki: algorithms