ó
ú£Õ\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   s  | d k	 rM d } x4 |  D], } t |  | | | | ƒ } | | | 7} q W| Si  j |  d ƒ } xK | D]C } t |  | | t | ƒ } x" | D] } | | c | | 7<q‹ Wqf W| r	t | ƒ }	 |	 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.
    For this load algorithm see the reference
    Scientific collaboration networks: II.
    Shortest paths, weighted networks, and centrality,
    M. E. J. Newman, Phys. Rev. E 64, 016132 (2001).

    g        i   g      ð?i   N(   t   Nonet   _node_betweennesst   fromkeyst   Falset   len(   t   Gt   vt   cutofft
   normalizedt   weightt   betweennesst   sourcet   ubetweent   vkt   ordert   scale(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/load.pyt   newman_betweenness_centrality   s&    +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   popR   t   float(   R   R   R	   R
   R   t   predt   lengtht   vertt   lt   onodest   betweenR   t	   num_pathst   xR   (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/load.pyR   T   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(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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#   (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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%   (    (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/load.pyt   <module>   s   		>5