在 NetworkX 中找到加权图的最短路径长度
Find the shortest path length of a weighted graph in NetworkX
我正在尝试使用 networkx 来确定源节点和目标节点之间的最短加权路径。为此,我使用 nx.shortest_path
。但是,我无法让它正常运行。
以下类似于我的设置:
import pandas as pd
import networkx as nx
df = pd.DataFrame({'F': ['a','b','c','d','d','e'], # f node identifier
'T': ['b','c','d','e','f','f'], # t node identifier
'weight': [1.2,5.2,2.7,2.8,1.3,7.4], # weight for shortest path algorithm
'dummy': ['q','w','e','r','t','y']}) # dummy variable
网络构建发生在一个函数内,因为如果我让它工作,它将应用于几个不同的数据集!这也是为什么将属性添加为字典而不是单独添加的原因。
def build_network(df=None, column_names=None):
g = nx.DiGraph()
for i,row in df.iterrows():
g.add_edge(row[column_names['F']],row[column_names['T']],attributes=row[column_names['attributes']].to_dict())
return g
g = build_network(df, column_names={'F':'F',
'T':'T',
'attributes':['weight','dummy']})
最后应用shortest_path_length
算法,表示长度为2(边数),而不是4.0(加权距离)。我怀疑这是因为我错误地引用了权重属性。但是,我不确定我应该怎么做。
nx.shortest_path_length(G=g, source='c', target='f', weight="['attributes']['weight']")
如有任何帮助,我们将不胜感激!
您使图表的创建过于复杂。您可以使用 nx.from_pandas_edgelist
以更简单的方式(包括边缘属性)从数据帧创建图形并找到最短路径长度:
G = nx.from_pandas_edgelist(df, source='F', target='T', edge_attr=['weight','dummy'],
create_using=nx.DiGraph)
G.edges(data=True)
# EdgeDataView([('a', 'b', {'weight': 1.2, 'dummy': 'q'}),
# ('b', 'c', {'weight': 5.2, 'dummy': 'w'})...
nx.shortest_path_length(G, source='c', target='f', weight='weight')
# 4.0
仔细研究您的方法,问题在于您如何在 nx.shortest_path_length
中指定权重。当 weight
参数应设置为指定权重属性名称的字符串时,您正在使用 "['attributes']['weight']"
。所以在你的情况下,"weight"
.
因此您得到的结果与:
nx.shortest_path_length(G=g, source='c', target='f', weight=None)
# 2
而你应该像上面那样做:
nx.shortest_path_length(G, source='c', target='f', weight='weight')
# 4.0
我正在尝试使用 networkx 来确定源节点和目标节点之间的最短加权路径。为此,我使用 nx.shortest_path
。但是,我无法让它正常运行。
以下类似于我的设置:
import pandas as pd
import networkx as nx
df = pd.DataFrame({'F': ['a','b','c','d','d','e'], # f node identifier
'T': ['b','c','d','e','f','f'], # t node identifier
'weight': [1.2,5.2,2.7,2.8,1.3,7.4], # weight for shortest path algorithm
'dummy': ['q','w','e','r','t','y']}) # dummy variable
网络构建发生在一个函数内,因为如果我让它工作,它将应用于几个不同的数据集!这也是为什么将属性添加为字典而不是单独添加的原因。
def build_network(df=None, column_names=None):
g = nx.DiGraph()
for i,row in df.iterrows():
g.add_edge(row[column_names['F']],row[column_names['T']],attributes=row[column_names['attributes']].to_dict())
return g
g = build_network(df, column_names={'F':'F',
'T':'T',
'attributes':['weight','dummy']})
最后应用shortest_path_length
算法,表示长度为2(边数),而不是4.0(加权距离)。我怀疑这是因为我错误地引用了权重属性。但是,我不确定我应该怎么做。
nx.shortest_path_length(G=g, source='c', target='f', weight="['attributes']['weight']")
如有任何帮助,我们将不胜感激!
您使图表的创建过于复杂。您可以使用 nx.from_pandas_edgelist
以更简单的方式(包括边缘属性)从数据帧创建图形并找到最短路径长度:
G = nx.from_pandas_edgelist(df, source='F', target='T', edge_attr=['weight','dummy'],
create_using=nx.DiGraph)
G.edges(data=True)
# EdgeDataView([('a', 'b', {'weight': 1.2, 'dummy': 'q'}),
# ('b', 'c', {'weight': 5.2, 'dummy': 'w'})...
nx.shortest_path_length(G, source='c', target='f', weight='weight')
# 4.0
仔细研究您的方法,问题在于您如何在 nx.shortest_path_length
中指定权重。当 weight
参数应设置为指定权重属性名称的字符串时,您正在使用 "['attributes']['weight']"
。所以在你的情况下,"weight"
.
因此您得到的结果与:
nx.shortest_path_length(G=g, source='c', target='f', weight=None)
# 2
而你应该像上面那样做:
nx.shortest_path_length(G, source='c', target='f', weight='weight')
# 4.0