K iR1 dZddlZgdZejdd dZejdedfdZejdd dZejdd d Z ejdd d Z y) z5Functions for computing measures of structural holes.N) constraintlocal_constrainteffective_sizeweight) edge_attrsc |||j|d} |||j|d}||zS#t$rd}Y+wxYw#t$r d}Y||zSwxYw)aYReturns the sum of the weights of the edge from `u` to `v` and the edge from `v` to `u` in `G`. `weight` is the edge data key that represents the edge weight. If the specified key is `None` or is not in the edge data for an edge, that edge is assumed to have weight 1. Pre-conditions: `u` and `v` must both be in `G`. r)getKeyError)Guvra_uva_vus i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/structuralholes.py mutual_weightrs}tAw{{61%tAw{{61% $;   $;s 8A AA AAc |fdttjD}|dk(rdSt||z S)aReturns normalized mutual weight of the edges from `u` to `v` with respect to the mutual weights of the neighbors of `u` in `G`. `norm` specifies how the normalization factor is computed. It must be a function that takes a single argument and returns a number. The argument will be an iterable of mutual weights of pairs ``(u, w)``, where ``w`` ranges over each (in- and out-)neighbor of ``u``. Commons values for `normalization` are ``sum`` and ``max``. `weight` can be ``None`` or a string, if None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. c3:K|]}t|ywN)r).0wr r rs r z+normalized_mutual_weight..0sUAq!Q/Ur)setnx all_neighborsr)r r rnormrscales`` ` rnormalized_mutual_weightrsI" UR=M=MaQR=S9TU UE 1F aAv > FFc dd ddl}ddl}d}|.|r+tj}||jz}|j }j d5||jd dd|jfz}||jd jz}dddd ||jzjz } |dkD| zjd } |jd |jz } | dk(} td | | <tt| jSi} |}j!sxv|D]ot#fd Drtd | <)tj$dd } t'| d| j)zt'| z z | <q| S|D]_t#fdDrtd | <)tfdt+tj,D| <a| S#d}Y,xYw#1swYxYw)a Returns the effective size of all nodes in the graph ``G``. The *effective size* of a node's ego network is based on the concept of redundancy. A person's ego network has redundancy to the extent that her contacts are connected to each other as well. The nonredundant part of a person's relationships is the effective size of her ego network [1]_. Formally, the effective size of a node $u$, denoted $e(u)$, is defined by .. math:: e(u) = \sum_{v \in N(u) \setminus \{u\}} \left(1 - \sum_{w \in N(v)} p_{uw} m_{vw}\right) where $N(u)$ is the set of neighbors of $u$ and $p_{uw}$ is the normalized mutual weight of the (directed or undirected) edges joining $u$ and $v$, for each vertex $u$ and $v$ [1]_. And $m_{vw}$ is the mutual weight of $v$ and $w$ divided by $v$ highest mutual weight with any of its neighbors. The *mutual weight* of $u$ and $v$ is the sum of the weights of edges joining them (edge weights are assumed to be one if the graph is unweighted). For the case of unweighted and undirected graphs, Borgatti proposed a simplified formula to compute effective size [2]_ .. math:: e(u) = n - \frac{2t}{n} where `t` is the number of ties in the ego network (not including ties to ego) and `n` is the number of nodes (excluding ego). Parameters ---------- G : NetworkX graph The graph containing ``v``. Directed graphs are treated like undirected graphs when computing neighbors of ``v``. nodes : container, optional Container of nodes in the graph ``G`` to compute the effective size. If None, the effective size of every node is computed. weight : None or string, optional If None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. Returns ------- dict Dictionary with nodes as keys and the effective size of the node as values. Notes ----- Isolated nodes, including nodes which only have self-loop edges, do not have a well-defined effective size:: >>> G = nx.path_graph(3) >>> G.add_edge(4, 4) >>> nx.effective_size(G) {0: 1.0, 1: 2.0, 2: 1.0, 4: nan} Burt also defined the related concept of *efficiency* of a node's ego network, which is its effective size divided by the degree of that node [1]_. So you can easily compute efficiency: >>> G = nx.DiGraph() >>> G.add_edges_from([(0, 1), (0, 2), (1, 0), (2, 1)]) >>> esize = nx.effective_size(G) >>> efficiency = {n: v / G.degree(n) for n, v in esize.items()} See also -------- constraint References ---------- .. [1] Burt, Ronald S. *Structural Holes: The Social Structure of Competition.* Cambridge: Harvard University Press, 1995. .. [2] Borgatti, S. "Structural Holes: Unpacking Burt's Redundancy Measures" CONNECTIONS 20(1):35-38. http://www.analytictech.com/connections/v20(1)/holes.htm Nc ttfdttjD}d|z S)Nc 3ZK|]"}||tz$yw)r)rrN)maxrrr nmwr rrs rrz5effective_size..redundancy..s6  1a '#aAC*O O s(+r rsumrrr)r r rrrr's```` @r redundancyz"effective_size..redundancys<&  ))!Q/0  1u r rTFr$ignoredivider axisnanc3(K|] }|k( ywrrr rs rrz!effective_size..(a16()center undirectedc3(K|] }|k( ywrr3r4s rrz!effective_size..r5r6c34K|]}|ywrr3)rr r r+rrs rrz!effective_size..s!$01 1aF+$sr)numpyscipyradjacency_matrixTcopyerrstater)newaxisr%toarraydiagonalfloatdictziptolist is_directedall ego_graphlensizerr)r nodesrnpsp has_scipyPmutual_weights1mutual_weights2r*rsum_mutual_weightsisolated_nodesEr+rs` ` @@rrr4sgr    }   & 1acc')..0 [[[ ) E 222:1bjj=I IO 222:BBD DO  E ?#4#44==? ?*Q.!388a8@-00a08?;S;S;UU+q0).u~&C>002344N } ==?v~ AA(1Q4(($)%Lq! Q%DAA #A!affh,#a&)@ @N1  A$  A(1Q4(($)%Lq! #$589I9I!Q9O5P$!N1    q  E Es H9"A I9II c  ddl}ddl}d}||rtj}||jz}|j d}j d 5||dd|jfz}ddd|||zzd z} |dkD| zj d} |d |jzz dk(} td | | <tt| jSi} |}|D]X t dk(rtd | <#t fd ttj D| <Z| S#d}YFxYw#1swYxYw) uReturns the constraint on all nodes in the graph ``G``. The *constraint* is a measure of the extent to which a node *v* is invested in those nodes that are themselves invested in the neighbors of *v*. Formally, the *constraint on v*, denoted `c(v)`, is defined by .. math:: c(v) = \sum_{w \in N(v) \setminus \{v\}} \ell(v, w) where $N(v)$ is the subset of the neighbors of `v` that are either predecessors or successors of `v` and $\ell(v, w)$ is the local constraint on `v` with respect to `w` [1]_. For the definition of local constraint, see :func:`local_constraint`. Parameters ---------- G : NetworkX graph The graph containing ``v``. This can be either directed or undirected. nodes : container, optional Container of nodes in the graph ``G`` to compute the constraint. If None, the constraint of every node is computed. weight : None or string, optional If None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. Returns ------- dict Dictionary with nodes as keys and the constraint on the node as values. See also -------- local_constraint References ---------- .. [1] Burt, Ronald S. "Structural holes and good ideas". American Journal of Sociology (110): 349–399. rNTFr$r r/r,r-r9r1c3:K|]}t|ywr)r)rnr rrs rrzconstraint..7s" 23 Q1f - r)r<r=rr>r?r)rArBrDrErFrGrHrLrr)r rNrrOrPrQrRmutual_weightsrUlocal_constraints constraintsrVrrs` ` @rrrsb    }   & 1QSS,//Q/7 [[[ ) @ 0BJJ? ?N @,n~.MMRSS&*.??DD!DL ,a.2I2I2K.KKqP&+El N#C;--/011J }   qt9>!%LJqM  7:2;K;KAq;Q7R  1   Q  @ @s EEEE!c t}tfdttjD}||zdzS)uYReturns the local constraint on the node ``u`` with respect to the node ``v`` in the graph ``G``. Formally, the *local constraint on u with respect to v*, denoted $\ell(u, v)$, is defined by .. math:: \ell(u, v) = \left(p_{uv} + \sum_{w \in N(v)} p_{uw} p_{wv}\right)^2, where $N(v)$ is the set of neighbors of $v$ and $p_{uv}$ is the normalized mutual weight of the (directed or undirected) edges joining $u$ and $v$, for each vertex $u$ and $v$ [1]_. The *mutual weight* of $u$ and $v$ is the sum of the weights of edges joining them (edge weights are assumed to be one if the graph is unweighted). Parameters ---------- G : NetworkX graph The graph containing ``u`` and ``v``. This can be either directed or undirected. u : node A node in the graph ``G``. v : node A node in the graph ``G``. weight : None or string, optional If None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. Returns ------- float The constraint of the node ``v`` in the graph ``G``. See also -------- constraint References ---------- .. [1] Burt, Ronald S. "Structural holes and good ideas". American Journal of Sociology (110): 349–399. r$c3PK|]}||zyw)r$Nr3r&s rrz#local_constraint..rs4  Aq!F#c!Q&&AAs#&r9r()r r rrdirectindirectr's```` @rrr=sVf #C Aq (FR%%a+,H X ! ##r r)NN) __doc__networkxr__all__ _dispatchablerr)rrrrr3r rrfs; >X&',X&+.tG'G(X&a'aHX&`'`FX&8$'8$r