Class BellmanFordShortestPath<V,​E>

  • Type Parameters:
    V - the graph vertex type
    E - the graph edge type
    All Implemented Interfaces:
    ShortestPathAlgorithm<V,​E>

    public class BellmanFordShortestPath<V,​E>
    extends Object
    The Bellman-Ford algorithm.

    Computes shortest paths from a single source vertex to all other vertices in a weighted graph. The Bellman-Ford algorithm supports negative edge weights.

    Negative weight cycles are not allowed and will be reported by the algorithm. This implies that negative edge weights are not allowed in undirected graphs. In such cases the code will throw an exception of type NegativeCycleDetectedException which will contain the detected negative weight cycle. Note that the algorithm will not report or find negative weight cycles which are not reachable from the source vertex.

    The running time is $O(|E||V|)$.

    Author:
    Dimitrios Michail
    • Field Detail

      • maxHops

        protected final int maxHops
      • GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE

        protected static final String GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
        Error message for reporting the existence of a negative-weight cycle.
        See Also:
        Constant Field Values
      • GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX

        protected static final String GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
        Error message for reporting that a source vertex is missing.
        See Also:
        Constant Field Values
      • GRAPH_MUST_CONTAIN_THE_SINK_VERTEX

        protected static final String GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
        Error message for reporting that a sink vertex is missing.
        See Also:
        Constant Field Values
      • graph

        protected final Graph<V,​E> graph
        The underlying graph.
    • Constructor Detail

      • BellmanFordShortestPath

        public BellmanFordShortestPath​(Graph<V,​E> graph)
        Construct a new instance.
        Parameters:
        graph - the input graph
      • BellmanFordShortestPath

        public BellmanFordShortestPath​(Graph<V,​E> graph,
                                       double epsilon)
        Construct a new instance.
        Parameters:
        graph - the input graph
        epsilon - tolerance when comparing floating point values
      • BellmanFordShortestPath

        public BellmanFordShortestPath​(Graph<V,​E> graph,
                                       double epsilon,
                                       int maxHops)
        Construct a new instance.
        Parameters:
        graph - the input graph
        epsilon - tolerance when comparing floating point values
        maxHops - execute the algorithm for at most this many iterations. If this is smaller than the number of vertices, then the negative cycle detection feature is disabled.
        Throws:
        IllegalArgumentException - if the number of maxHops is not positive
    • Method Detail

      • getPath

        public GraphPath<V,​E> getPath​(V source,
                                            V sink)
        Get a shortest path from a source vertex to a sink vertex.
        Parameters:
        source - the source vertex
        sink - the target vertex
        Returns:
        a shortest path or null if no path exists
        Throws:
        NegativeCycleDetectedException - in case a negative weight cycle is detected
      • findPathBetween

        public static <V,​E> GraphPath<V,​E> findPathBetween​(Graph<V,​E> graph,
                                                                       V source,
                                                                       V sink)
        Find a path between two vertices.
        Type Parameters:
        V - the graph vertex type
        E - the graph edge type
        Parameters:
        graph - the graph to be searched
        source - the vertex at which the path should start
        sink - the vertex at which the path should end
        Returns:
        a shortest path, or null if no path exists
      • getPathWeight

        public double getPathWeight​(V source,
                                    V sink)
        Get the weight of the shortest path from a source vertex to a sink vertex. Returns Double.POSITIVE_INFINITY if no path exists.
        Specified by:
        getPathWeight in interface ShortestPathAlgorithm<V,​E>
        Parameters:
        source - the source vertex
        sink - the sink vertex
        Returns:
        the weight of the shortest path from a source vertex to a sink vertex, or Double.POSITIVE_INFINITY if no path exists
      • createEmptyPath

        protected final GraphPath<V,​E> createEmptyPath​(V source,
                                                             V sink)
        Create an empty path. Returns null if the source vertex is different than the target vertex.
        Parameters:
        source - the source vertex
        sink - the sink vertex
        Returns:
        an empty path or null null if the source vertex is different than the target vertex