easygraph.functions.basic.cluster module#
- easygraph.functions.basic.cluster.average_clustering(G, nodes=None, weight=None, count_zeros=True, n_workers=None)[source]#
Compute the average clustering coefficient for the graph G.
The clustering coefficient for the graph is the average,
\[C = \frac{1}{n}\sum_{v \in G} c_v,\]where \(n\) is the number of nodes in G.
- Parameters:
G (graph) –
nodes (container of nodes, optional (default=all nodes in G)) – Compute average clustering for nodes in this container.
weight (string or None, optional (default=None)) – The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.
count_zeros (bool) – If False include only the nodes with nonzero clustering in the average.
- Returns:
avg – Average clustering
- Return type:
float
Examples
>>> G = eg.complete_graph(5) >>> print(eg.average_clustering(G)) 1.0
Notes
This is a space saving routine; it might be faster to use the clustering function to get a list and then take the average.
Self loops are ignored.
References
[1]Generalizations of the clustering coefficient to weighted complex networks by J. Saramäki, M. Kivelä, J.-P. Onnela, K. Kaski, and J. Kertész, Physical Review E, 75 027105 (2007). http://jponnela.com/web_documents/a9.pdf
[2]Marcus Kaiser, Mean clustering coefficients: the role of isolated nodes and leafs on clustering measures for small-world networks. https://arxiv.org/abs/0802.2512
- easygraph.functions.basic.cluster.clustering(G, nodes=None, weight=None, n_workers=None)[source]#
Compute the clustering coefficient for nodes.
For unweighted graphs, the clustering of a node \(u\) is the fraction of possible triangles through that node that exist,
\[c_u = \frac{2 T(u)}{deg(u)(deg(u)-1)},\]where \(T(u)\) is the number of triangles through node \(u\) and \(deg(u)\) is the degree of \(u\).
For weighted graphs, there are several ways to define clustering [Ra06f0e43ac04-1]. the one used here is defined as the geometric average of the subgraph edge weights [Ra06f0e43ac04-2],
\[c_u = \frac{1}{deg(u)(deg(u)-1))} \sum_{vw} (\hat{w}_{uv} \hat{w}_{uw} \hat{w}_{vw})^{1/3}.\]The edge weights \(\hat{w}_{uv}\) are normalized by the maximum weight in the network \(\hat{w}_{uv} = w_{uv}/\max(w)\).
The value of \(c_u\) is assigned to 0 if \(deg(u) < 2\).
Additionally, this weighted definition has been generalized to support negative edge weights [Ra06f0e43ac04-3].
For directed graphs, the clustering is similarly defined as the fraction of all possible directed triangles or geometric average of the subgraph edge weights for unweighted and weighted directed graph respectively [Ra06f0e43ac04-4].
\[c_u = \frac{2}{deg^{tot}(u)(deg^{tot}(u)-1) - 2deg^{\leftrightarrow}(u)} T(u),\]where \(T(u)\) is the number of directed triangles through node \(u\), \(deg^{tot}(u)\) is the sum of in degree and out degree of \(u\) and \(deg^{\leftrightarrow}(u)\) is the reciprocal degree of \(u\).
- Parameters:
G (graph) –
nodes (container of nodes, optional (default=all nodes in G)) – Compute clustering for nodes in this container.
weight (string or None, optional (default=None)) – The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.
- Returns:
out – Clustering coefficient at specified nodes
- Return type:
float, or dictionary
Examples
>>> G = eg.complete_graph(5) >>> print(eg.clustering(G, 0)) 1.0 >>> print(eg.clustering(G)) {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}
Notes
Self loops are ignored.