clustering_coefficient.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from networkx.utils import not_implemented_for, py_random_state
  2. __all__ = ["average_clustering"]
  3. @py_random_state(2)
  4. @not_implemented_for("directed")
  5. def average_clustering(G, trials=1000, seed=None):
  6. r"""Estimates the average clustering coefficient of G.
  7. The local clustering of each node in `G` is the fraction of triangles
  8. that actually exist over all possible triangles in its neighborhood.
  9. The average clustering coefficient of a graph `G` is the mean of
  10. local clusterings.
  11. This function finds an approximate average clustering coefficient
  12. for G by repeating `n` times (defined in `trials`) the following
  13. experiment: choose a node at random, choose two of its neighbors
  14. at random, and check if they are connected. The approximate
  15. coefficient is the fraction of triangles found over the number
  16. of trials [1]_.
  17. Parameters
  18. ----------
  19. G : NetworkX graph
  20. trials : integer
  21. Number of trials to perform (default 1000).
  22. seed : integer, random_state, or None (default)
  23. Indicator of random number generation state.
  24. See :ref:`Randomness<randomness>`.
  25. Returns
  26. -------
  27. c : float
  28. Approximated average clustering coefficient.
  29. Examples
  30. --------
  31. >>> from networkx.algorithms import approximation
  32. >>> G = nx.erdos_renyi_graph(10, 0.2, seed=10)
  33. >>> approximation.average_clustering(G, trials=1000, seed=10)
  34. 0.214
  35. References
  36. ----------
  37. .. [1] Schank, Thomas, and Dorothea Wagner. Approximating clustering
  38. coefficient and transitivity. Universität Karlsruhe, Fakultät für
  39. Informatik, 2004.
  40. https://doi.org/10.5445/IR/1000001239
  41. """
  42. n = len(G)
  43. triangles = 0
  44. nodes = list(G)
  45. for i in [int(seed.random() * n) for i in range(trials)]:
  46. nbrs = list(G[nodes[i]])
  47. if len(nbrs) < 2:
  48. continue
  49. u, v = seed.sample(nbrs, 2)
  50. if u in G[v]:
  51. triangles += 1
  52. return triangles / trials