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.digraph_floyd_warshall_successor_and_distance#
- digraph_floyd_warshall_successor_and_distance(graph, /, weight_fn=None, as_undirected=False, default_weight=1.0, parallel_threshold=300)#
Find all-pairs shortest path lengths using Floyd’s algorithm
Floyd’s algorithm is used for finding shortest paths in dense graphs or graphs with negative weights (where Dijkstra’s algorithm fails).
This function is multithreaded and will launch a pool with threads equal to the number of CPUs by default if the number of nodes in the graph is above the value of
parallel_threshold
(it defaults to 300). You can tune the number of threads with theRAYON_NUM_THREADS
environment variable. For example, settingRAYON_NUM_THREADS=4
would limit the thread pool to 4 threads if parallelization was enabled.- Parameters:
graph (PyDiGraph) – The directed graph to run Floyd’s algorithm on
weight_fn –
A callable object (function, lambda, etc) which will be passed the edge object and expected to return a
float
. This tells rustworkx/rust how to extract a numerical weight as afloat
for edge object. Some simple examples are:graph_floyd_warshall_numpy(graph, weight_fn: lambda x: 1)
to return a weight of 1 for all edges. Also:
graph_floyd_warshall_numpy(graph, weight_fn: lambda x: float(x))
to cast the edge object as a float as the weight.
as_undirected – If set to true each directed edge will be treated as bidirectional/undirected.
parallel_threshold (int) – The number of nodes to execute the algorithm in parallel at. It defaults to 300, but this can be tuned
- Returns:
A tuple of two matrices. First one is a matrix of shortest path distances between nodes. If there is no path between two nodes then the corresponding matrix entry will be
np.inf
. Second one is a matrix of next nodes for given source and target. If there is no path between two nodes then the corresponding matrix entry will be the same as a target node. To reconstruct the shortest path among nodes:def reconstruct_path(source, target, successors): path = [] if source == target: return path curr = source while curr != target: path.append(curr) curr = successors[curr, target] path.append(target) return path
- Return type: