Click or drag to resize
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.

Inheritance Hierarchy
SystemObject
  Algs4NetBellmanFordSP

Namespace: Algs4Net
Assembly: Algs4Net (in Algs4Net.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
C#
public class BellmanFordSP

The BellmanFordSP type exposes the following members.

Constructors
  NameDescription
Public methodBellmanFordSP
Computes a shortest paths tree from s to every other vertex in the edge-weighted digraph G.
Top
Properties
  NameDescription
Public propertyHasNegativeCycle
Is there a negative cycle reachable from the source vertex s?
Top
Methods
  NameDescription
Public methodDistTo
Returns the length of a shortest path from the source vertex s to vertex v.
Public methodGetNegativeCycle
Returns a negative cycle reachable from the source vertex s, or null if there is no such cycle.
Public methodHasPathTo
Is there a path from the source s to vertex v?
Public methodStatic memberMainTest
Demo test the BellmanFordSP data type.
Public methodPathTo
Returns a shortest path from the source s to vertex v.
Top
Remarks

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.

See Also