Home > front end >  Networkx - Fraction of nodes in the largest weakly connected component
Networkx - Fraction of nodes in the largest weakly connected component

Time:12-22

I have an adjacent matrix and I need to calculate the fraction of nodes in the largest component (or the largest weakly connected component in the case of a directed network):

    # from dataframe
    matrix_weak = matrix.copy()
    # to numpy arrays
    matrix_weak_to_numpy = matrix_weak.to_numpy()
    G = nx.from_numpy_matrix(matrix_weak_to_numpy)
    G = G.to_directed() # weakly connected component needs a directed 

graph

    max_wcc = max(nx.weakly_connected_components(G), key=len)
    max_wcc = nx.subgraph(G, max_wcc)

How do I calculate this fraction from the code above?

CodePudding user response:

The total number of nodes in the network is G.number_of_nodes(), so if I understand correctly, the answer is:

fraction = max_wcc.number_of_nodes() / G.number_of_nodes()
  • Related