Skip to main content
`Ctrl`+`K`
scipy.sparse.csgraph.

# bellman_ford

scipy.sparse.csgraph.bellman_ford(csgraph, directed=True, indices=None, return_predecessors=False, unweighted=False)

  Compute the shortest path lengths using the Bellman-Ford algorithm.

  The Bellman-Ford algorithm can robustly deal with graphs with negative weights. If a negative cycle is detected, an error is raised. For graphs without negative edge weights, Dijkstra’s algorithm may be faster.

  Added in version 0.11.0.

  Parameters:
    csgraph array_like, or sparse array or matrix, 2 dimensions

      The N x N array of distances representing the input graph.

    directed bool, optional

      If True (default), then find the shortest path on a directed graph: only move from point i to point j along paths csgraph[i, j]. If False, then find the shortest path on an undirected graph: the algorithm can progress from point i to j along csgraph[i, j] or csgraph[j, i]

    indices array_like or int, optional

      if specified, only compute the paths from the points at the given indices.

    return_predecessors bool, optional

      If True, return the size (N, N) predecessor matrix.

    unweighted bool, optional

      If True, then find unweighted distances. That is, rather than finding the path between each point such that the sum of weights is minimized, find the path such that the number of edges is minimized.

  Returns:
    dist_matrix ndarray

      The N x N matrix of distances between graph nodes. dist_matrix[i,j] gives the shortest distance from point i to point j along the graph.

    predecessors ndarray, shape (n_indices, n_nodes,)

      Returned only if `return_predecessors=True`. If indices is None then `n_indices = n_nodes` and the shape of the matrix becomes `(n_nodes, n_nodes)`. The matrix of predecessors, which can be used to reconstruct the shortest paths. Row i of the predecessor matrix contains information on the shortest paths from point i: each entry predecessors[i, j] gives the index of the previous node in the path from point i to point j. If no path exists between point i and j, then predecessors[i, j] = -9999

  Raises:
    NegativeCycleError:

      if there are negative cycles in the graph

  Notes

  This routine is specially designed for graphs with negative edge weights. If all edge weights are positive, then Dijkstra’s algorithm is a better choice.

  If multiple valid solutions are possible, output may vary with SciPy and Python version.

  Examples

      >>> from scipy.sparse import csr_array
      >>> from scipy.sparse.csgraph import bellman_ford

      >>> graph = [
      ... [0, 1 ,2, 0],
      ... [0, 0, 0, 1],
      ... [2, 0, 0, 3],
      ... [0, 0, 0, 0]
      ... ]
      >>> graph = csr_array(graph)
      >>> print(graph)
      <Compressed Sparse Row sparse array of dtype 'int64'
          with 5 stored elements and shape (4, 4)>
          Coords  Values
          (0, 1)  1
          (0, 2)  2
          (1, 3)  1
          (2, 0)  2
          (2, 3)  3

      >>> dist_matrix, predecessors = bellman_ford(csgraph=graph, directed=False, indices=0, return_predecessors=True)
      >>> dist_matrix
      array([0., 1., 2., 2.])
      >>> predecessors
      array([-9999,     0,     0,     1], dtype=int32)