ó
 ,µ[c           @   s…   d  Z  d d l Z d d d d d d g Z d d	 „ Z d d d
 „ Z d e d „ Z d e d „ Z d e d „ Z	 d e d „ Z
 d S(   s:   Graph diameter, radius, eccentricity and other properties.iÿÿÿÿNt   extrema_boundingt   eccentricityt   diametert   radiust	   peripheryt   centerc            s  t  |  j ƒ  ƒ } t | d | j ƒ} t | ƒ } t } t  j |  d ƒ ‰  t  j |  | ƒ ‰ t |  ƒ } | ‰ d ‰ | ‰ d ‰ xÞ| ra| r™ | } n | } | } t  t j	 |  | ƒ ƒ }	 t |	 ƒ | k rè d }
 t j
 |
 ƒ ‚ n  t |	 j ƒ  ƒ } d } d } x« | D]£ } |	 | } t ˆ  | t | | | ƒ ƒ ˆ  | <} t ˆ | | | ƒ ˆ | <} t ˆ  | ˆ ƒ ‰ t ˆ  | ˆ ƒ ‰ t ˆ | ˆ ƒ ‰ t ˆ | ˆ ƒ ‰ qW| d k râ‡  ‡ ‡ ‡ f d †  | Dƒ } nŸ | d k r‡  ‡ ‡ ‡ f d †  | Dƒ } nq | d k r>‡  ‡ ‡ ‡ f d	 †  | Dƒ } nC | d
 k rl‡  ‡ ‡ ‡ f d †  | Dƒ } n | d k ri  } n  | j ‡  ‡ f d †  | Dƒ ƒ | | 8} x° | D]¨ } | d k s ˆ  | ˆ  | k rì| | | | k s ˆ  | ˆ  | k  r	| } n  | d k sQˆ | ˆ | k r=| | | | k sQˆ | ˆ | k r²| } q²q²Wq„ W| d k rrˆ S| d k r‚ˆ S| d k r»g  |  D] } ˆ  | ˆ k r•| ^ q•} | S| d
 k rôg  |  D] } ˆ | ˆ k rÎ| ^ qÎ} | S| d k rˆ  Sd S(   sÃ  Compute requested extreme distance metric of undirected graph G

    Computation is based on smart lower and upper bounds, and in practice
    linear in the number of nodes, rather than quadratic (except for some
    border cases such as complete graphs or circle shaped graphs).

    Parameters
    ----------
    G : NetworkX graph
       An undirected graph

    compute : string denoting the requesting metric
       "diameter" for the maximal eccentricity value,
       "radius" for the minimal eccentricity value,
       "periphery" for the set of nodes with eccentricity equal to the diameter
       "center" for the set of nodes with eccentricity equal to the radius

    Returns
    -------
    value : value of the requested metric
       int for "diameter" and "radius" or
       list of nodes for "center" and "periphery"

    Raises
    ------
    NetworkXError
        If the graph consists of multiple components

    Notes
    -----
    This algorithm was proposed in the following papers:

    F.W. Takes and W.A. Kosters, Determining the Diameter of Small World
    Networks, in Proceedings of the 20th ACM International Conference on
    Information and Knowledge Management (CIKM 2011), pp. 1191-1196, 2011.
    doi: https://doi.org/10.1145/2063576.2063748

    F.W. Takes and W.A. Kosters, Computing the Eccentricity Distribution of
    Large Graphs, Algorithms 6(1): 100-118, 2013.
    doi: https://doi.org/10.3390/a6010100

    M. Borassi, P. Crescenzi, M. Habib, W.A. Kosters, A. Marino and F.W. Takes,
    Fast Graph Diameter and Radius BFS-Based Computation in (Weakly Connected)
    Real-World Graphs, Theoretical Computer Science 586: 59-80, 2015.
    doi: https://doi.org/10.1016/j.tcs.2015.02.033
    t   keyi    s5   Cannot compute metric because graph is not connected.R   c            s:   h  |  ]0 } ˆ | ˆ k r d  ˆ  | ˆ k r | ’ q S(   i   (    (   t   .0t   i(   t	   ecc_lowert	   ecc_uppert   maxlowert   maxupper(    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pys	   <setcomp>{   s   	 R   c            s>   h  |  ]4 } ˆ  | ˆ k r ˆ | d  d ˆ k r | ’ q S(   i   i   (    (   R   R   (   R	   R
   t   minlowert   minupper(    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pys	   <setcomp>   s   	 R   c            sB   h  |  ]8 } ˆ | ˆ k  r ˆ ˆ k s8 ˆ  | ˆ k r | ’ q S(    (    (   R   R   (   R	   R
   R   R   (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pys	   <setcomp>ƒ   s   	 R   c            sJ   h  |  ]@ } ˆ  | ˆ k r ˆ ˆ k s@ ˆ | d  d ˆ k  r | ’ q S(   i   i   (    (   R   R   (   R	   R
   R   R   (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pys	   <setcomp>‡   s   	 t   eccentricitiesc         3   s)   |  ] } ˆ  | ˆ | k r | Vq d  S(   N(    (   R   R   (   R	   R
   (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pys	   <genexpr>   s    N(   t   dictt   degreet   maxt   gett   lent   Falset   fromkeyst   sett   networkxt"   single_source_shortest_path_lengtht   NetworkXErrort   valuest   Nonet   mint   update(   t   Gt   computet   degreest   minlowernodet   Nt   hight
   candidatest   maxuppernodet   currentt   distt   msgt   current_eccR   t   dt   lowt   uppt	   ruled_outt   vt   pt   c(    (   R	   R
   R   R   R   R   sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR       s†    1			
(""""	 
	))c   	      C   s  |  j  ƒ  } i  } xÐ |  j | ƒ D]¿ } | d k rU t j |  | ƒ } t | ƒ } n= y | | } t | ƒ } Wn  t k
 r‘ t j d ƒ ‚ n X| | k rË |  j ƒ  r³ d } n d } t j | ƒ ‚ n  t	 | j
 ƒ  ƒ | | <q" W| |  k rù | | S| Sd S(   sÝ  Return the eccentricity of nodes in G.

    The eccentricity of a node v is the maximum distance from v to
    all other nodes in G.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    v : node, optional
       Return value of specified node

    sp : dict of dicts, optional
       All pairs shortest path lengths as a dictionary of dictionaries

    Returns
    -------
    ecc : dictionary
       A dictionary of eccentricity values keyed by node.
    s   Format of "sp" is invalid.sH   Found infinite path length because the digraph is not strongly connecteds=   Found infinite path length because the graph is not connectedN(   t   ordert   nbunch_iterR   R   R   R   t	   TypeErrorR   t   is_directedR   R   (	   R   R/   t   spR2   t   et   nt   lengtht   LR)   (    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR   Á   s(    
	c         C   s`   | t  k r5 | d k r5 |  j ƒ  r5 t |  d d ƒS| d k rP t |  ƒ } n  t | j ƒ  ƒ S(   sg  Return the diameter of the graph G.

    The diameter is the maximum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    d : integer
       Diameter of graph

    See Also
    --------
    eccentricity
    R    R   N(   t   TrueR   R5   R    R   R   R   (   R   R7   t	   usebounds(    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR   û   s
    %c         C   s   | t  k r5 | d k r5 |  j ƒ  r5 t |  d d ƒS| d k rP t |  ƒ } n  t | j ƒ  ƒ } g  | D] } | | | k ri | ^ qi } | S(   sc  Return the periphery of the graph G.

    The periphery is the set of nodes with eccentricity equal to the diameter.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    p : list
       List of nodes in periphery
    R    R   N(   R;   R   R5   R    R   R   R   (   R   R7   R<   R   R/   R0   (    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR     s    %)c         C   s`   | t  k r5 | d k r5 |  j ƒ  r5 t |  d d ƒS| d k rP t |  ƒ } n  t | j ƒ  ƒ S(   s5  Return the radius of the graph G.

    The radius is the minimum eccentricity.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    r : integer
       Radius of graph
    R    R   N(   R;   R   R5   R    R   R   R   (   R   R7   R<   (    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR   3  s
    %c         C   s   | t  k r5 | d k r5 |  j ƒ  r5 t |  d d ƒS| d k rP t |  ƒ } n  t | j ƒ  ƒ } g  | D] } | | | k ri | ^ qi } | S(   sT  Return the center of the graph G.

    The center is the set of nodes with eccentricity equal to radius.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    e : eccentricity dictionary, optional
      A precomputed dictionary of eccentricities.

    Returns
    -------
    c : list
       List of nodes in center
    R    R   N(   R;   R   R5   R    R   R   R   (   R   R7   R<   R   R/   R0   (    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyR   L  s    %)(   t   __doc__R   t   __all__R    R   R   R   R   R   R   R   (    (    (    sD   lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyt   <module>   s   	¯: