Class NegativeCycleException

  • All Implemented Interfaces:
    Serializable

    public class NegativeCycleException
    extends RuntimeException
    Exception thrown when a negative cycle is detected in a graph during a shortest path computation.

    Negative weights are supported by some implementations of shortest path algorithm, and the 'shortest path' is well defined as long as there are no negative cycle in the graph as a path can loop in the cycle and achieve arbitrary small 'length'. When negative cycles exists, algorithms will raise NegativeCycleException exception.

    Author:
    Barak Ugav
    See Also:
    ShortestPathSingleSource, ShortestPathAllPairs, Serialized Form
    • Constructor Detail

      • NegativeCycleException

        public NegativeCycleException​(Graph<V,​E> graph,
                                      Path<V,​E> cycle)
        Create a new instance of the exception.
        Type Parameters:
        V - the vertices type
        E - the edges type
        Parameters:
        graph - the graph on which the negative cycle was detected
        cycle - the negative cycle that was found in the graph
    • Method Detail

      • cycle

        public Path<?,​?> cycle()
        Get the negative cycle that was found in the graph.
        Returns:
        the negative cycle that was found in the graph.
      • cycle

        public <V,​E> Path<V,​E> cycle​(Graph<V,​E> g)
        Get the negative cycle that was found in the graph in type generic safe way.

        by passing the graph on which the exception was thrown to this method, we can safely conclude the generic types of the return path and avoid unchecked casts.

        Type Parameters:
        V - the vertices type
        E - the edges type
        Parameters:
        g - the graph on which the exception was thrown
        Returns:
        the negative cycle that was found in the graph.