python - Dijkstra's Algorithm from Adjacency Matrix -
i have adjacency matrix of directed acyclic graph represented 2d array:
[[0, 4, 3, 0] [0, 0, 0, 1] [0, 3, 0, 1] [2, 0, 0, 0]]
is there python module or quick code snippet can run dijkstra's algorithm on info type? python cookbook uses priority dictionary believe, i'd maintain in 2d array. help appreciated.
networkx might fit needs:
import networkx nx import numpy np = np.array([[0, 4, 3, 0], [0, 0, 0, 1], [0, 3, 0, 1], [2, 0, 0, 0]]) g = nx.from_numpy_matrix(a, create_using=nx.digraph()) print(nx.dijkstra_path(g, 0, 1))
see also: networkx.dijkstra_path
python graph matrix dijkstra
No comments:
Post a Comment