0
networkxには、与えられた(任意選択で重み付けされた)距離内にあるすべての祖先/子孫を特定する関数/メソッドがありますか?networkx内の特定の距離内の祖先/子孫を効率的に特定する
たとえば、以下の関数と同じ結果を効率的に生成するものはありますか?
import networkx
g = networkx.DiGraph()
edges_with_atts = [(1, 2, {'length':5}),
(1, 3, {'length':11}),
(2, 4, {'length':4}),
(2, 5,{'length':7})]
g.add_edges_from(edges_with_atts)
def descendants_within(graph, start_node=1, constraint=10, weight='length'):
result = set()
for node in networkx.descendants(graph, start_node):
if networkx.shortest_path_length(graph, start_node, node, weight) < constraint:
result.add(node)
return result
print(descendants_within(g))
#set([2, 4])
ありがとうございました。この使用を計画している他の人のためのメモ:single_source_dijkstra_path_lengthは子孫のみを処理します。先祖を得るために、私はグラフを逆転させなければならなかった:networkx.reverse(g)。 – Tom