modularitymatrix.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """Modularity matrix of graphs.
  2. """
  3. import networkx as nx
  4. from networkx.utils import not_implemented_for
  5. __all__ = ["modularity_matrix", "directed_modularity_matrix"]
  6. @not_implemented_for("directed")
  7. @not_implemented_for("multigraph")
  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. def directed_modularity_matrix(G, nodelist=None, weight=None):
  61. """Returns the directed modularity matrix of G.
  62. The modularity matrix is the matrix B = A - <A>, where A is the adjacency
  63. matrix and <A> is the expected adjacency matrix, assuming that the graph
  64. is described by the configuration model.
  65. More specifically, the element B_ij of B is defined as
  66. .. math::
  67. B_{ij} = A_{ij} - k_i^{out} k_j^{in} / m
  68. where :math:`k_i^{in}` is the in degree of node i, and :math:`k_j^{out}` is the out degree
  69. of node j, with m the number of edges in the graph. When weight is set
  70. to a name of an attribute edge, Aij, k_i, k_j and m are computed using
  71. its value.
  72. Parameters
  73. ----------
  74. G : DiGraph
  75. A NetworkX DiGraph
  76. nodelist : list, optional
  77. The rows and columns are ordered according to the nodes in nodelist.
  78. If nodelist is None, then the ordering is produced by G.nodes().
  79. weight : string or None, optional (default=None)
  80. The edge attribute that holds the numerical value used for
  81. the edge weight. If None then all edge weights are 1.
  82. Returns
  83. -------
  84. B : Numpy array
  85. The modularity matrix of G.
  86. Examples
  87. --------
  88. >>> G = nx.DiGraph()
  89. >>> G.add_edges_from(
  90. ... (
  91. ... (1, 2),
  92. ... (1, 3),
  93. ... (3, 1),
  94. ... (3, 2),
  95. ... (3, 5),
  96. ... (4, 5),
  97. ... (4, 6),
  98. ... (5, 4),
  99. ... (5, 6),
  100. ... (6, 4),
  101. ... )
  102. ... )
  103. >>> B = nx.directed_modularity_matrix(G)
  104. Notes
  105. -----
  106. NetworkX defines the element A_ij of the adjacency matrix as 1 if there
  107. is a link going from node i to node j. Leicht and Newman use the opposite
  108. definition. This explains the different expression for B_ij.
  109. See Also
  110. --------
  111. to_numpy_array
  112. modularity_spectrum
  113. adjacency_matrix
  114. modularity_matrix
  115. References
  116. ----------
  117. .. [1] E. A. Leicht, M. E. J. Newman,
  118. "Community structure in directed networks",
  119. Phys. Rev Lett., vol. 100, no. 11, p. 118703, 2008.
  120. """
  121. import numpy as np
  122. if nodelist is None:
  123. nodelist = list(G)
  124. A = nx.to_scipy_sparse_array(G, nodelist=nodelist, weight=weight, format="csr")
  125. k_in = A.sum(axis=0)
  126. k_out = A.sum(axis=1)
  127. m = k_in.sum()
  128. # Expected adjacency matrix
  129. X = np.outer(k_out, k_in) / m
  130. return A - X