Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Networkx – Fraction of nodes in the largest weakly connected component

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading