我有一个相当大的networkx图(785个节点,11k+个边),表示2018年的citibike交通数据(here's a link to the data)。你知道吗
节点是站点ID。每条边有两个相关的权重-遍历频率(int)和平均行程持续时间(float)。你知道吗
我需要计算图表上的最长最短路径(即直径)。如果我不在乎体重,那么做起来就很简单了:
import networkx
G = {code to make graph}
diameter = nx.diameter(G)
但是,需要根据每个特征计算直径。即,我要根据频率计算直径,根据行程时间计算直径。理想情况下,可以做到以下几点:
nx.diameter(G, weight='feature_name')
……但这似乎不是直径函数的特征。你知道吗
我已经近似了一个解决方案,其中我计算了图的未加权外围,然后暴力计算了其中每个节点之间的最短路径。这是可行的解决方案吗?我的直觉告诉我没有
这是黑客代码:
def approx_weighted_diameter(a):
g = trim_subs(a.copy())
print('Calculating periphery of graph - takes a while (~2min).')
peri = nx.periphery(g)
stations = peri
max_distances = dict()
for s1 in tqdm(stations):
distances = dict()
for s2 in stations:
if s1 != s2:
try:
distances[s2] = nx.dijkstra_path_length(G,s1,s2,weight='dur')
except:
print('Some error occurred.')
continue
max_dist_node, max_dist = sorted(distances.items(), key=lambda x: x[1])[-1]
max_distances[s1] = (max_dist_node, max_dist)
#Longest path (approximated via brute force on unweighted periphery)
start,(end,dist) = sorted(max_distances.items(), key=lambda x: x[1][1])[-1]
return (start, end, dist)
目前没有回答
相关问题 更多 >
编程相关推荐