local_clustering

local_clustering#

graph_tool.clustering.local_clustering(g, weight=None, prop=None, undirected=True, weight_type='zhang', vertices=None)[git][source]#

Return the local clustering coefficients of the vertices.

Parameters:
gGraph

Graph to be used.

weightEdgePropertyMap (optional, default: None)

Edge weights. If omitted, a constant value of 1 will be used.

propVertexPropertyMap or string (optional, default: None)

Vertex property map where results will be stored. If specified, this parameter will also be the return value.

undirectedbool (optional, default: True)

Calculate the undirected clustering coefficient, if graph is directed (this option has no effect if the graph is undirected).

weight_typestr (optional, default: "zhang")

Definition of the weighted coefficient, if weight is given: one of "zhang", "barrat", or "onnela" (see below). The last two require undirected == True.

verticesiterable of Vertex or array of int (optional, default: None)

Vertices for which the values are computed. If None, all vertices are used. Otherwise, the values of the other vertices in the returned property map are not modified (they remain zero in a new property map), and the neighborhoods are still those in the whole graph.

Returns:
propVertexPropertyMap

Vertex property containing the clustering coefficients.

See also

global_clustering

Global clustering coefficient

extended_clustering

Extended (generalized) clustering coefficient

motifs

Motif counting

square_clustering

Square clustering coefficient

edge_clustering

Edge clustering coefficient

generalized_degree

Generalized degree

Notes

The local clustering coefficient [watts-collective-1998] \(c_i\) is defined as

\[c_i = \frac{|\{(j,k)\}|}{d_i(d_i-1)} :\, j,k \in N_i,\, (j,k) \in E\]

where \(d_i\) is the out-degree of vertex \(i\), and

\[N_i = \{j : (i,j) \in E\}\]

is the set of out-neighbors of vertex \(i\). For undirected graphs the value of \(c_i\) is further normalized as

\[c_i \leftarrow 2c_i.\]

If weights are given, the following definition is used [zhang-general-2005]:

\[c_i = \frac{\sum_{jk}A_{ij}A_{jk}A_{ik}}{\sum_{j\ne k}A_{ij}A_{ik}},\]

where \(\boldsymbol A\) is the weighted adjacency matrix, and it is assumed that the weights are normalized in the unit interval, i.e. \(A_{ij} \in [0,1]\).

With weight_type == "barrat", the definition of [barrat-architecture-2004] is used instead,

\[c_i = \frac{1}{s_i(k_i-1)}\sum_{j\ne h}\frac{w_{ij}+w_{ih}}{2} a_{ij}a_{ih}a_{jh},\]

where \(w_{ij}\) is the total weight of the edges between \(i\) and \(j\), \(s_i = \sum_jw_{ij}\) the weighted degree, \(k_i\) the number of neighbors, and \(a_{ij}\in\{0,1\}\) the adjacency, and with weight_type == "onnela", the definition of [onnela-intensity-2005],

\[c_i = \frac{1}{k_i(k_i-1)}\sum_{j\ne h}(\hat w_{ij}\hat w_{ih} \hat w_{jh})^{1/3},\]

with \(\hat w_{ij} = w_{ij}/\max_{kl}w_{kl}\), the maximum being taken over all pairs of adjacent vertices of the graph. In both cases, the weights of parallel edges are summed, self-loops are ignored, and the value is zero for vertices with fewer than two neighbors.

The implemented algorithm runs in \(O(|V|\left<k^2\right>)\) time, where \(\left<k^2\right>\) is the second moment of the degree distribution. If vertices is given, it runs in time \(O(\sum_{v\in S}\sum_{u\in N_v}k_u)\), where \(S\) is the set of given vertices, so that the values of a few vertices of a large graph are obtained without computing those of the others. (With weight_type == "onnela", the maximum weight still requires a pass over all the edges, in time \(O(|E|)\).)

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[watts-collective-1998]

D. J. Watts and Steven Strogatz, “Collective dynamics of ‘small-world’ networks”, Nature, vol. 393, pp 440-442, 1998. DOI: 10.1038/30918 [sci-hub, @tor]

[zhang-general-2005]

Zhang B, Horvath S, “A general framework for weighted gene co-expression network analysis”, Stat Appl Genet Mol Biol. 4 17 (2005) DOI: 10.2202/1544-6115.1128 [sci-hub, @tor]

[barrat-architecture-2004]

A. Barrat, M. Barthélemy, R. Pastor-Satorras and A. Vespignani, “The architecture of complex weighted networks”, Proc. Natl. Acad. Sci. USA 101, 3747-3752 (2004), DOI: 10.1073/pnas.0400087101 [sci-hub, @tor], arXiv: cond-mat/0311416

[onnela-intensity-2005]

Jukka-Pekka Onnela, Jari Saramäki, János Kertész and Kimmo Kaski, “Intensity and coherence of motifs in weighted complex networks”, Phys. Rev. E 71, 065103 (2005), DOI: 10.1103/PhysRevE.71.065103 [sci-hub, @tor], arXiv: cond-mat/0408629

Examples

>>> g = gt.collection.data["karate"]
>>> clust = gt.local_clustering(g)
>>> print(gt.vertex_average(g, clust))
(0.5706384782..., 0.05869813676...)

The coefficients of only some of the vertices are obtained by passing them via vertices, and they are equal to those computed for the whole graph, as we can see by comparing them with the previous values:

>>> clust_s = gt.local_clustering(g, vertices=[0, 1, 33])
>>> print(clust_s.a[[0, 1, 33]], clust.a[[0, 1, 33]])
[0.15       0.33333333 0.11029412] [0.15       0.33333333 0.11029412]