BellmanFordSP Class |
The BellmanFordSP class represents a data type for solving the single-source shortest paths problem in edge-weighted digraphs with no negative cycles. The edge weights can be positive, negative, or zero. This class finds either a shortest path from the source vertex S to every other vertex or a negative cycle reachable from the source vertex.
This implementation uses the Bellman-Ford-Moore algorithm. The constructor takes time proportional to V (V + E) in the worst case, where V is the number of vertices and E is the number of edges. Afterwards, the distTo(), hasPathTo(), and hasNegativeCycle() methods take constant time; the pathTo() and negativeCycle() method takes time proportional to the number of edges returned.
Namespace: Algs4Net
public class BellmanFordSP
The BellmanFordSP type exposes the following members.
Name | Description | |
---|---|---|
![]() | BellmanFordSP | Computes a shortest paths tree from s to every other vertex in
the edge-weighted digraph G. |
Name | Description | |
---|---|---|
![]() | HasNegativeCycle |
Is there a negative cycle reachable from the source vertex s? |
Name | Description | |
---|---|---|
![]() | DistTo |
Returns the length of a shortest path from the source vertex s to vertex v. |
![]() | GetNegativeCycle |
Returns a negative cycle reachable from the source vertex s, or null
if there is no such cycle. |
![]() | HasPathTo |
Is there a path from the source s to vertex v? |
![]() ![]() | MainTest |
Demo test the BellmanFordSP data type. |
![]() | PathTo |
Returns a shortest path from the source s to vertex v. |
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
This class is a C# port from the original Java class BellmanFordSP implementation by the respective authors.