BFS所有特定深度的路径

发布于 2025-02-04 00:54:53 字数 1519 浏览 5 评论 0原文

我正在使用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 = 1decort = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-02-11 00:54:54

这样的事情可能会起作用:我添加了另一个词典,该字典在途中收集了每个站点,而Ouput与您所说的相同
(请参阅底部输出)

...
    ...
    layer_with_distance = {}
    tracker = {} # added this
    while current_distance < distance:
        tracker[source] = {} # added this
        next_layer = set()
        for node in current_layer:
            # print(G[node])
            tracker[source][node] = []  # added this
            for child in G[node]:
                if child not in visited:
                    tracker[source][node].append(child) #added this
                    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, tracker # added to this


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)
print(descendants_at_distance(Graph, 1, 2))

输出

layer_with_distance: {1: {2, 3, 4}, 2: {8, 9, 6, 7}}

tracker: {1: {2: [6], 3: [9], 4: [7, 8]}}

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)

...
    ...
    layer_with_distance = {}
    tracker = {} # added this
    while current_distance < distance:
        tracker[source] = {} # added this
        next_layer = set()
        for node in current_layer:
            # print(G[node])
            tracker[source][node] = []  # added this
            for child in G[node]:
                if child not in visited:
                    tracker[source][node].append(child) #added this
                    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, tracker # added to this


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)
print(descendants_at_distance(Graph, 1, 2))

output

layer_with_distance: {1: {2, 3, 4}, 2: {8, 9, 6, 7}}

tracker: {1: {2: [6], 3: [9], 4: [7, 8]}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文