networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturns
_bellman_ford(G, source, weight, pred=None, paths=None, dist=None, target=None, heuristic=True)

This is an implementation of the SPFA variant. See https://en.wikipedia.org/wiki/Shortest_Path_Faster_Algorithm

Parameters

G : NetworkX graph
source: list :

List of source nodes. The shortest path from any of the source nodes will be found if multiple sources are provided.

weight : function

The weight of an edge is the value returned by the function. The function must accept exactly three positional arguments: the two endpoints of an edge and the dictionary of edge attributes for that edge. The function must return a number.

pred: dict of lists, optional (default=None) :

dict to store a list of predecessors keyed by that node If None, predecessors are not stored

paths: dict, optional (default=None) :

dict to store the path list from source to each node, keyed by node If None, paths are not stored

dist: dict, optional (default=None) :

dict to store distance from source to the keyed node If None, returned dist dict contents default to 0 for every node in the source list

target: node label, optional :

Ending node for path. Path lengths to other destinations may (and probably will) be incorrect.

heuristic : bool

Determines whether to use a heuristic to early detect negative cycles at a hopefully negligible cost.

Raises

NodeNotFound

If any of :None:None:`source` is not in G.

NetworkXUnbounded

If the (di)graph contains a negative (di)cycle, the algorithm raises an exception to indicate the presence of the negative (di)cycle. Note: any negative weight edge in an undirected graph is a negative cycle

Returns

dist : dict

Returns a dict keyed by node to the distance from the source. Dicts for paths and pred are in the mutated input dicts by those names.

Calls relaxation loop for Bellman–Ford algorithm and builds paths

Examples

See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/shortest_paths/weighted.py#1238
type: <class 'function'>
Commit: