ó
 ,µ[c           @   sŠ   d  Z  d d l m Z d d l m Z d d l Z d d g Z d d e	 d d „ Z
 e e	 d d „ Z e
 Z e d	 „ Z d e d
 „ Z d S(   s   Load centrality.iÿÿÿÿ(   t   division(   t
   itemgetterNt   load_centralityt   edge_load_centralityc         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 (default=True)
      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 (default=None)
      If None, edge weights are ignored.
      Otherwise holds the name of the edge attribute used as weight.

    cutoff : bool, optional (default=None)
      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(    (    sB   lib/python2.7/site-packages/networkx/algorithms/centrality/load.pyt   newman_betweenness_centrality   s0    1$c         C   sÅ  | d k r3 t j |  | d | d t ƒ\ } } n t j |  | | | ƒ \ } } 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(   s   Node betweenness_centrality 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.
    R   t   return_seeni    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   (    (    sB   lib/python2.7/site-packages/networkx/algorithms/centrality/load.pyR   a   s4    +
,	)c   	      C   sš   i  } x: |  j  ƒ  D], \ } } d | | | f <d | | | f <q WxP |  D]H } t |  | d | ƒ} x* | j ƒ  D] \ } } | | c | 7<qr WqJ W| S(   s¿  Compute edge load.

    WARNING: This concept of edge load has not been analysed
    or discussed outside of NetworkX that we know of.
    It is based loosely on load_centrality in the sense that
    it counts the number of shortest paths which cross each edge.
    This function is for demonstration and testing purposes.

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

    cutoff : bool, optional (default=False)
        If specified, only consider paths of length <= cutoff.

    Returns
    -------
    A dict keyed by edge 2-tuple to the number of shortest paths
    which use that edge. Where more than one path is shortest
    the count is divided equally among paths.
    g        R   (   t   edgest   _edge_betweennessR   (	   R	   R   R   t   uR
   R   R   t   et	   ubetweenv(    (    sB   lib/python2.7/site-packages/networkx/algorithms/centrality/load.pyR   š   s    c         C   sn  t  j |  | d | d t ƒ\ } } g  t | j ƒ  d t d ƒ ƒD] \ } } | ^ qC } i  }	 x= |  j | ƒ D], \ }
 } d |	 |
 | f <d |	 | |
 f <qq WxÆ | ri| 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   t   keyi   g      ð?(	   R   R   R   t   sortedR   R   R&   R   R   (   R	   R   t   nodesR   R   R   t   nt   dR"   R#   R(   R
   R$   t   wR%   (    (    sB   lib/python2.7/site-packages/networkx/algorithms/centrality/load.pyR'   ½   s"    $7	$6(   t   __doc__t
   __future__R    t   operatorR   t   networkxR   t   __all__R   R   R   R   R   R   R   R'   (    (    (    sB   lib/python2.7/site-packages/networkx/algorithms/centrality/load.pyt   <module>   s   K5#