org.graphstream.algorithm
Class Dijkstra

java.lang.Object
  extended by org.graphstream.algorithm.Dijkstra
All Implemented Interfaces:
Algorithm

public class Dijkstra
extends Object
implements Algorithm

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.

Usage

The classical usage of this class takes place in 4 steps.

  1. Definition of a Dijkstra instance with parameters needed for the initialization.
  2. Initialization of the algorithm with a graph through the init(Graph) method from the Algorithm interface.
  3. Computation of the shortest path tree with the compute() method from the Algorithm interface
  4. Retrieving of shortest paths for given destinations with the getShortestPath(Node) method for instance.

The creation of the Dijkstra instance is done with the Dijkstra(Element, String, String) constructor by giving 3 parameters:

Example

 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")));
        }
 }
 

Some Features

Shortest Path Value

If you only need to know the value of a shortest path and are not interested in the path itself, then the getShortestPathValue(Node) method with its given target element is for you.

ShortestPath Length

If you are interested in the length of a path in terms of elements nodes or edges rather than in the path itself or its value, then you can use getShortestPathLength(Node).

Static Access

The 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:

All Shortest Paths

It is possible that multiple path in a graph are the shortest ones. Especially if the graph is a grid and the weight is unitary. The getAllShortestPaths(Node) tries to construct all the possible shortest paths linking the computed source to the given destination.

All Edges involved

The 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).

Author:
Antoine Dutot, Yoann Pigné
Computational Complexity :
O(n log(n) + m) with n the number of nodes and m the number of edges.
Scientific Reference :
E. W. Dijkstra. A note on two problems in connexion with graphs. Numerische Mathematik, 1:269–271, 1959.

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

Dijkstra

public Dijkstra(Dijkstra.Element element,
                String attribute)
Same as Dijkstra(Element, String, String) but source node id will be set to null.

Parameters:
element - The kind of element observed in the graph.
attribute - The attribute considered for the distance computation.

Dijkstra

public 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).

Parameters:
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

getIdentifier

public String getIdentifier()
The Identifier is a string which is used to mark the graph with the computed Dijkstra's shortest tree. This string is the key of the attribute that the algorithm sets in the elements of the graph. The main advantage of this identifier is that once Dijkstra is computed on a graph for one given source, it is then only necessary to browse the graph according to this identifier; the Dijkstra instance is no longer necessary since all the useful information is already in the graph. Warning: If multiple instances of Dijkstra are run on the same graph (with different sources or different wait values) identifiers have to be unique! By default the identified is set to be unique.

Returns:
the unique identifier of this instance of Dijkstra.
See Also:
getShortestPath(String,Node,Node)

setIdentifier

public void setIdentifier(String identifier)
Set the unique identifier for this instance of Dijkstra.

Parameters:
identifier - the unique identifier to set
See Also:
getIdentifier(), getShortestPath(String,Node,Node)

setSource

public void setSource(String sourceNodeId)
Set the id of the source node which will be used on the computation.

Parameters:
sourceNodeId - id of the source node

getShortestPath

public org.graphstream.graph.Path getShortestPath(org.graphstream.graph.Node target)
Returns the shortest path between the source node and one given target one. If multiple shortest paths exist, a of them is returned at random.

Parameters:
target - the target of the shortest path starting at the source node given in the constructor.
Returns:
A Path object that constrains the list of nodes and edges that constitute it.

getShortestPath

public 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. This means that given nodes (source and target) should belong to a graph that contains attributes with the given identifier as a key.

What is this method useful for?

It allows to get rid of the 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.

Parameters:
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.
Returns:
a shortest path linking the source to the target. The returned path can be empty, if the no path can link the source and the target.
See Also:
getIdentifier()

treeWeight

public 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).

Returns:
the sum of the weights of the all shortest path tree.

treeEdges

public List<org.graphstream.graph.Edge> treeEdges()
Return the set of edges involved in the shortest path tree.

Returns:
The set of edges.

getAllShortestPaths

public 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.

WARNING

This may result in a huge number of paths, you may even crash the VM because of memory consumption.

Parameters:
end - The destination to which shortest paths are computed.
Returns:
a list of shortest paths given with Path objects. The List is empty if end is not in the same connected component as the source node.

getShortestPaths

@Deprecated
public List<org.graphstream.graph.Edge> getShortestPaths(org.graphstream.graph.Node target)
Deprecated. 

Synonym to getEdgeSetShortestPaths(Node).

Parameters:
target - The target node for the shortest path.
Returns:
A list of edges.
See Also:
getEdgeSetShortestPaths(Node)

getEdgeSetShortestPaths

public List<org.graphstream.graph.Edge> getEdgeSetShortestPaths(org.graphstream.graph.Node target)
Returns a set of edges that compose the shortest path. If more than one path is the shortest one, the edges are included in the returned set of edges.

Parameters:
target - The endpoint of the path to compute from the source node given in the constructor.
Returns:
The set of edges that belong the the solution. Returns an empty list if the target node is not in the same connected component as the source node. Returns null if target is the same as the source node.

getShortestPathValue

public 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. If target is not in the same connected component as the source node, then the method returns Double.POSITIVE_INFINITY.

Parameters:
target - The endpoint of the path to compute from the source node given in the constructor.
Returns:
A numerical value that represent the distance of the shortest path.

getShortestPathLength

public 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.

Parameters:
target - The node to compute the shortest path to.
Returns:
the number of edges in the shortest path.

init

public void init(org.graphstream.graph.Graph graph)
Description copied from interface: Algorithm
Initialization of the algorithm. This method has to be called before the Algorithm.compute() method to initialize or reset the algorithm according to the new given graph.

Specified by:
init in interface Algorithm
Parameters:
graph - The graph this algorithm is using.

compute

public 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).

Specified by:
compute in interface Algorithm
See Also:
Algorithm.compute()


Copyright © 2011. All Rights Reserved.