networkX node_connected_component未针对定向类型实现

时间:2017-11-14 10:42:11

标签: python networkx directed-graph

我使用networkX构建有向图,我需要找到包含特殊节点的子图。我尝试了node_connected_component,但它不能实现有向图,是否还有其他功能可以实现networkX中的有向图?

1 个答案:

答案 0 :(得分:0)

正如有人提到的,它取决于你在有向图中所谓的连通组件:

  1. 强大连接的组件:

    节点A到节点B之间有一条有向路径,另一条从节点B到节点A.

  2. 弱连接组件:

    从节点A到节点B有一条有向路径,但不一定是从节点B到节点A.

  3. 你能做什么:

    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