ó žÃÒYc@ szdZddlmZddlZdddgZdd„Zedd„Z ddd „Z ddd „Z dd „Z dS( s5Functions for computing measures of structural holes.iÿÿÿÿ(tdivisionNt constrainttlocal_constraintteffective_sizecC sxy|||j|dƒ}Wntk r7d}nXy|||j|dƒ}Wntk rod}nX||S(sYReturns 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`. ii(tgettKeyError(tGtutvtweightta_uvta_vu((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyt mutual_weights     c s[|‡‡‡fd†ttjˆˆƒƒDƒƒ}|dkrDdStˆˆ|ˆƒ|S(s‹Returns 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 s$|]}tˆˆ|ˆƒVqdS(N(R (t.0tw(RRR (sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pys 7si(tsettnxt all_neighborsR (RRRtnormR tscale((RRR sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pytnormalized_mutual_weight'sc sBdd„‰i}|dkr'ˆ}nˆjƒ rƈdkrÆxû|D]x‰tˆˆƒdkrytdƒ|ˆ>> G = nx.DiGraph() >>> G.add_edges_from([(0, 1), (0, 2), (1, 0), (2, 1)]) >>> esize = nx.effective_size(G) >>> efficency = {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 c sHt‰t‡‡‡‡‡fd†ttjˆˆƒƒDƒƒ}d|S(Nc 3 sC|]9}ˆˆˆ|dˆƒˆˆˆ|dtdˆƒVqdS(R RN(tmax(R R(RtnmwRRR (sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pys si(RtsumRRR(RRRR tr((RRRRR sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyt redundancy‹sitnantcentert undirectedic3 s$|]}ˆˆˆ|ˆƒVqdS(N((R R(RRRR (sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pys ¢sN( tNonet is_directedtlentfloatRt ego_graphtFalsetTruetsizeRRR(RtnodesR RtE((RRRR sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyR<s$O    / 'c s”|dkrˆ}ni}xr|D]j‰tˆˆƒdkrTtdƒ|ˆÝsN(RRR RRRR(RR%R R((RRR sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyR§s.   'c sdt‰ˆˆˆˆdˆƒ}t‡‡‡‡‡fd†ttjˆˆƒƒDƒƒ}||dS(sUReturns 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(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 c3 s=|]3}ˆˆˆ|dˆƒˆˆ|ˆdˆƒVqdS(R N((R R(RRRRR (sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pys si(RRRRR(RRRR tdirecttindirect((RRRRR sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyRâs 2( t__doc__t __future__RtnetworkxRt__all__RR RRRRR(((sy/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/structuralholes.pyt s  k;