[ Home ] | [ Concepts ] | [ Tutorial ] | [ API ] | [ Download ] | [ Resources ] | [ Credits ] |
Position
.
In order to manipulate the graph (or any of its edges and vertices), we have to call methods of the graph.
boolean directed=false; GraphIf one wants to creat a directed graph, only the boolean valueg = new DefaultGraph (directed); Position vertA = g.insertVertex("A"); Position vertB = g.insertVertex("B"); Position vertC = g.insertVertex("C"); Position vertD = g.insertVertex("D"); Position vertE = g.insertVertex("E"); Position vertF = g.insertVertex("F"); g.insertEdge(0,vertA,vertE); g.insertEdge(0,vertE,vertD); g.insertEdge(0,vertC,vertE); g.insertEdge(0,vertC,vertD); g.insertEdge(0,vertB,vertC); g.insertEdge(0,vertA,vertB); g.insertEdge(0,vertA,vertF); g.insertEdge(0,vertA,vertF);
directed
has to be changes.
It is also possible to change a directed edge into an undirected one or vice-versa using the two methods: graph.setDirected(edge)
and graph.setUndirected(edge)
.
g.vertices()
can be used to visit all of the vertices of a graph or g.edges()
for all of its edges.ListThe following program presents an implementation of the breadth first search algorithm. In this version, we use a buffer (which is a sequence) to store vertices. We also have a sequence result that stores the returned value. We need in that version a hash table for "marking" the visited vertices, we use a java.util.HashSet to do this.> vertices = g.vertices(); for(Position v : vertices){ // Do something with v }
for(Positionv2 : g.adjacentVertices(v,Direction.ALL)){ // Work using v2 }
Graph g = ...; SequenceUsing a hash table to mark the vertices of a graph is not object oriented, even if it works and is quite efficient. We suggest never to use such a system, but instead to use the decorator pattern as it is presented in the chapter Add properties to the Edges or the Vertices> buffer = new LinkedSequence >(); Sequence > result = new LinkedSequence >(); Set > bufferedVertices = new HashSet >(); List > vertices = g.vertices(); for(Position v : vertices){ if(! bufferedVertices.contains(v)){ buffer.add(v); bufferedVertices.add(v); while(! buffer.isEmpty()){ Position > pFirst= buffer.first(); v = (Position )buffer.element(pFirst); buffer.delete(pFirst); result.add(v); for(Position v2 : g.adjacentVertices(v,Direction.ALL)){ if(!bufferedVertices.contains(v2) && v2 !=null){ bufferedVertices.add(v2); buffer.add(v2); } } } }
Examples:
HashMap
. (java source file)