degree_alg.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """Degree centrality measures."""
  2. import networkx as nx
  3. from networkx.utils.decorators import not_implemented_for
  4. __all__ = ["degree_centrality", "in_degree_centrality", "out_degree_centrality"]
  5. @nx._dispatch
  6. def degree_centrality(G):
  7. """Compute the degree centrality for nodes.
  8. The degree centrality for a node v is the fraction of nodes it
  9. is connected to.
  10. Parameters
  11. ----------
  12. G : graph
  13. A networkx graph
  14. Returns
  15. -------
  16. nodes : dictionary
  17. Dictionary of nodes with degree centrality as the value.
  18. Examples
  19. --------
  20. >>> G = nx.Graph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
  21. >>> nx.degree_centrality(G)
  22. {0: 1.0, 1: 1.0, 2: 0.6666666666666666, 3: 0.6666666666666666}
  23. See Also
  24. --------
  25. betweenness_centrality, load_centrality, eigenvector_centrality
  26. Notes
  27. -----
  28. The degree centrality values are normalized by dividing by the maximum
  29. possible degree in a simple graph n-1 where n is the number of nodes in G.
  30. For multigraphs or graphs with self loops the maximum degree might
  31. be higher than n-1 and values of degree centrality greater than 1
  32. are possible.
  33. """
  34. if len(G) <= 1:
  35. return {n: 1 for n in G}
  36. s = 1.0 / (len(G) - 1.0)
  37. centrality = {n: d * s for n, d in G.degree()}
  38. return centrality
  39. @nx._dispatch
  40. @not_implemented_for("undirected")
  41. def in_degree_centrality(G):
  42. """Compute the in-degree centrality for nodes.
  43. The in-degree centrality for a node v is the fraction of nodes its
  44. incoming edges are connected to.
  45. Parameters
  46. ----------
  47. G : graph
  48. A NetworkX graph
  49. Returns
  50. -------
  51. nodes : dictionary
  52. Dictionary of nodes with in-degree centrality as values.
  53. Raises
  54. ------
  55. NetworkXNotImplemented
  56. If G is undirected.
  57. Examples
  58. --------
  59. >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
  60. >>> nx.in_degree_centrality(G)
  61. {0: 0.0, 1: 0.3333333333333333, 2: 0.6666666666666666, 3: 0.6666666666666666}
  62. See Also
  63. --------
  64. degree_centrality, out_degree_centrality
  65. Notes
  66. -----
  67. The degree centrality values are normalized by dividing by the maximum
  68. possible degree in a simple graph n-1 where n is the number of nodes in G.
  69. For multigraphs or graphs with self loops the maximum degree might
  70. be higher than n-1 and values of degree centrality greater than 1
  71. are possible.
  72. """
  73. if len(G) <= 1:
  74. return {n: 1 for n in G}
  75. s = 1.0 / (len(G) - 1.0)
  76. centrality = {n: d * s for n, d in G.in_degree()}
  77. return centrality
  78. @nx._dispatch
  79. @not_implemented_for("undirected")
  80. def out_degree_centrality(G):
  81. """Compute the out-degree centrality for nodes.
  82. The out-degree centrality for a node v is the fraction of nodes its
  83. outgoing edges are connected to.
  84. Parameters
  85. ----------
  86. G : graph
  87. A NetworkX graph
  88. Returns
  89. -------
  90. nodes : dictionary
  91. Dictionary of nodes with out-degree centrality as values.
  92. Raises
  93. ------
  94. NetworkXNotImplemented
  95. If G is undirected.
  96. Examples
  97. --------
  98. >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
  99. >>> nx.out_degree_centrality(G)
  100. {0: 1.0, 1: 0.6666666666666666, 2: 0.0, 3: 0.0}
  101. See Also
  102. --------
  103. degree_centrality, in_degree_centrality
  104. Notes
  105. -----
  106. The degree centrality values are normalized by dividing by the maximum
  107. possible degree in a simple graph n-1 where n is the number of nodes in G.
  108. For multigraphs or graphs with self loops the maximum degree might
  109. be higher than n-1 and values of degree centrality greater than 1
  110. are possible.
  111. """
  112. if len(G) <= 1:
  113. return {n: 1 for n in G}
  114. s = 1.0 / (len(G) - 1.0)
  115. centrality = {n: d * s for n, d in G.out_degree()}
  116. return centrality