rustworkx.bellman_ford_shortest_paths#

bellman_ford_shortest_paths(graph, source, target=None, weight_fn=None, default_weight=1.0, as_undirected=False)[source]#

Find the shortest path from a node

This function will generate the shortest path from a source node using the Bellman-Ford algorithm wit the SPFA heuristic.

Parameters:
  • graph – The input graph to use. Can either be a PyGraph or PyDiGraph

  • source (int) – The node index to find paths from

  • target (int) – An optional target to find a path to

  • weight_fn – An optional weight function for an edge. It will accept a single argument, the edge’s weight object and will return a float which will be used to represent the weight/cost of the edge

  • default_weight (float) – If weight_fn isn’t specified this optional float value will be used for the weight/cost of each edge.

  • as_undirected (bool) – If set to true the graph will be treated as undirected for finding the shortest path. This only works with a PyDiGraph input for graph

Returns:

A read-only dictionary of paths. The keys are destination node indices and the dict values are lists of node indices making the path.

Return type:

PathMapping

Raises:

NegativeCycle: when there is a negative cycle and the shortest path is not defined