The dijkstra method finds the shortest path in a graph between elements and accepts the following arguments:
Determine the shortest path between element 1 and element 2.
import numpy as np from scipy.sparse.csgraph import dijkstra from scipy.sparse import csr_matrix arr = np.array([ [0, 1, 2], [1, 0, 0], [2, 0, 0] ]) newarr = csr_matrix(arr) print(dijkstra(newarr, return_predecessors=True, indices=0)) |
Use the floyd_warshall() method to compute the shortest paths between all pairs of elements.
Determine the shortest paths between all pairs of elements.
import numpy as np from scipy.sparse.csgraph import floyd_warshall from scipy.sparse import csr_matrix arr = np.array([ [0, 1, 2], [1, 0, 0], [2, 0, 0] ]) newarr = csr_matrix(arr) print(floyd_warshall(newarr, return_predecessors=True)) |