ó
|£*^c           @   s‚   d  Z  d j d d d g ƒ Z d d g Z d d l Z d d e d d	 „ Z e	 e d d
 „ Z
 e Z d e	 d „ Z e	 d „ Z d S(   s   
Load centrality.

s   
s   Aric Hagberg (hagberg@lanl.gov)s   Pieter Swart (swart@lanl.gov)s#   Sasha Gutfraind (ag362@cornell.edu)t   load_centralityt	   edge_loadiÿÿÿÿNc         C   sb  | d k	 rž d } xF |  D]> } t |  | | t | ƒ } | | | k rP | | n d 7} q W| rš |  j ƒ  } | d k r} | S| d | d | d 9} n  | Si  j |  d ƒ } xK | D]C } t |  | | t | ƒ } x" | D] }	 | |	 c | |	 7<qÜ Wq· W| rZ|  j ƒ  } | d k r | Sd | d | d }
 x! | D] } | | c |
 9<q=Wn  | Sd S(   s�  Compute load centrality for nodes.

    The load centrality of a node is the fraction of all shortest
    paths that pass through that node.

    Parameters
    ----------
    G : graph
      A networkx graph

    normalized : bool, optional
      If True the betweenness values are normalized by b=b/(n-1)(n-2) where
      n is the number of nodes in G.

    weight : None or string, optional
      If None, edge weights are ignored.
      Otherwise holds the name of the edge attribute used as weight.

    cutoff : bool, optional
      If specified, only consider paths of length <= cutoff.

    Returns
    -------
    nodes : dictionary
       Dictionary of nodes with centrality as the value.

    See Also
    --------
    betweenness_centrality()

    Notes
    -----
    Load centrality is slightly different than betweenness. It was originally
    introduced by [2]_. For this load algorithm see [1]_.

    References
    ----------
    .. [1] Mark E. J. Newman:
       Scientific collaboration networks. II.
       Shortest paths, weighted networks, and centrality.
       Physical Review E 64, 016132, 2001.
       http://journals.aps.org/pre/abstract/10.1103/PhysRevE.64.016132
    .. [2] Kwang-Il Goh, Byungnam Kahng and Doochul Kim
       Universal behavior of Load Distribution in Scale-Free Networks.
       Physical Review Letters 87(27):1â€“4, 2001.
       http://phya.snu.ac.kr/~dkim/PRL87278701.pdf
    g        i    i   g      ð?i   N(   t   Nonet   _node_betweennesst   Falset   ordert   fromkeys(   t   Gt   vt   cutofft
   normalizedt   weightt   betweennesst   sourcet   ubetweenR   t   vkt   scale(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pyt   newman_betweenness_centrality   s0    2$c         C   sÅ  | d k r3 t j |  | d | d t ƒ\ } } n t j |  | d | ƒ\ } } g  | j ƒ  D] \ } } | | f ^ q^ }	 |	 j ƒ  g  |	 D] \ } } | d k r� | ^ q� |	 (i  j | d ƒ }
 xx |	 r>|	 j ƒ  } | | k rÇ t	 | | ƒ } xC | | D]4 } | | k rPn  |
 | c |
 | t
 | ƒ 7<q WqÇ qÇ Wx |
 D] } |
 | c d 8<qFW| rÁt	 |
 ƒ } | d k rÁd t
 | d | d ƒ } x! |
 D] } |
 | c | 9<q¡WqÁn  |
 S(	   s6  Node betweenness helper:
    see betweenness_centrality for what you probably want.

    This actually computes "load" and not betweenness.
    See https://networkx.lanl.gov/ticket/103

    This calculates the load of each node for paths from a single source.
    (The fraction of number of shortests paths from source that go
    through each node.)

    To get the load for a node you need to do all-pairs shortest paths.

    If weight is not None then use Dijkstra for finding shortest paths.
    In this case a cutoff is not implemented and so is ignored.

    R	   t   return_seenR   i    g      ð?i   i   N(   R   t   nxt   predecessort   Truet!   dijkstra_predecessor_and_distancet   itemst   sortR   t   popt   lent   float(   R   R   R	   R
   R   t   predt   lengtht   vertt   lt   onodest   betweenR   t	   num_pathst   xR   (    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pyR   a   s0    '+
,	)c         C   sƒ   i  } | s |  j  ƒ  } n  xa | D]Y } t |  | | d | ƒ} x8 | j ƒ  D]* } | j | d ƒ } | | | | | <qM Wq" W| S(   sb   Compute edge load.

    WARNING:

    This module is for demonstration and testing purposes.

    R	   i    (   t   nodest   _edge_betweennesst   keyst
   setdefault(   R   R$   R	   R   R   R   R   t   b(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pyR   ™   s    c         C   sl  i  } t  j |  | d | d t ƒ\ } } g  t d „  | j ƒ  Dƒ ƒ D] \ } } | ^ qG }	 x= |  j | ƒ D], \ }
 } d | |
 | f <d | | |
 f <qo WxÆ |	 rg|	 j ƒ  } | | k r¢ t | | ƒ } x‘ | | D]‚ } | | k rÛ t | | ƒ } x] | | D]N } | | | f c | | | f | 7<| | | f c | | | f | 7<qWqÛ qÛ Wq¢ q¢ W| S(   s"   
    Edge betweenness helper.
    R	   R   c         s   s!   |  ] \ } } | | f Vq d  S(   N(    (   t   .0t   nt   dist(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pys	   <genexpr>´   s    g      ð?(   R   R   R   t   sortedR   t   edgesR   R   (   R   R   R$   R	   R!   R   R   t   ddt   nnR    t   uR   R"   t   wR#   (    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pyR%   «   s"    $5	$6(   t   __doc__t   joint
   __author__t   __all__t   networkxR   R   R   R   R   R   R    R   R%   (    (    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/load.pyt   <module>   s   		J5