Networkx - 最大弱连接组件中的节点分数

Networkx - Fraction of nodes in the largest weakly connected component

我有一个相邻矩阵,我需要计算最大分量(或在有向网络的情况下最大的弱连接分量)中的节点分数:

    # 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 

图表

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

如何根据上面的代码计算这个分数?

网络中的节点总数是G.number_of_nodes(),所以如果我理解正确的话,答案是:

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