ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html |
| Last Crawled | 2026-04-05 04:34:43 (1 day ago) |
| First Indexed | 2014-11-10 18:16:09 (11 years ago) |
| HTTP Status Code | 200 |
| Meta Title | bellman_ford — SciPy v1.17.0 Manual |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | scipy.sparse.csgraph.
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) |
| Markdown | [Skip to main content](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html#main-content)
Back to top
[  SciPy](https://docs.scipy.org/doc/scipy/index.html)
- [Installing](https://scipy.org/install/)
- [User Guide](https://docs.scipy.org/doc/scipy/tutorial/index.html)
- [API reference](https://docs.scipy.org/doc/scipy/reference/index.html)
- [Building from source](https://docs.scipy.org/doc/scipy/building/index.html)
- [Development](https://docs.scipy.org/doc/scipy/dev/index.html)
- [Release notes](https://docs.scipy.org/doc/scipy/release.html)
Choose version
- [GitHub](https://github.com/scipy/scipy "GitHub")
- [Scientific Python Forum](https://discuss.scientific-python.org/c/contributor/scipy/ "Scientific Python Forum")
- [Installing](https://scipy.org/install/)
- [User Guide](https://docs.scipy.org/doc/scipy/tutorial/index.html)
- [API reference](https://docs.scipy.org/doc/scipy/reference/index.html)
- [Building from source](https://docs.scipy.org/doc/scipy/building/index.html)
- [Development](https://docs.scipy.org/doc/scipy/dev/index.html)
- [Release notes](https://docs.scipy.org/doc/scipy/release.html)
Choose version
- [GitHub](https://github.com/scipy/scipy "GitHub")
- [Scientific Python Forum](https://discuss.scientific-python.org/c/contributor/scipy/ "Scientific Python Forum")
Search `Ctrl`\+`K`
Section Navigation
- [scipy](https://docs.scipy.org/doc/scipy/reference/main_namespace.html)
- [scipy.cluster](https://docs.scipy.org/doc/scipy/reference/cluster.html)
- [scipy.constants](https://docs.scipy.org/doc/scipy/reference/constants.html)
- [scipy.datasets](https://docs.scipy.org/doc/scipy/reference/datasets.html)
- [scipy.differentiate](https://docs.scipy.org/doc/scipy/reference/differentiate.html)
- [scipy.fft](https://docs.scipy.org/doc/scipy/reference/fft.html)
- [scipy.fftpack](https://docs.scipy.org/doc/scipy/reference/fftpack.html)
- [scipy.integrate](https://docs.scipy.org/doc/scipy/reference/integrate.html)
- [scipy.interpolate](https://docs.scipy.org/doc/scipy/reference/interpolate.html)
- [scipy.io](https://docs.scipy.org/doc/scipy/reference/io.html)
- [scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html)
- [scipy.ndimage](https://docs.scipy.org/doc/scipy/reference/ndimage.html)
- [scipy.odr](https://docs.scipy.org/doc/scipy/reference/odr.html)
- [scipy.optimize](https://docs.scipy.org/doc/scipy/reference/optimize.html)
- [scipy.signal](https://docs.scipy.org/doc/scipy/reference/signal.html)
- [scipy.sparse](https://docs.scipy.org/doc/scipy/reference/sparse.html)
- [scipy.spatial](https://docs.scipy.org/doc/scipy/reference/spatial.html)
- [scipy.special](https://docs.scipy.org/doc/scipy/reference/special.html)
- [scipy.stats](https://docs.scipy.org/doc/scipy/reference/stats.html)
- [SciPy API](https://docs.scipy.org/doc/scipy/reference/index.html)
- [Sparse arrays (`scipy.sparse`)](https://docs.scipy.org/doc/scipy/reference/sparse.html)
- [Compressed sparse graph routines (`scipy.sparse.csgraph`)](https://docs.scipy.org/doc/scipy/reference/sparse.csgraph.html)
- bellman\_ford
scipy.sparse.csgraph.
# bellman\_ford[\#](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html#bellman-ford "Link to this heading")
scipy.sparse.csgraph.bellman\_ford(*csgraph*, *directed\=True*, *indices\=None*, *return\_predecessors\=False*, *unweighted\=False*)[\#](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html#scipy.sparse.csgraph.bellman_ford "Link to this definition")
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
Try it in your browser\!
```
>>> 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)
```
Go Back
Open In Tab
[previous floyd\_warshall](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.floyd_warshall.html "previous page")
[next johnson](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.johnson.html "next page")
On this page
- [`bellman_ford`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html#scipy.sparse.csgraph.bellman_ford)
© Copyright 2008, The SciPy community.
Created using [Sphinx](https://www.sphinx-doc.org/) 8.1.3.
Built with the [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/index.html) 0.16.1. |
| Readable Markdown | scipy.sparse.csgraph.
scipy.sparse.csgraph.bellman\_ford(*csgraph*, *directed\=True*, *indices\=None*, *return\_predecessors\=False*, *unweighted\=False*)[\#](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html#scipy.sparse.csgraph.bellman_ford "Link to this definition")
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)
``` |
| Shard | 63 (laksa) |
| Root Hash | 12122434965281355463 |
| Unparsed URL | org,scipy!docs,/doc/scipy/reference/generated/scipy.sparse.csgraph.bellman_ford.html s443 |