ó žÃÒYc@ s>dZddlmZddlmZddlmZddlZddlm Z ddl m Z dj d d d d gƒZ d dddddgZe dƒdd„ƒZe dƒdd„ƒZe dƒddd„ƒZdded„Ze dƒddd„ƒZd„Zdd„Ze dƒdd„ƒZdS(s>Algorithms to characterize the number of triangles in a graph.iÿÿÿÿ(tdivision(t combinations(tCounterN(t NetworkXError(tnot_implemented_fors s%Aric Hagberg s Dan Schult (dschult@colgate.edu)sPieter Swart (swart@lanl.gov)s%Jordi Torrents t trianglestaverage_clusteringt clusteringt transitivitytsquare_clusteringtgeneralized_degreetdirectedcC s>||kr'tt||ƒƒddSd„t||ƒDƒS(sCompute the number of triangles. Finds the number of triangles that include a node as one vertex. Parameters ---------- G : graph A networkx graph nodes : container of nodes, optional (default= all nodes in G) Compute triangles for nodes in this container. Returns ------- out : dictionary Number of triangles keyed by node label. Examples -------- >>> G=nx.complete_graph(5) >>> print(nx.triangles(G,0)) 6 >>> print(nx.triangles(G)) {0: 6, 1: 6, 2: 6, 3: 6, 4: 6} >>> print(list(nx.triangles(G,(0,1)).values())) [6, 6] Notes ----- When computing triangles for the entire graph each triangle is counted three times, once at each node. Self loops are ignored. icS s)i|]\}}}}|d|“qS(i((t.0tvtdttt_((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys Ds (tnextt_triangles_and_degree_iter(tGtnodes((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyRs$ t multigraphc # së|dkrˆjjƒ}n‡fd†ˆj|ƒDƒ}x§|D]Ÿ\}}t|ƒ|h}tg|D]'}t|tˆ|ƒ|h@ƒ^qmƒ}tg|jƒD]\}} || ^q­ƒ} |t|ƒ| |fVqDWdS(så Return an iterator of (node, degree, triangles, generalized degree). This double counts triangles so you may want to divide by 2. See degree(), triangles() and generalized_degree() for definitions and details. c3 s|]}|ˆ|fVqdS(N((R tn(R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys SsN(tNonetadjtitemst nbunch_itertsetRtlentsum( RRt nodes_nbrsR tv_nbrstvstwt gen_degreetktvalt ntriangles((Rsq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyRGs :/tweightc # sdˆd ksˆjƒdkr'd‰n(t‡fd†ˆjdtƒDƒƒ‰|d krmˆjjƒ}n‡fd†ˆj|ƒDƒ}‡‡‡fd†‰x¼|D]´\‰}t|ƒˆh}d}tƒ}xk|D]c‰|j ˆƒtˆˆƒ|}ˆˆˆƒ‰|t ‡‡‡‡fd†||@Dƒƒ7}qÝWˆt |ƒd|fVq¨Wd S( sc Return an iterator of (node, degree, weighted_triangles). Used for weighted clustering. iic3 s*|] \}}}|jˆdƒVqdS(iN(tget(R tuR R(R&(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys fstdatac3 s|]}|ˆ|fVqdS(N((R R(R(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys jsc sˆ||jˆdƒˆS(Ni(R'(R(R (Rt max_weightR&(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pytwtlsc3 s3|])}ˆˆˆ|ƒˆ|ˆƒdVqdS(iiNgUUUUUUÕ?((R R#(titjtwijR+(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys zsiN( Rtnumber_of_edgestmaxtedgestTrueRRRRtaddRR( RRR&Rtnbrstinbrstweighted_trianglestseentjnbrs((RR,R-R*R&R.R+sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyt#_weighted_triangles_and_degree_iter\s$ (    cC s]t||d|ƒjƒ}|sIg|D]}|dkr(|^q(}nt|ƒt|ƒS(sçCompute the average clustering coefficient for the graph G. The clustering coefficient for the graph is the average, .. math:: 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 : float Average clustering Examples -------- >>> G=nx.complete_graph(5) >>> print(nx.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 R&i(RtvaluesRR(RRR&t count_zerostcR ((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyRs5(cC sh|dk r1t|||ƒ}d„|Dƒ}nt||ƒ}d„|Dƒ}||krd||S|S(s¾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, .. math:: 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, the clustering is defined as the geometric average of the subgraph edge weights [1]_, .. math:: c_u = \frac{1}{deg(u)(deg(u)-1))} \sum_{uv} (\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`. 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 : float, or dictionary Clustering coefficient at specified nodes Examples -------- >>> G=nx.complete_graph(5) >>> print(nx.clustering(G,0)) 1.0 >>> print(nx.clustering(G)) {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0} Notes ----- 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 cS s@i|]6\}}}|dkr'dn|||d|“qS(ii((R R RR((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys ús cS sCi|]9\}}}}|dkr*dn|||d|“qS(ii((R R RRR((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys þs N(RR9R(RRR&ttd_itertclusterc((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyRºs>    cC sPtd„t|ƒDƒƒ}td„t|ƒDƒƒ}|dkrHdS||S(säCompute graph transitivity, the fraction of all possible triangles present in G. Possible triangles are identified by the number of "triads" (two edges with a shared vertex). The transitivity is .. math:: T = 3\frac{\#triangles}{\#triads}. Parameters ---------- G : graph Returns ------- out : float Transitivity Examples -------- >>> G = nx.complete_graph(5) >>> print(nx.transitivity(G)) 1.0 cs s!|]\}}}}|VqdS(N((R R RRR((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys "scs s)|]\}}}}||dVqdS(iN((R R RRR((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys #si(RR(RRtcontri((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyRsc C s<|dkr|}n|j|ƒ}i}x÷|D]ï}d||>> G=nx.complete_graph(5) >>> print(nx.square_clustering(G,0)) 1.0 >>> print(nx.square_clustering(G)) {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0} Notes ----- While `C_3(v)` (triangle clustering) gives the probability that two neighbors of node v are connected with each other, `C_4(v)` is the probability that two neighbors of node v share a common neighbor different from v. This algorithm can be applied to both bipartite and unipartite networks. References ---------- .. [1] Pedro G. Lind, Marta C. González, and Hans J. Herrmann. 2005 Cycles and clustering in bipartite networks. Physical Review E (72) 056127. iiiN(RRRRR( RRt node_iterRR t potentialR(R!tsquarestdegm((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyR 's&2     1  2  cC s:||kr#tt||ƒƒdSd„t||ƒDƒS(sÍ Compute the generalized degree for nodes. For each node, the generalized degree shows how many edges of given triangle multiplicity the node is connected to. The triangle multiplicity of an edge is the number of triangles an edge participates in. The generalized degree of node `i` can be written as a vector `\mathbf{k}_i=(k_i^{(0)}, \dotsc, k_i^{(N-2)})` where `k_i^{(j)}` is the number of edges attached to node `i` that participate in `j` triangles. Parameters ---------- G : graph nodes : container of nodes, optional (default=all nodes in G) Compute the generalized degree for nodes in this container. Returns ------- out : Counter, or dictionary of Counters Generalized degree of specified nodes. The Counter is keyed by edge triangle multiplicity. Examples -------- >>> G=nx.complete_graph(5) >>> print(nx.generalized_degree(G,0)) Counter({3: 4}) >>> print(nx.generalized_degree(G)) {0: Counter({3: 4}), 1: Counter({3: 4}), 2: Counter({3: 4}), 3: Counter({3: 4}), 4: Counter({3: 4})} To recover the number of triangles attached to a node: >>> k1 = nx.generalized_degree(G,0) >>> sum([k*v for k,v in k1.items()])/2 == nx.triangles(G,0) True Notes ----- In a network of N nodes, the highest triangle multiplicty an edge can have is N-2. The return value does not include a `zero` entry if no edges of a particular triangle multiplicity are present. The number of triangles node `i` is attached to can be recovered from the generalized degree `\mathbf{k}_i=(k_i^{(0)}, \dotsc, k_i^{(N-2)})` by `(k_i^{(1)}+2k_i^{(2)}+\dotsc +(N-2)k_i^{(N-2)})/2`. References ---------- .. [1] Networks with arbitrary edge multiplicities by V. Zlatić, D. Garlaschelli and G. Caldarelli, EPL (Europhysics Letters), Volume 97, Number 2 (2012). https://iopscience.iop.org/article/10.1209/0295-5075/97/28005 icS s%i|]\}}}}||“qS(((R R RRtgd((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pys ¬s (RR(RR((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyR ps: (t__doc__t __future__Rt itertoolsRt collectionsRtnetworkxtnxRtnetworkx.utilsRtjoint __author__t__all__RRRR9R2RRRR R (((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/cluster.pyt s2      *  "; K ! I