我使用networkX构建有向图,我需要找到包含特殊节点的子图。我尝试了node_connected_component,但它不能实现有向图,是否还有其他功能可以实现networkX中的有向图?
答案 0 :(得分:0)
正如有人提到的,它取决于你在有向图中所谓的连通组件:
强大连接的组件:
节点A到节点B之间有一条有向路径,另一条从节点B到节点A.
弱连接组件:
从节点A到节点B有一条有向路径,但不一定是从节点B到节点A.
你能做什么:
def get_strongly_cc(G, node):
""" get storngly connected component of node"""
for cc in nx.strongly_connected_components(G):
if node in cc:
return cc
else:
return set()
def get_weakly_cc(G, node):
""" get weakly connected component of node"""
for cc in nx.weakly_connected_components(G):
if node in cc:
return cc
else:
return set()
weak_component = get_weakly_cc(G, node) # Weakly connected component of node in G
strong_component = get_strongly_cc(G, node) # Strongly connected component of node in G