### comparison env/lib/python3.9/site-packages/networkx/algorithms/approximation/clustering_coefficient.py @ 0:4f3585e2f14bdraftdefaulttip

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