modularitymatrix.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Modularity matrix of graphs."""
  2. import networkx as nx
  3. from networkx.utils import not_implemented_for
  4. __all__ = ["modularity_matrix", "directed_modularity_matrix"]
  5. @not_implemented_for("directed")
  6. @not_implemented_for("multigraph")
  7. @nx._dispatchable(edge_attrs="weight")
  8. def modularity_matrix(G, nodelist=None, weight=None):
  9. r"""Returns the modularity matrix of G.
  10. The modularity matrix is the matrix B = A - <A>, where A is the adjacency
  11. matrix and <A> is the average adjacency matrix, assuming that the graph
  12. is described by the configuration model.
  13. More specifically, the element B_ij of B is defined as
  14. .. math::
  15. A_{ij} - {k_i k_j \over 2 m}
  16. where k_i is the degree of node i, and where m is the number of edges
  17. in the graph. When weight is set to a name of an attribute edge, Aij, k_i,
  18. k_j and m are computed using its value.
  19. Parameters
  20. ----------
  21. G : Graph
  22. A NetworkX graph
  23. nodelist : list, optional
  24. The rows and columns are ordered according to the nodes in nodelist.
  25. If nodelist is None, then the ordering is produced by G.nodes().
  26. weight : string or None, optional (default=None)
  27. The edge attribute that holds the numerical value used for
  28. the edge weight. If None then all edge weights are 1.
  29. Returns
  30. -------
  31. B : Numpy array
  32. The modularity matrix of G.
  33. Examples
  34. --------
  35. >>> k = [3, 2, 2, 1, 0]
  36. >>> G = nx.havel_hakimi_graph(k)
  37. >>> B = nx.modularity_matrix(G)
  38. See Also
  39. --------
  40. to_numpy_array
  41. modularity_spectrum
  42. adjacency_matrix
  43. directed_modularity_matrix
  44. References
  45. ----------
  46. .. [1] M. E. J. Newman, "Modularity and community structure in networks",
  47. Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
  48. """
  49. import numpy as np
  50. if nodelist is None:
  51. nodelist = list(G)
  52. A = nx.to_scipy_sparse_array(G, nodelist=nodelist, weight=weight, format="csr")
  53. k = A.sum(axis=1)
  54. m = k.sum() * 0.5
  55. # Expected adjacency matrix
  56. X = np.outer(k, k) / (2 * m)
  57. return A - X
  58. @not_implemented_for("undirected")
  59. @not_implemented_for("multigraph")
  60. @nx._dispatchable(edge_attrs="weight")
  61. def directed_modularity_matrix(G, nodelist=None, weight=None):
  62. """Returns the directed modularity matrix of G.
  63. The modularity matrix is the matrix B = A - <A>, where A is the adjacency
  64. matrix and <A> is the expected adjacency matrix, assuming that the graph
  65. is described by the configuration model.
  66. More specifically, the element B_ij of B is defined as
  67. .. math::
  68. B_{ij} = A_{ij} - k_i^{out} k_j^{in} / m
  69. where :math:`k_i^{in}` is the in degree of node i, and :math:`k_j^{out}` is the out degree
  70. of node j, with m the number of edges in the graph. When weight is set
  71. to a name of an attribute edge, Aij, k_i, k_j and m are computed using
  72. its value.
  73. Parameters
  74. ----------
  75. G : DiGraph
  76. A NetworkX DiGraph
  77. nodelist : list, optional
  78. The rows and columns are ordered according to the nodes in nodelist.
  79. If nodelist is None, then the ordering is produced by G.nodes().
  80. weight : string or None, optional (default=None)
  81. The edge attribute that holds the numerical value used for
  82. the edge weight. If None then all edge weights are 1.
  83. Returns
  84. -------
  85. B : Numpy array
  86. The modularity matrix of G.
  87. Examples
  88. --------
  89. >>> G = nx.DiGraph()
  90. >>> G.add_edges_from(
  91. ... (
  92. ... (1, 2),
  93. ... (1, 3),
  94. ... (3, 1),
  95. ... (3, 2),
  96. ... (3, 5),
  97. ... (4, 5),
  98. ... (4, 6),
  99. ... (5, 4),
  100. ... (5, 6),
  101. ... (6, 4),
  102. ... )
  103. ... )
  104. >>> B = nx.directed_modularity_matrix(G)
  105. Notes
  106. -----
  107. NetworkX defines the element A_ij of the adjacency matrix as 1 if there
  108. is a link going from node i to node j. Leicht and Newman use the opposite
  109. definition. This explains the different expression for B_ij.
  110. See Also
  111. --------
  112. to_numpy_array
  113. modularity_spectrum
  114. adjacency_matrix
  115. modularity_matrix
  116. References
  117. ----------
  118. .. [1] E. A. Leicht, M. E. J. Newman,
  119. "Community structure in directed networks",
  120. Phys. Rev Lett., vol. 100, no. 11, p. 118703, 2008.
  121. """
  122. import numpy as np
  123. if nodelist is None:
  124. nodelist = list(G)
  125. A = nx.to_scipy_sparse_array(G, nodelist=nodelist, weight=weight, format="csr")
  126. k_in = A.sum(axis=0)
  127. k_out = A.sum(axis=1)
  128. m = k_in.sum()
  129. # Expected adjacency matrix
  130. X = np.outer(k_out, k_in) / m
  131. return A - X