Python:Networkx Dijkstra算法的问题
我的目标是做完全相同的事情张贴 我有一个矩阵(Python:Networkx Dijkstra算法的问题,python,dijkstra,networkx,Python,Dijkstra,Networkx,我的目标是做完全相同的事情张贴 我有一个矩阵(DataFrame或df)如下所示: community A B C D A 0 3 4 1 B 3 0 2 0 C 4 2 0 1 D 1 0 1 0 这是一个对称的df,每个权重表示每个社区之间连接的权重或强度。如链接中所述,我希望生成一个矩阵,显示所有节点(或社区)之间的最短距离。我首先反转上述矩阵并设置G图形网络:
DataFrame
或df
)如下所示:
community A B C D
A 0 3 4 1
B 3 0 2 0
C 4 2 0 1
D 1 0 1 0
这是一个对称的df
,每个权重表示每个社区之间连接的权重或强度。如链接中所述,我希望生成一个矩阵,显示所有节点(或社区)之间的最短距离。我首先反转上述矩阵并设置G
图形网络:
G = nx.Graph()
commu = list(df.index)
for i in range(0,len(commu)):
for j in range(0,len(commu)):
if i == j:
pass
else:
G.add_edge(str(i),str(j),weight=df.ix[df.index[i], df.columns[j]])
这给了我下面的网络图:边的不同颜色用于不同的权重。(我更改了图表中数字的字母)
好的,到目前为止还不错。现在,我想要所有节点之间的最短距离。我正在考虑使用这个nx.dijkstra\u最短路径长度(G,source,target)
并在source
和target
的所有节点上循环,生成一个类似于上述链接的矩阵,其中包含矩阵每个单元格中所有节点的最短路径的所有值,但出于某种原因nx.dijkstra\u最短路径长度(G,源,目标)
对我不起作用。
如果我使用
nx.dijkstra\u最短路径长度(G,A,B)
或节点的任何组合,我总是得到一个值0。为什么?有没有一种有效的方法可以使用Networkx
和nx.dijkstra
算法来复制链接中的矩阵?你可以使用Networkx.shortest\u路径(G)
带有weight='weight'关键字。
e、 g
您只需使用带有weight='weight'关键字的
networkx.shortest_path(G)
。
e、 g
您只需使用带有weight='weight'关键字的
networkx.shortest_path(G)
。
e、 g
您只需使用带有weight='weight'关键字的
networkx.shortest_path(G)
。
e、 g
(不是回答,只是长篇大论)。如果
而不是
G = nx.Graph()
commu = list(df.index)
for i in range(0,len(commu)):
for j in range(0,len(commu)):
if i == j:
pass
else:
G.add_edge(str(i),str(j),weight=df.ix[df.index[i], df.columns[j]])
您可以使用
In [66]: G = nx.from_numpy_matrix(df.values)
In [67]: G.edges(data=True)
Out[67]:
[(0, 1, {'weight': 3}),
(0, 2, {'weight': 4}),
(0, 3, {'weight': 1}),
(1, 2, {'weight': 2}),
(2, 3, {'weight': 1})]
如果要标记节点ABCD
,而不是0123
:
In [68]: G = nx.relabel_nodes(G, dict(zip(range(4), 'ABCD')))
In [69]: G.edges(data=True)
Out[69]:
[('A', 'C', {'weight': 4}),
('A', 'B', {'weight': 3}),
('A', 'D', {'weight': 1}),
('C', 'B', {'weight': 2}),
('C', 'D', {'weight': 1})]
(不是回答,只是长篇大论)。如果
而不是
G = nx.Graph()
commu = list(df.index)
for i in range(0,len(commu)):
for j in range(0,len(commu)):
if i == j:
pass
else:
G.add_edge(str(i),str(j),weight=df.ix[df.index[i], df.columns[j]])
您可以使用
In [66]: G = nx.from_numpy_matrix(df.values)
In [67]: G.edges(data=True)
Out[67]:
[(0, 1, {'weight': 3}),
(0, 2, {'weight': 4}),
(0, 3, {'weight': 1}),
(1, 2, {'weight': 2}),
(2, 3, {'weight': 1})]
如果要标记节点ABCD
,而不是0123
:
In [68]: G = nx.relabel_nodes(G, dict(zip(range(4), 'ABCD')))
In [69]: G.edges(data=True)
Out[69]:
[('A', 'C', {'weight': 4}),
('A', 'B', {'weight': 3}),
('A', 'D', {'weight': 1}),
('C', 'B', {'weight': 2}),
('C', 'D', {'weight': 1})]
(不是回答,只是长篇大论)。如果
而不是
G = nx.Graph()
commu = list(df.index)
for i in range(0,len(commu)):
for j in range(0,len(commu)):
if i == j:
pass
else:
G.add_edge(str(i),str(j),weight=df.ix[df.index[i], df.columns[j]])
您可以使用
In [66]: G = nx.from_numpy_matrix(df.values)
In [67]: G.edges(data=True)
Out[67]:
[(0, 1, {'weight': 3}),
(0, 2, {'weight': 4}),
(0, 3, {'weight': 1}),
(1, 2, {'weight': 2}),
(2, 3, {'weight': 1})]
如果要标记节点ABCD
,而不是0123
:
In [68]: G = nx.relabel_nodes(G, dict(zip(range(4), 'ABCD')))
In [69]: G.edges(data=True)
Out[69]:
[('A', 'C', {'weight': 4}),
('A', 'B', {'weight': 3}),
('A', 'D', {'weight': 1}),
('C', 'B', {'weight': 2}),
('C', 'D', {'weight': 1})]
(不是回答,只是长篇大论)。如果
而不是
G = nx.Graph()
commu = list(df.index)
for i in range(0,len(commu)):
for j in range(0,len(commu)):
if i == j:
pass
else:
G.add_edge(str(i),str(j),weight=df.ix[df.index[i], df.columns[j]])
您可以使用
In [66]: G = nx.from_numpy_matrix(df.values)
In [67]: G.edges(data=True)
Out[67]:
[(0, 1, {'weight': 3}),
(0, 2, {'weight': 4}),
(0, 3, {'weight': 1}),
(1, 2, {'weight': 2}),
(2, 3, {'weight': 1})]
如果要标记节点ABCD
,而不是0123
:
In [68]: G = nx.relabel_nodes(G, dict(zip(range(4), 'ABCD')))
In [69]: G.edges(data=True)
Out[69]:
[('A', 'C', {'weight': 4}),
('A', 'B', {'weight': 3}),
('A', 'D', {'weight': 1}),
('C', 'B', {'weight': 2}),
('C', 'D', {'weight': 1})]
因此,
nx.shortest_path_length
和nx.dijkstra_shortest_path_length
之间没有区别?感谢您的帮助如果您将weight=attribute关键字与nx.shortest_path_length一起使用,它将运行nx.dijkstra_shortest_path_length。默认值为weight=None,它将执行非加权最短路径计算。因此存在nonx.shortest_path_length
与nx.dijkstra_shortest_path_length
之间的差异?感谢您的帮助如果您将weight=attribute关键字与nx.shortest_path_length一起使用,它将运行nx.dijkstra_shortest_path_length。默认值为weight=None,它将执行非加权最短路径计算。因此没有差异在nx.shortest_path_length
和nx.dijkstra_shortest_path_length
之间?感谢您的帮助如果您将weight=attribute关键字与nx.shortest_path_length一起使用,它将运行nx.dijkstra_shortest_path_length。默认值为weight=None,它将执行非加权最短路径计算。因此之间没有差异>nx.shortest_path_length
和nx.dijkstra_shortest_path_length
?感谢您的帮助如果您将weight=attribute关键字与nx.shortest_path_length一起使用,它将运行nx.dijkstra_shortest_path_length。默认值为weight=None,它将执行非加权最短路径计算。