我有一个很大的(半百万条边)加权图(非定向),我想找到两个节点 u 和 v 之间的距离。
我可以使用 my_graph.shortest_paths(u, v, weights='length')
来获取距离。但是,这真的很慢。
我也可以先找到路径,然后计算它的长度。这很快,但我不明白为什么这比直接计算长度要快。
在 networkx 中我使用了 nx.shortest_path_length(my_graph u, v, weight='length')
我使用此代码来计算速度。对于任何想要运行代码的人,我将边缘列表放在 Google Drive here
import pandas as pd
import networkx as nx
import igraph
import time
# load edgelist
edgelist = pd.read_pickle('edgelist.pkl')
# create igraph
tuples = [tuple(x) for x in edgelist[['u', 'v', 'length']].values]
graph_igraph = igraph.Graph.TupleList(tuples, directed=False, edge_attrs=['length'])
# create nx graph
graph_nx = nx.from_pandas_edgelist(edgelist, source='u', target='v', edge_attr=True)
def distance_shortest_path(u, v):
return graph_igraph.shortest_paths(u, v, weights='length')[0]
get_length = lambda edge: graph_igraph.es[edge]['length']
def distance_path_then_sum(u, v):
path = graph_igraph.get_shortest_paths(u, v, weights='length', output='epath')[0]
return sum(map(get_length, path))
def distance_nx(u, v):
return nx.shortest_path_length(graph_nx, u, v, weight='length')
some_nodes = [
'Delitzsch unt Bf',
'Neustadt(Holst)Gbf',
'Delitzsch ob Bf',
'Karlshagen',
'Berlin-Karlshorst (S)',
'Köln/Bonn Flughafen',
'Mannheim Hbf',
'Neu-Edingen/Friedrichsfeld',
'Ladenburg',
'Heddesheim/Hirschberg',
'Weinheim-Lützelsachsen',
'Wünsdorf-Waldstadt',
'Zossen',
'Dabendorf',
'Rangsdorf',
'Dahlewitz',
'Blankenfelde(Teltow-Fläming)',
'Berlin-Schönefeld Flughafen',
'Berlin Ostkreuz',
]
print('distance_shortest_path ', end='')
start = time.time()
for node in some_nodes:
distance_shortest_path('Köln Hbf', node)
print('took', time.time() - start)
print('distance_nx ', end='')
start = time.time()
for node in some_nodes:
distance_nx('Köln Hbf', node)
print('took', time.time() - start)
print('distance_path_then_sum ', end='')
start = time.time()
for node in some_nodes:
distance_path_then_sum('Köln Hbf', node)
print('took', time.time() - start)
结果
distance_shortest_path took 46.34037733078003
distance_nx took 12.006148099899292
distance_path_then_sum took 0.9555535316467285
答案 0 :(得分:0)
您可以在 igraph
中为此使用 shortest_paths
函数。使用非常简单,假设 G
是您的图,具有 G.es['weight']
边权重,然后
D = G.shortest_paths(weights='weight'))
会给你一个igraph
matrix D
。您可以将其转换为 numpy
数组为
D = np.array(list(D))
要仅获取特定对(组)节点之间的距离,您可以指定 source
的 target
和 shortest_paths
参数。