如何使用邻接矩阵确定从一个节点到另一个节点的所有路由?
How can i determine all routes from one node to another by using adjacency matrix?
mat = [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]]
network image
如何根据邻接矩阵确定路线?
预先感谢您的关注..
预期结果:从 1 到 4:
1-2-4
1-3-4
您可以为此使用 networkx。
示例:
>>> import networkx as nx
>>> import numpy as np
>>> g = nx.from_numpy_array(np.array(mat))
>>> g = nx.relabel_nodes(g, {i:i+1 for i in range(len(mat))})
>>> list(nx.all_simple_paths(g, 1, 4))
[[1, 2, 4], [1, 3, 4]]
mat = [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]]
network image
如何根据邻接矩阵确定路线?
预先感谢您的关注..
预期结果:从 1 到 4:
1-2-4
1-3-4
您可以为此使用 networkx。
示例:
>>> import networkx as nx
>>> import numpy as np
>>> g = nx.from_numpy_array(np.array(mat))
>>> g = nx.relabel_nodes(g, {i:i+1 for i in range(len(mat))})
>>> list(nx.all_simple_paths(g, 1, 4))
[[1, 2, 4], [1, 3, 4]]