ó
 ,µ[c           @   s#  d  Z  d d l Z d d l m Z d d l m Z d d d d g Z e d	 ƒ e d
 ƒ e d ƒ d e d d „ ƒ ƒ ƒ Z	 e d ƒ e d
 ƒ e d ƒ d d e d d „ ƒ ƒ ƒ Z
 e d	 ƒ e d
 ƒ e d ƒ d d d d „ ƒ ƒ ƒ Z e d	 ƒ e d
 ƒ e d ƒ d d d d „ ƒ ƒ ƒ Z d S(   sG  Functions for estimating the small-world-ness of graphs.

A small world network is characterized by a small average shortest path length,
and a large clustering coefficient.

Small-worldness is commonly measured with the coefficient sigma or omega.

Both coefficients compare the average clustering coefficient and shortest path
length of a given graph against the same quantities for an equivalent random
or lattice graph.

For more information, see the Wikipedia article on small-world network [1]_.

.. [1] Small-world network:: https://en.wikipedia.org/wiki/Small-world_network

iÿÿÿÿN(   t   not_implemented_for(   t   py_random_statet   random_referencet   lattice_referencet   sigmat   omegai   t   directedt
   multigraphi   c         C   sÆ  |  j  ƒ  r$ d } t j | ƒ ‚ n  t |  ƒ d k  rH t j d ƒ ‚ n  d d l m } m } t j j } |  j	 ƒ  }  t
 |  j ƒ  Œ  \ } }	 t j j |	 ƒ }
 t |  ƒ } t j |  ƒ } | | } t | | | | d d ƒ } d } xÔt | ƒ D]Æ} d } x·| | k  r½| d d	 |
 d
 | ƒ\ } } | | k rCqn  | | } | | } | j t |  j | ƒ ƒ ƒ } | j t |  j | ƒ ƒ ƒ } | j | ƒ } | j | ƒ } | | | | g k s| | | | g k ráqn  | |  | k r°| |  | k r°|  j | | ƒ |  j | | ƒ |  j | | ƒ |  j | | ƒ | r¢| |  | | ƒ d k r¢|  j | | ƒ |  j | | ƒ |  j | | ƒ |  j | | ƒ q°| d 7} Pn  | d 7} qWqø W|  S(   s‚  Compute a random graph by swapping edges of a given graph.

    Parameters
    ----------
    G : graph
        An undirected graph with 4 or more nodes.

    niter : integer (optional, default=1)
        An edge is rewired approximately `niter` times.

    connectivity : boolean (optional, default=True)
        When True, ensure connectivity for the randomized graph.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G : graph
        The randomized graph.

    Notes
    -----
    The implementation is adapted from the algorithm by Maslov and Sneppen
    (2002) [1]_.

    References
    ----------
    .. [1] Maslov, Sergei, and Kim Sneppen.
           "Specificity and stability in topology of protein networks."
           Science 296.5569 (2002): 910-913.
    s3   random_reference() not defined for directed graphs.i   s   Graph has less than four nodes.iÿÿÿÿ(   t   cumulative_distributiont   discrete_sequencei   i   i    t   cdistributiont   seed(   t   is_directedt   nxt   NetworkXErrort   lent   networkx.utilsR   R	   t   connectivityt   local_edge_connectivityt   copyt   zipt   degreet   utilst   number_of_edgest   intt   ranget   choicet   listt	   neighborst   indext   add_edget   remove_edge(   t   Gt   niterR   R   t   msgR   R	   t
   local_connt   keyst   degreest   cdft   nnodest   nedgest   ntriest	   swapcountt   it   nt   ait   cit   at   ct   bt   dt   bit   di(    (    s=   lib/python2.7/site-packages/networkx/algorithms/smallworld.pyR      sV    %
 

* 
i   c         C   s   d d l  } d d l m } m } t j j } |  j ƒ  rR d }	 t j |	 ƒ ‚ n  t	 |  ƒ d k  rv t j d ƒ ‚ n  |  j
 ƒ  }  t |  j ƒ  Œ  \ }
 } | | ƒ } t	 |  ƒ } t j |  ƒ } | d k rÖ| j | | f ƒ } | j d | ƒ } | j | d d d ƒ } | j d | j | | k  | | ƒ ƒ } xŸ t t | j | d	 ƒ ƒ ƒ D]{ } | j | | d | | d  ƒ | | | d d d … f <| | | d d d … f d d d … | | d d … f <qTWn  | | } t | | | | d d	 ƒ } d } xt | ƒ D]} d } xö| | k  r| d	 d
 | d | ƒ\ } } | | k r^q"n  |
 | } |
 | } | j t |  j | ƒ ƒ ƒ } | j t |  j | ƒ ƒ ƒ } |
 j | ƒ } |
 j | ƒ } | | | | g k s"| | | | g k rüq"n  | |  | k r
| |  | k r
| | | f | | | f | | | f | | | f k r
|  j | | ƒ |  j | | ƒ |  j | | ƒ |  j | | ƒ | rù| |  | | ƒ d k rù|  j | | ƒ |  j | | ƒ |  j | | ƒ |  j | | ƒ q| d 7} Pq
n  | d 7} q"WqW|  S(   s”  Latticize the given graph by swapping edges.

    Parameters
    ----------
    G : graph
        An undirected graph with 4 or more nodes.

    niter : integer (optional, default=1)
        An edge is rewired approximatively niter times.

    D : numpy.array (optional, default=None)
        Distance to the diagonal matrix.

    connectivity : boolean (optional, default=True)
        Ensure connectivity for the latticized graph when set to True.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G : graph
        The latticized graph.

    Notes
    -----
    The implementation is adapted from the algorithm by Sporns et al. [1]_.
    which is inspired from the original work by Maslov and Sneppen(2002) [2]_.

    References
    ----------
    .. [1] Sporns, Olaf, and Jonathan D. Zwi.
       "The small world of the cerebral cortex."
       Neuroinformatics 2.2 (2004): 145-162.
    .. [2] Maslov, Sergei, and Kim Sneppen.
       "Specificity and stability in topology of protein networks."
       Science 296.5569 (2002): 910-913.
    iÿÿÿÿN(   R   R	   s4   lattice_reference() not defined for directed graphs.i   s   Graph has less than four nodes.i   i    i   R
   R   (   i    (   t   numpyR   R   R	   R   R   R   R   R   R   R   R   R   R   t   Nonet   zerost   aranget   appendt   whereR   R   t   ceilR   R   R   R   R   R   (   R    R!   t   DR   R   t   npR   R	   R#   R"   R$   R%   R&   R'   R(   t   unt   umt   ut   vR)   R*   R+   R,   R-   R.   R/   R0   R1   R2   R3   R4   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/smallworld.pyR   |   sj    +'&:B
 

* <
id   i
   c         C   sÝ   d d l  } i g  d 6g  d 6} x` t | ƒ D]R } t |  d | d | ƒ} | d j t j | ƒ ƒ | d j t j | ƒ ƒ q- Wt j |  ƒ } t j |  ƒ }	 | j | d ƒ }
 | j | d ƒ } | |
 |	 | } | S(   s‚  Return the small-world coefficient (sigma) of the given graph.

    The small-world coefficient is defined as:
    sigma = C/Cr / L/Lr
    where C and L are respectively the average clustering coefficient and
    average shortest path length of G. Cr and Lr are respectively the average
    clustering coefficient and average shortest path length of an equivalent
    random graph.

    A graph is commonly classified as small-world if sigma>1.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.
    niter : integer (optional, default=100)
        Approximate number of rewiring per edge to compute the equivalent
        random graph.
    nrand : integer (optional, default=10)
        Number of random graphs generated to compute the average clustering
        coefficient (Cr) and average shortest path length (Lr).
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    sigma : float
        The small-world coefficient of G.

    Notes
    -----
    The implementation is adapted from Humphries et al. [1]_ [2]_.

    References
    ----------
    .. [1] The brainstem reticular formation is a small-world, not scale-free,
           network M. D. Humphries, K. Gurney and T. J. Prescott,
           Proc. Roy. Soc. B 2006 273, 503-511, doi:10.1098/rspb.2005.3354.
    .. [2] Humphries and Gurney (2008).
           "Network 'Small-World-Ness': A Quantitative Method for Determining
           Canonical Network Equivalence".
           PLoS One. 3 (4). PMID 18446219. doi:10.1371/journal.pone.0002051.
    iÿÿÿÿNt   Ct   LR!   R   (   R5   R   R   R9   R   t   transitivityt   average_shortest_path_lengtht   mean(   R    R!   t   nrandR   R=   t   randMetricsR+   t   GrRB   RC   t   Crt   LrR   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/smallworld.pyR   ò   s    0c         C   sõ   d d l  } i g  d 6g  d 6} xx t | ƒ D]j } t |  d | d | ƒ} t |  d | d | ƒ} | d j t j | ƒ ƒ | d j t j | ƒ ƒ q- Wt j |  ƒ }	 t j |  ƒ }
 | j | d ƒ } | j | d ƒ } | |
 |	 | } | S(   s|  Return the small-world coefficient (omega) of a graph

    The small-world coefficient of a graph G is:

    omega = Lr/L - C/Cl

    where C and L are respectively the average clustering coefficient and
    average shortest path length of G. Lr is the average shortest path length
    of an equivalent random graph and Cl is the average clustering coefficient
    of an equivalent lattice graph.

    The small-world coefficient (omega) ranges between -1 and 1. Values close
    to 0 means the G features small-world characteristics. Values close to -1
    means G has a lattice shape whereas values close to 1 means G is a random
    graph.

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

    niter: integer (optional, default=100)
        Approximate number of rewiring per edge to compute the equivalent
        random graph.

    nrand: integer (optional, default=10)
        Number of random graphs generated to compute the average clustering
        coefficient (Cr) and average shortest path length (Lr).

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.


    Returns
    -------
    omega : float
        The small-work coefficient (omega)

    Notes
    -----
    The implementation is adapted from the algorithm by Telesford et al. [1]_.

    References
    ----------
    .. [1] Telesford, Joyce, Hayasaka, Burdette, and Laurienti (2011).
           "The Ubiquity of Small-World Networks".
           Brain Connectivity. 1 (0038): 367-75.  PMC 3604768. PMID 22432451.
           doi:10.1089/brain.2011.0038.
    iÿÿÿÿNRB   RC   R!   R   (	   R5   R   R   R   R9   R   RD   RE   RF   (   R    R!   RG   R   R=   RH   R+   RI   t   GlRB   RC   t   ClRK   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/smallworld.pyR   6  s    6(   t   __doc__t   networkxR   R   R    R   t   __all__t   TrueR6   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/networkx/algorithms/smallworld.pyt   <module>   s(   			[			s			A			