python networkx 求图中的环_Python NetworkX从节点作为根在有向图中找到子图

I am writing a code to extract information from a directed graph. This graph has cycles as well. For example,

A->B->C->D

A->E->F->A

B->F->G

From this graph, I want to create a sub graph or the list of the nodes, where the input would be any node, and output would be the graph where the input node is the root, or the list of the nodes that has all the child nodes ( till the end of the graph ) from the input nodes

For example, in the above example,

1. If the input node is C, the output would be D

2. If the input node is B, the output node would be C,D,F,G,A ( Since there is a cycle, which makes A to B bidirectional )

3. If the input is G, the output is blank or null.

Is there any functionality in python networkx, that I can use to solve this problem ?

Alternatively, is there any other tool that can help me solve this problem ?

解决方案

What you want is the function dfs_preorder_nodes(). Here is a little demo based on your data:

import networkx as nx

g = nx.DiGraph()

g.add_edge('A', 'B')

g.add_edge('B', 'C')

g.add_edge('C', 'D')

g.add_edge('A', 'E')

g.add_edge('E', 'F')

g.add_edge('F', 'A')

g.add_edge('B', 'F')

g.add_edge('F', 'G')

print('A:', list(nx.dfs_preorder_nodes(g, 'A')))

print('B:', list(nx.dfs_preorder_nodes(g, 'B')))

print('G:', list(nx.dfs_preorder_nodes(g, 'G')))

Output:

A: ['A', 'B', 'C', 'D', 'F', 'G', 'E']

B: ['B', 'C', 'D', 'F', 'A', 'E', 'G']

G: ['G']

The output includes the starting node. Therefore, if you don't want it, just remove the first element from the list.

Note that dfs_preorder_nodes() returns a generator object. That is why I called list() to get usable output.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值