如何计算邻接矩阵的短路测地距离csv [python]? [英] how to calculate short path geodesic distance of adjacency matrix csv [python]?
问题描述
我有一个图的邻接矩阵
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 1 1 0 1 0 0 0 0 0
4 1 0 1 0 1 1 0 0 0
5 0 0 0 1 0 1 1 1 0
6 0 0 0 1 1 0 1 1 0
7 0 0 0 0 1 1 0 1 1
8 0 0 0 0 1 1 1 0 0
9 0 0 0 0 0 0 1 0 0
如何使用python将其转换为测地点矩阵? p>
我的目标是让它像这样:
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 2 2 3 3 4
2 1 0 1 2 3 3 4 4 5
3 1 1 0 1 2 2 3 3 4
4 1 2 1 0 1 1 2 2 3
5 2 3 2 1 0 1 1 1 2
6 2 3 2 1 1 0 1 1 2
7 3 4 3 2 1 1 0 1 1
8 3 4 3 2 1 1 1 0 2
9 4 5 4 3 2 2 1 2 0
我试过在networkx中的一些代码,但它只能计算一个源和一个目的地(n)不是整个矩阵。我真的需要你的帮助。
谢谢
networkx
可以计算整个矩阵。一个只是不需要给源或目标到 nx.shortest_path
函数(参见 https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.generic .shortest_path.html - 上一个示例)。这是我的解决方案:
import pprint
import networkx as nx
import pandas as pd
import numpy as np
mat = pd.read_csv('adjacency.csv',index_col = 0,delim_whitespace = True).values
G = nx.from_numpy_matrix(mat)
p = nx.shortest_path (b)
short_path_mat = np.zeros(mat.shape)
对于i在范围内(mat.shape [0]):
shortest_path_mat [i,:] = np.array len(x)for x in p [i] .values()])
pprint.pprint(shortest_path_mat-1)
adjacency.csv
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 1 1 0 1 0 0 0 0 0
4 1 0 1 0 1 1 0 0 0
5 0 0 0 1 0 1 1 1 0
6 0 0 0 1 1 0 1 1 0
7 0 0 0 0 1 1 0 1 1
8 0 0 0 0 1 1 1 0 0
9 0 0 0 0 0 0 1 0 0
i have an adjacency matrix of graph
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 1 1 0 1 0 0 0 0 0
4 1 0 1 0 1 1 0 0 0
5 0 0 0 1 0 1 1 1 0
6 0 0 0 1 1 0 1 1 0
7 0 0 0 0 1 1 0 1 1
8 0 0 0 0 1 1 1 0 0
9 0 0 0 0 0 0 1 0 0
how to convert it to geodesic discance matrix using python?
my goal is to make it like this :
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 2 2 3 3 4
2 1 0 1 2 3 3 4 4 5
3 1 1 0 1 2 2 3 3 4
4 1 2 1 0 1 1 2 2 3
5 2 3 2 1 0 1 1 1 2
6 2 3 2 1 1 0 1 1 2
7 3 4 3 2 1 1 0 1 1
8 3 4 3 2 1 1 1 0 2
9 4 5 4 3 2 2 1 2 0
i've tried some code in networkx but it only can calculate at one source and one destination of (n) not the whole matrix. I really need your help. Thank you
networkx
can calculate the whole matrix. One just need not to give source or destination to the nx.shortest_path
function (see https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.generic.shortest_path.html - last example). Here's my solution:
import pprint
import networkx as nx
import pandas as pd
import numpy as np
mat = pd.read_csv('adjacency.csv', index_col=0, delim_whitespace=True).values
G = nx.from_numpy_matrix(mat)
p = nx.shortest_path(G)
shortest_path_mat = np.zeros(mat.shape)
for i in range(mat.shape[0]):
shortest_path_mat[i, :] = np.array([len(x) for x in p[i].values()])
pprint.pprint(shortest_path_mat-1)
adjacency.csv
n 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 1 1 0 1 0 0 0 0 0
4 1 0 1 0 1 1 0 0 0
5 0 0 0 1 0 1 1 1 0
6 0 0 0 1 1 0 1 1 0
7 0 0 0 0 1 1 0 1 1
8 0 0 0 0 1 1 1 0 0
9 0 0 0 0 0 0 1 0 0
这篇关于如何计算邻接矩阵的短路测地距离csv [python]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!