[英]Dijkstra's Algorithm from Adjacency Matrix
I have an adjacency matrix of a directed acyclic graph represented by a 2D array: 我有一个由2D数组表示的有向无环图的邻接矩阵:
[[0, 4, 3, 0]
[0, 0, 0, 1]
[0, 3, 0, 1]
[2, 0, 0, 0]]
Is there a Python module or a quick code snippet that can run Dijkstra's Algorithm on this data type? 是否存在可以在此数据类型上运行Dijkstra算法的Python模块或快速代码段? The Python cookbook uses a priority dictionary I believe, but I'd really like to keep it in a 2D array.
我相信Python食谱使用优先级字典,但我真的很想将其保存在2D数组中。 Any help would be appreciated.
任何帮助,将不胜感激。
networkx might fit your needs: networkx可能符合您的需求:
import networkx as nx
import numpy as np
A = 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 另请参阅: networkx.dijkstra_path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.