Beispiel #1
0
def digraph_connected_components(graph: nx.DiGraph, subgraph=None) -> List[List]:
    """
    the networkx module does not support deriving connected
    components from digraphs (only simple graphs)
    this function assumes that connection != reachable
    this means there is no difference between connected components
    in a simple graph and a digraph

    Args:
        graph: the input graph to gather components from

    Returns:
        returns a list of compnents which are lists of node names
    """
    if subgraph is None:
        subgraph = set(graph.get_nodes())
    g = nx.Graph()
    for src, tgt in graph.edges():
        if src in subgraph and tgt in subgraph:
            g.add_edge(src, tgt)
    for node in subgraph:
        if graph.has_node(node):
            g.add_node(node)
    return nx.connected_components(g)