BFS所有特定深度的路径
我正在使用NetworkX库来生成一个无方向的图形,给定父母的关系。给定的深度x
和图形的起点,我可以在深度x
上找到所有节点,但是我也希望所需的路径到达该节点。到达路径时,我如何收集遍历的节点?
这是我的代码,它可以在depth&lt&= x
上
def descendants_at_distance(G, source, distance):
if not G.has_node(source):
raise nx.NetworkXError(f"The node {source} is not in the graph.")
current_distance = 0
current_layer = {source}
visited = {source}
layer_with_distance = {}
while current_distance < distance:
next_layer = set()
for node in current_layer:
# print(G[node])
for child in G[node]:
if child not in visited:
visited.add(child)
next_layer.add(child)
current_layer = next_layer
current_distance += 1
layer_with_distance.update({current_distance: current_layer})
layer_with_distance = {key:val for (key, val) in layer_with_distance.items() if val}
return layer_with_distance
为我提供所有节点。 x
df_links = [(1,2),(1,3),(1,4),(2,6),(3,9),(4,7),(4,8),(9,7),(9,10),(7,8),(7,10), (15,16)]
Graph = nx.Graph(df_links)
例如,如果source = 1
和decort = 3
输出应为 {1:{2,3,4},2:{8,9,6,7},3:{10}}}
其中key = gandy = distance
and 值=距离处的节点
。
我想要的是每个键值对穿过的节点。例如,在depth = 2
节点为{8,9,6,7}
(顺序没关系),每个节点都像这样。 。 1-4-8,1-3-9,1-2-6,1-4-7
让我知道是否需要进一步澄清。
I'm using networkx library to generate an undirected graph given the parent-child relationship. Given depth x
and a starting point for the graph, I can find all the nodes at depth x
, but I also want the path taken to get to that node. How do I collect nodes traversed while getting to the path?
Here is my code that gets me all the nodes at depth <= x
def descendants_at_distance(G, source, distance):
if not G.has_node(source):
raise nx.NetworkXError(f"The node {source} is not in the graph.")
current_distance = 0
current_layer = {source}
visited = {source}
layer_with_distance = {}
while current_distance < distance:
next_layer = set()
for node in current_layer:
# print(G[node])
for child in G[node]:
if child not in visited:
visited.add(child)
next_layer.add(child)
current_layer = next_layer
current_distance += 1
layer_with_distance.update({current_distance: current_layer})
layer_with_distance = {key:val for (key, val) in layer_with_distance.items() if val}
return layer_with_distance
The above code is modification to the inbuilt networkx library code since I need all the nodes between depth = 1...x
df_links = [(1,2),(1,3),(1,4),(2,6),(3,9),(4,7),(4,8),(9,7),(9,10),(7,8),(7,10), (15,16)]
Graph = nx.Graph(df_links)
For example above if the source = 1
and distance = 3
the output should be{1: {2, 3, 4}, 2: {8, 9, 6, 7}, 3: {10}}
where key = distance
and value = nodes at distance
.
What I want with this is the nodes traversed at each key-value pair. For example, at depth = 2
the nodes are {8,9,6,7}
(order doesn't matter), and the traversed nodes for each are like this. 1-4-8, 1-3-9, 1-2-6, 1-4-7
Let me know if any further clarification is needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的事情可能会起作用:我添加了另一个词典,该字典在途中收集了每个站点,而Ouput与您所说的相同
(请参阅底部输出)
输出
Something like this might work: I added another dictionary that collected each stop along the way and the ouput is identical to what you said it would be
(see the bottom for the output)
output