| Home | OR-Objects | Tutorials | Prev | Next |
![]() |
In this tutorial you will learn how to use OR-Objects to build a graph like the one pictured above and use it in a shortest path algorithm. This graph has seven vertices which are labeled 'A' through 'G'. It has ten edges which connect the vertices and there is a cost associated with each edge. Eight of the edges are undirected and the two with arrows are directed. An undirected edge is one that can be crossed in both directions and a directed edge is one that can only be crossed in a single direction.
For example, it costs '2' to cross the edge between 'A' and 'B' and it can be crossed both ways. It costs '1' to cross the edge between 'E' and 'B', but it can only be crossed from 'E' to 'B'. The algorithm will find the shortest path from a beginning vertex to a ending vertex making sure it only crosses the edges in the correct direction. There are many other properties that can be associated with the vertices and edges, but this tutorial only uses cost to keep things simple. If you would like to see an example of a more complex problem see Tutorial-2.
Since this tutorial is about using OR-Objects and not writing applets
some of the applet details will be skipped. You can view the complete
source if you would like more details.
public class Tutorial_1
extends Applet
{
SingleVertexI
algorithm;
public void
init()
{
SparseGraph graph = new SparseGraph();
// Add the vertices
try{
graph.addVertex("a");
graph.addVertex("b");
graph.addVertex("c");
graph.addVertex("d");
graph.addVertex("e");
graph.addVertex("f");
graph.addVertex("g");
graph.addVertex("h");
}
catch(DuplicateVertexException e){}
// Add the edges
try{
graph.addEdge("a","b",new Double(2));
graph.addEdge("a","c",new Double(2));
graph.addEdge("d","b",new Double(3));
graph.addEdge("d","c",new Double(2));
graph.addEdge("d","e",new Double(2));
graph.addEdge("d","f",new Double(3));
graph.addEdge("g","e",new Double(2));
graph.addEdge("g","f",new Double(2));
graph.addEdge("e","b",new Double(1),true); // directed
graph.addEdge("c","f",new Double(1),true); // directed
}
catch(DuplicateEdgeException e){}
catch(VertexNotFoundException e){}
algorithm = new Dijkstra(graph); }
public void
displayPath(Object src, Object dst)
{
if(src.equals(dst)){
pathLabel.setText(src);
costLabel.setText("0.0");
return;
}
try{
algorithm.setCandidate(false);
algorithm.setCandidate(dst,
true);
algorithm.generatePathsFrom(src);
The algorithm has a 'generatePathsTo' that is not used here. It creates the paths starting at the candidates and ending at the destination vertex passed as the argument.
VertexI nearest = algorithm.getNearestCandidate();
double cost = algorithm.getEdgeValue(nearest).getCost(false);
costLabel.setText(String.valueOf(cost));
Vector path = algorithm.getPath(nearest); Enumeration enum = path.elements(); VertexI vertex = (VertexI)enum.nextElement(); String pathStr = String.valueOf(vertex.getKey());
while(enum.hasMoreElements()){
enum.nextElement();// Discard Edge //
vertex = (VertexI)enum.nextElement();
pathStr += " - " + vertex.getKey();
}
pathLabel.setText(pathStr);
}
catch(VertexNotFoundException e){}
catch(InvalidPropertyException e){}
}
![]() |
|
![]() |