def directed_modularity_matrix(G, nodelist=None):
""" INCLUDED FOR TESTING PURPOSES - Not implemented yet.
Return the directed modularity matrix of G.
The modularity matrix is the matrix B = A - <A>, where A is the adjacency
matrix and <A> is the expected adjacency matrix, assuming that the graph
is described by the configuration model.
More specifically, the element B_ij of B is defined as
B_ij = A_ij - k_i(out) k_j(in)/m
where k_i(in) is the in degree of node i, and k_j(out) is the out degree
of node j, with m the number of edges in the graph.
Parameters
----------
G : DiGraph
A NetworkX DiGraph
nodelist : list, optional
The rows and columns are ordered according to the nodes in nodelist.
If nodelist is None, then the ordering is produced by G.nodes().
Returns
-------
B : Numpy matrix
The modularity matrix of G.
Notes
-----
NetworkX defines the element A_ij of the adjacency matrix as 1 if there
is a link going from node i to node j. Leicht and Newman use the opposite
definition. This explains the different expression for B_ij.
See Also
--------
to_numpy_matrix
adjacency_matrix
laplacian_matrix
modularity_matrix
References
----------
.. [1] E. A. Leicht, M. E. J. Newman,
"Community structure in directed networks",
Phys. Rev Lett., vol. 100, no. 11, p. 118703, 2008.
"""
if nodelist is None:
nodelist = G.nodes()
A = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, format='csr')
k_in = A.sum(axis=0)
k_out = A.sum(axis=1)
m = G.number_of_edges()
# Expected adjacency matrix
X = k_out * k_in / m
return A - X
评论列表
文章目录