Networkx:如何以广度优先的方式迭代DiGraph的所有边缘?

时间:2023-02-03 19:31:55

I have a networkx DiGraph (not necessarily acyclic). All nodes have a common predecessor : the source node 0.

我有一个networkx DiGraph(不一定是非循环的)。所有节点都有一个共同的前驱:源节点0。

I want to be able to edit the attributes of all edges, in a breadth first order. To do so, I would like to be able to iterate on ALL edges, starting from source, in a breadth-first manner.

我希望能够以广度优先顺序编辑所有边的属性。为此,我希望能够以广度优先的方式从源头开始迭代所有边缘。

bfs_edges allows classic breadth-first search, which leads to the following issue: if a node has n direct predecessors, only one edge will appear in the iterator, instead of n.

bfs_edges允许经典的广度优先搜索,这会导致以下问题:如果一个节点有n个直接前驱,那么迭代器中只会出现一条边,而不是n。

My edges :

我的边缘:

([(0.0, 1), (1, 2), (1, 7), (2, 3), (2, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 7), (6, 7)])

what bfs_edges returns :

什么bfs_edges返回:

for edge in nx.bfs_edges(digraph,0):
print(edge[0], edge[1])

0 1
1 2
1 7
2 3
2 4
3 5
3 6

Can anyone help? Thanks!

有人可以帮忙吗?谢谢!

1 个解决方案

#1


0  

Maybe using NetworkX bfs_predecessors is better for your case, since it returns a iterator of predecessors. You can find the documentation for this method here with this example.

也许使用NetworkX bfs_predecessors对你的情况更好,因为它返回一个前辈的迭代器。您可以在此处找到此方法的文档。

>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_predecessors(G, 0)))
{1: 0, 2: 1}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> dict(nx.bfs_predecessors(H, 0))
{1: 0, 2: 0, 3: 1, 4: 1, 5: 2, 6: 2}

EDIT

If you want to find all predecessors from a node, you need to iteratively run NetworkX predecessors and print the nodes, the following example do that.

如果要查找节点中的所有前置任务,则需要迭代运行NetworkX前置任务并打印节点,以下示例执行此操作。

import networkx as nx

G = nx.DiGraph()
G.add_nodes_from([1,2,3])
G.add_edges_from([(2, 1), (3, 1), (2, 3)])

def activate_node(g, start_node):          
    stack = [start_node]

    while stack:
        node = stack.pop()
        preds = g.predecessors(node)
        stack += preds
        print('%s -> %s' % (node, preds))

activate_node(G, 1)

This will print:

这将打印:

1 -> [2, 3]
3 -> [2]
2 -> []
2 -> []

The code above was adapted from this question.

上面的代码改编自这个问题。

#1


0  

Maybe using NetworkX bfs_predecessors is better for your case, since it returns a iterator of predecessors. You can find the documentation for this method here with this example.

也许使用NetworkX bfs_predecessors对你的情况更好,因为它返回一个前辈的迭代器。您可以在此处找到此方法的文档。

>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_predecessors(G, 0)))
{1: 0, 2: 1}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> dict(nx.bfs_predecessors(H, 0))
{1: 0, 2: 0, 3: 1, 4: 1, 5: 2, 6: 2}

EDIT

If you want to find all predecessors from a node, you need to iteratively run NetworkX predecessors and print the nodes, the following example do that.

如果要查找节点中的所有前置任务,则需要迭代运行NetworkX前置任务并打印节点,以下示例执行此操作。

import networkx as nx

G = nx.DiGraph()
G.add_nodes_from([1,2,3])
G.add_edges_from([(2, 1), (3, 1), (2, 3)])

def activate_node(g, start_node):          
    stack = [start_node]

    while stack:
        node = stack.pop()
        preds = g.predecessors(node)
        stack += preds
        print('%s -> %s' % (node, preds))

activate_node(G, 1)

This will print:

这将打印:

1 -> [2, 3]
3 -> [2]
2 -> []
2 -> []

The code above was adapted from this question.

上面的代码改编自这个问题。