Note

This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.

rustworkx.graph_single_source_all_shortest_paths#

graph_single_source_all_shortest_paths(graph, source, /, weight_fn=None, default_weight=1.0)#

Find all shortest paths from a single source node to all other nodes in an undirected graph

This function will generate all possible shortest paths from a source node to all other nodes using Dijkstra’s algorithm.

Parameters:
  • graph (PyGraph) – The input undirected graph.

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

  • 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.

Returns:

A dictionary where keys are node indices and values are lists of all shortest paths from the source to that node. Each path is a list of node indices starting with the source.

Return type:

dict

Raises:
  • ValueError – when an edge weight with NaN or negative value is provided.

  • IndexError – if the source node index is out of range.

Warning

This function can return an exponential number of paths in certain graphs, especially with zero-weight edges. For most use cases, consider using rustworkx.dijkstra_shortest_paths for a single shortest path, which runs much faster.