|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.graphstream.algorithm.Dijkstra
public class Dijkstra
Dijkstra's algorithm is a greedy algorithm that solves the single-source shortest path problem for a directed graph with non negative edge weights (Wikipedia).
This length can be the absolute length of the path (a path with 3 edges has a length of 3), it can also be computed considering other constraints situated on the edges or on the nodes.
Note that Dijkstra's algorithm only computes with non-negative values.
The classical usage of this class takes place in 4 steps.
init(Graph)
method from the
Algorithm
interface.compute()
method
from the Algorithm
interfacegetShortestPath(Node)
method for instance.
The creation of the Dijkstra instance is done with the
Dijkstra(Element, String, String)
constructor by giving 3
parameters:
Dijkstra.Element
).import java.io.ByteArrayInputStream; import java.io.IOException; import org.graphstream.algorithm.Dijkstra; import org.graphstream.algorithm.Dijkstra.Element; import org.graphstream.graph.Graph; import org.graphstream.graph.implementations.DefaultGraph; import org.graphstream.stream.file.FileSourceDGS; // B-(1)-C // / \ // (1) (10) // / \ // A F // \ / // (1) (1) // \ / // D-(1)-E public class DijkstraTest { static String my_graph = "DGS004\n" + "my 0 0\n" + "an A \n" + "an B \n" + "an C \n" + "an D \n" + "an E \n" + "an F \n" + "ae AB A B weight:1 \n" + "ae AD A D weight:1 \n" + "ae BC B C weight:1 \n" + "ae CF C F weight:10 \n" + "ae DE D E weight:1 \n" + "ae EF E F weight:1 \n"; public static void main(String[] args) throws IOException { Graph graph = new DefaultGraph("Dijkstra Test"); ByteArrayInputStream bs = new ByteArrayInputStream(my_graph.getBytes()); FileSourceDGS source = new FileSourceDGS(); source.addSink(graph); source.readAll(bs); Dijkstra dijkstra = new Dijkstra(Element.edge, "weight", "A"); dijkstra.init(graph); dijkstra.compute(); System.out.println(dijkstra.getShortestPath(graph.getNode("F"))); } }
getShortestPathValue(Node)
method with its
given target element is for you.
getShortestPathLength(Node)
.
getShortestPath(String, Node, Node)
is a static method to get
shortest paths from a graph already computed with Dijkstra.
This means the given nodes (source and target) should belong to a graph that
contains attributes with the given "identifier" as a key.
It allows to get rid of Dijkstra instances once computed. Since all the
useful information to retrieve a shortest path is stored in the graph, a
Dijkstra instance is not necessary. You will use this method if you are
computing a lot of instances of Dijkstra in a raw and want to lower the
memory consumption.
The 3 following parameters are necessary to this method:
getAllShortestPaths(Node)
tries to construct all the possible shortest
paths linking the computed source to the given destination.
getEdgeSetShortestPaths(Node)
methods returns the set of edges that
are involved in the shortest paths. If Several paths are the shortest paths
and some edges belong to several of these paths, then those edge will appear
only once. In other words, the return List is a set according to the
mathematical definition of a set (no repetition, no order).
Nested Class Summary | |
---|---|
static class |
Dijkstra.Element
This enumeration help identifying the kind of element to be used to compute the shortest path. |
Constructor Summary | |
---|---|
Dijkstra(Dijkstra.Element element,
String attribute)
Same as Dijkstra(Element, String, String) but source node id
will be set to null. |
|
Dijkstra(Dijkstra.Element element,
String attribute,
String sourceNodeId)
Computes the Dijkstra's algorithm on the given graph starting from the given source node, considering the given attribute locates on the given kind of elements (nodes or edges). |
Method Summary | |
---|---|
void |
compute()
Computes the Dijkstra's algorithm on the given graph starting from the given source node, considering the given attribute locates on the given kind of elements (nodes or edges). |
List<org.graphstream.graph.Path> |
getAllShortestPaths(org.graphstream.graph.Node end)
This method tries to construct ALL the possible paths form the source to end . |
List<org.graphstream.graph.Edge> |
getEdgeSetShortestPaths(org.graphstream.graph.Node target)
Returns a set of edges that compose the shortest path. |
String |
getIdentifier()
The Identifier is a string which is used to mark the graph with the computed Dijkstra's shortest tree. |
org.graphstream.graph.Path |
getShortestPath(org.graphstream.graph.Node target)
Returns the shortest path between the source node and one given target one. |
static org.graphstream.graph.Path |
getShortestPath(String identifier,
org.graphstream.graph.Node source,
org.graphstream.graph.Node target)
A Static method to get shortest paths from a graph already computed with Dijkstra. |
double |
getShortestPathLength(org.graphstream.graph.Node target)
Returns the number of edges in the shortest path from the source to the given target.If target is not in the same connected
component as the source node, then the method returns
Double.POSITIVE_INFINITY . |
List<org.graphstream.graph.Edge> |
getShortestPaths(org.graphstream.graph.Node target)
Deprecated. |
double |
getShortestPathValue(org.graphstream.graph.Node target)
Returns the value of the shortest path between the source node and the given target according to the attribute specified in the constructor. |
void |
init(org.graphstream.graph.Graph graph)
Initialization of the algorithm. |
void |
setIdentifier(String identifier)
Set the unique identifier for this instance of Dijkstra. |
void |
setSource(String sourceNodeId)
Set the id of the source node which will be used on the computation. |
List<org.graphstream.graph.Edge> |
treeEdges()
Return the set of edges involved in the shortest path tree. |
double |
treeWeight()
Returns the weight of the shortest path tree : The sum of the weight of all the elements (nodes of edges, depending on the initialization). |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Dijkstra(Dijkstra.Element element, String attribute)
Dijkstra(Element, String, String)
but source node id
will be set to null.
element
- The kind of element observed in the graph.attribute
- The attribute considered for the distance computation.public Dijkstra(Dijkstra.Element element, String attribute, String sourceNodeId)
element
- The kind of element observed in the graph.attribute
- The attribute considered for the distance computation.sourceNodeId
- Id of the root node of the shortest path tree.Method Detail |
---|
public String getIdentifier()
getShortestPath(String,Node,Node)
public void setIdentifier(String identifier)
identifier
- the unique identifier to setgetIdentifier()
,
getShortestPath(String,Node,Node)
public void setSource(String sourceNodeId)
sourceNodeId
- id of the source nodepublic org.graphstream.graph.Path getShortestPath(org.graphstream.graph.Node target)
target
- the target of the shortest path starting at the source node
given in the constructor.
Path
object that constrains the
list of nodes and edges that constitute it.public static org.graphstream.graph.Path getShortestPath(String identifier, org.graphstream.graph.Node source, org.graphstream.graph.Node target)
identifier
as a key.
identifier
- The unique string used to identify attributes that represent
the solution of a Dijkstra computation (a shortest tree rooted
in the source node).source
- The source node for what a Dijkstra object as already been
initialized.target
- The Target node to be sought in the graph according the given
identifier.
getIdentifier()
public double treeWeight()
public List<org.graphstream.graph.Edge> treeEdges()
public List<org.graphstream.graph.Path> getAllShortestPaths(org.graphstream.graph.Node end)
end
.
This may result in a huge number of paths, you may even crash the VM because of memory consumption.
end
- The destination to which shortest paths are computed.
Path
objects. The List is empty if
end
is not in the same connected component as the
source node.@Deprecated public List<org.graphstream.graph.Edge> getShortestPaths(org.graphstream.graph.Node target)
getEdgeSetShortestPaths(Node)
.
target
- The target node for the shortest path.
getEdgeSetShortestPaths(Node)
public List<org.graphstream.graph.Edge> getEdgeSetShortestPaths(org.graphstream.graph.Node target)
target
- The endpoint of the path to compute from the source node given
in the constructor.
public double getShortestPathValue(org.graphstream.graph.Node target)
target
is not in the same connected component as the source
node, then the method returns Double.POSITIVE_INFINITY
.
target
- The endpoint of the path to compute from the source node given
in the constructor.
public double getShortestPathLength(org.graphstream.graph.Node target)
target
is not in the same connected
component as the source node, then the method returns
Double.POSITIVE_INFINITY
.
target
- The node to compute the shortest path to.
public void init(org.graphstream.graph.Graph graph)
Algorithm
Algorithm.compute()
method to initialize or reset the algorithm according
to the new given graph.
init
in interface Algorithm
graph
- The graph this algorithm is using.public void compute()
compute
in interface Algorithm
Algorithm.compute()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |