ó
|£*^c           @   sñ   d  Z  d j d d d g ƒ Z d d d d g Z d	 d
 l m Z d	 d l m Z d	 d l m	 Z	 d	 d l
 Z
 d	 d l Z d	 d l Z d	 d l Z d d d „ Z d d d d d „ Z d d „ Z d d d d d „ Z d d d d d d „ Z d S(   s"   
Generators for geometric graphs.
s   
s   Aric Hagberg (hagberg@lanl.gov)s    Dan Schult (dschult@colgate.edu)s!   Ben Edwards (BJEdwards@gmail.com)t   random_geometric_grapht   waxman_grapht   geographical_threshold_grapht   navigable_small_world_graphiÿÿÿÿ(   t   bisect_left(   t   reduce(   t   productNi   c         C   s0  t  j ƒ  } d | _ | j t |  ƒ ƒ | d k r{ xW | D]9 }  g  t d | ƒ D] } t j ƒ  ^ qQ | j |  d <q; Wn t  j | d | ƒ | j	 d t
 ƒ } x‰ | r+| j ƒ  \ } } | d }	 x` | D]X \ }
 } | d } t d „  t |	 | ƒ Dƒ ƒ } | | d k rÌ | j | |
 ƒ qÌ qÌ Wq£ W| S(   sä  Returns a random geometric graph in the unit cube.

    The random geometric graph model places ``n`` nodes uniformly at random in
    the unit cube. Two nodes are joined by an edge if the Euclidean distance
    between the nodes is at most ``radius``.

    Parameters
    ----------
    n : int
        Number of nodes
    radius: float
        Distance threshold value
    dim : int, optional
        Dimension of graph
    pos : dict, optional
        A dictionary keyed by node with node positions as values.

    Returns
    -------
    Graph

    Examples
    --------
    Create a random geometric graph on twenty nodes where nodes are joined by
    an edge if their distance is at most 0.1::

    >>> G = nx.random_geometric_graph(20, 0.1)

    Notes
    -----
    This algorithm currently only supports Euclidean distance.

    This uses an `O(n^2)` algorithm to build the graph.  A faster algorithm
    is possible using k-d trees.

    The ``pos`` keyword argument can be used to specify node positions so you
    can create an arbitrary distribution and domain for positions.

    For example, to use a 2D Gaussian distribution of node positions with mean
    (0, 0) and standard deviation 2::

    >>> import random
    >>> n = 20
    >>> p = {i: (random.gauss(0, 2), random.gauss(0, 2)) for i in range(n)}
    >>> G = nx.random_geometric_graph(n, 0.2, pos=p)

    References
    ----------
    .. [1] Penrose, Mathew, Random Geometric Graphs,
       Oxford Studies in Probability, 5, 2003.
    s   Random Geometric Graphi    t   post   datac         s   s#   |  ] \ } } | | d  Vq d S(   i   N(    (   t   .0t   at   b(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pys	   <genexpr>d   s    i   N(   t   nxt   Grapht   namet   add_nodes_fromt   ranget   Nonet   randomt   nodet   set_node_attributest   nodest   Truet   popt   sumt   zipt   add_edge(   t   nt   radiust   dimR   t   Gt   iR   t   ut   dut   put   vt   dvt   pvt   d(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyR       s"    4	:	

c   	      C   s  t  j ƒ  } | j g  t |  ƒ D] } | ^ q ƒ | d k rl x> | D]  }  t j d ƒ | j |  d <qE Wn t  j | d | ƒ | d k rÒ xW | D]9 }  g  t d | ƒ D] } t j ƒ  ^ q¨ | j |  d <q’ Wn t  j | d | ƒ | j	 t
 | | | ƒ ƒ | S(   ss  Returns a geographical threshold graph.

    The geographical threshold graph model places ``n`` nodes uniformly at
    random in a rectangular domain.  Each node `u` is assigned a weight `w_u`.
    Two nodes `u` and `v` are joined by an edge if

    .. math::

       w_u + w_v \ge \theta r^{\alpha}

    where `r` is the Euclidean distance between `u` and `v`, and `\theta`,
    `\alpha` are parameters.

    Parameters
    ----------
    n : int
        Number of nodes
    theta: float
        Threshold value
    alpha: float, optional
        Exponent of distance function
    dim : int, optional
        Dimension of graph
    pos : dict
        Node positions as a dictionary of tuples keyed by node.
    weight : dict
        Node weights as a dictionary of numbers keyed by node.

    Returns
    -------
    Graph

    Examples
    --------
    >>> G = nx.geographical_threshold_graph(20, 50)

    Notes
    -----

    If weights are not specified they are assigned to nodes by drawing randomly
    from the exponential distribution with rate parameter `\lambda=1`.  To
    specify weights from a different distribution, use the ``weight`` keyword
    argument::

    >>> import random
    >>> n = 20
    >>> w = {i: random.expovariate(5.0) for i in range(n)}
    >>> G = nx.geographical_threshold_graph(20, 50, weight=w)

    If node positions are not specified they are randomly assigned from the
    uniform distribution.

    References
    ----------
    .. [1] Masuda, N., Miwa, H., Konno, N.:
       Geographical threshold graphs with small-world and scale-free
       properties.
       Physical Review E 71, 036108 (2005)
    .. [2]  Milan BradonjiÄ‡, Aric Hagberg and Allon G. Percus,
       Giant component and connectivity in geographical threshold graphs,
       in Algorithms and Models for the Web-Graph (WAW 2007),
       Antony Bonato and Fan Chung (Eds), pp. 209--216, 2007
    g      ð?t   weighti    R   N(   R   R   R   R   R   R   t   expovariateR   R   t   add_edges_fromt   geographical_threshold_edges(	   R   t   thetat   alphaR   R   R'   R   R#   R   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyR   j   s    A&!:c         c   sÂ   |  j  d t ƒ } x© | r½ | j ƒ  \ } } | d } | d } xv | D]n \ } }	 |	 d }
 |	 d } t j t d „  t | | ƒ Dƒ ƒ ƒ } | |
 | | | k rH | | f VqH qH Wq Wd S(   s¡   Generates edges for a geographical threshold graph given a graph with
    positions and weights assigned as node attributes ``'pos'`` and
    ``'weight'``.

    R   R'   R   c         s   s#   |  ] \ } } | | d  Vq d S(   i   N(    (   R	   R
   R   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pys	   <genexpr>Ì   s    N(   R   R   R   t   matht   sqrtR   R   (   R   R+   R,   R   R    R!   t   wuR"   R#   R$   t   wvR%   t   r(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyR*   ¾   s    	



(gš™™™™™Ù?gš™™™™™¹?i    i   c         C   st  t  j ƒ  } | j t |  ƒ ƒ | \ } } } }	 xI | D]A }  | | | t j ƒ  | |	 | t j ƒ  f | j |  d <q8 W| d k r$d }
 t t  j | d ƒ j	 ƒ  ƒ } xb | r| j
 ƒ  \ } } xC | D]; \ } } | | d | | d } | |
 k rÏ | }
 qÏ qÏ Wq° Wt j |
 ƒ }
 n | }
 | j ƒ  } | d k rûx+| r÷| j
 ƒ  } | j | d \ } } xƒ | D]{ } | j | d \ } } t j | | d | | d ƒ } t j ƒ  | t j | | |
 ƒ k  ru| j | | ƒ ququWqEWnu xr | ro| j
 ƒ  } xY | D]Q } t j ƒ  |
 } t j ƒ  | t j | | |
 ƒ k  r| j | | ƒ qqWqþW| S(   sÇ  Return a Waxman random graph.

    The Waxman random graph model places ``n`` nodes uniformly at random in a
    rectangular domain. Each pair of nodes at Euclidean distance `d` is joined
    by an edge with probability

    .. math::
            p = \alpha \exp(-d / \beta L).

    This function implements both Waxman models, using the ``L`` keyword
    argument.

    * Waxman-1: if ``L`` is not specified, it is set to be the maximum distance
      between any pair of nodes.
    * Waxman-2: if ``L`` is specified, the distance between a pair of nodes is
      chosen uniformly at random from the interval `[0, L]`.

    Parameters
    ----------
    n : int
        Number of nodes
    alpha: float
        Model parameter
    beta: float
        Model parameter
    L : float, optional
        Maximum distance between nodes.  If not specified, the actual distance
        is calculated.
    domain : four-tuple of numbers, optional
        Domain size, given as a tuple of the form `(x_min, y_min, x_max,
        y_max)`.

    Returns
    -------
    G: Graph

    References
    ----------
    .. [1]  B. M. Waxman, Routing of multipoint connections.
       IEEE J. Select. Areas Commun. 6(9),(1988) 1617-1622.
    R   i    i   N(   R   R   R   R   R   R   R   t   listt   get_node_attributest   valuesR   R-   R.   R   t   expR   (   R   R,   t   betat   Lt   domainR   t   xmint   ymint   xmaxt   ymaxt   lR   t   x1t   y1t   x2t   y2t   r2R   R    R#   R1   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyR   Ñ   sD    +*		#(	(c      	   C   s“  | d k  r t  j d ƒ ‚ n  | d k  r< t  j d ƒ ‚ n  | d k  rZ t  j d ƒ ‚ n  | d	 k	 rv t j | ƒ n  t  j ƒ  } t t t |  ƒ d | ƒƒ } xì | D]ä } d g }	 xp | D]h }
 | |
 k rÕ q½ n  t	 d „  t
 | |
 ƒ Dƒ ƒ } | | k r| j | |
 ƒ n  |	 j | | ƒ q½ Wt t  j j |	 ƒ ƒ } xG t | ƒ D]9 } | t | t j d | d ƒ ƒ } | j | | ƒ qNWq§ W| S(
   sæ  Return a navigable small-world graph.

    A navigable small-world graph is a directed grid with additional long-range
    connections that are chosen randomly.

      [...] we begin with a set of nodes [...] that are identified with the set
      of lattice points in an `n 	imes n` square, `\{(i, j): i \in \{1, 2,
      \ldots, n\}, j \in \{1, 2, \ldots, n\}\}`, and we define the *lattice
      distance* between two nodes `(i, j)` and `(k, l)` to be the number of
      "lattice steps" separating them: `d((i, j), (k, l)) = |k - i| + |l - j|`.
      For a universal constant `p \geq 1`, the node `u` has a directed edge to
      every other node within lattice distance `p` --- these are its *local
      contacts*. For universal constants `q \ge 0` and `r \ge 0` we also
      construct directed edges from `u` to `q` other nodes (the *long-range
      contacts*) using independent random trials; the `i`th directed edge from
      `u` has endpoint `v` with probability proportional to `[d(u,v)]^{-r}`.

      -- [1]_

    Parameters
    ----------
    n : int
        The number of nodes.
    p : int
        The diameter of short range connections. Each node is joined with every
        other node within this lattice distance.
    q : int
        The number of long-range connections for each node.
    r : float
        Exponent for decaying probability of connections.  The probability of
        connecting to a node at lattice distance `d` is `1/d^r`.
    dim : int
        Dimension of grid
    seed : int, optional
        Seed for random number generator (default=None).

    References
    ----------
    .. [1] J. Kleinberg. The small-world phenomenon: An algorithmic
       perspective. Proc. 32nd ACM Symposium on Theory of Computing, 2000.
    i   s   p must be >= 1i    s   q must be >= 0s   r must be >= 1t   repeatc         s   s%   |  ] \ } } t  | | ƒ Vq d  S(   N(   t   abs(   R	   R
   R   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pys	   <genexpr>b  s    iÿÿÿÿN(   R   t   NetworkXExceptionR   R   t   seedt   DiGraphR2   R   R   R   R   R   t   appendt   utilst
   accumulateR   t   uniform(   R   t   pt   qR1   R   RF   R   R   t   p1t   probst   p2R&   t   cdft   _t   target(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyR   )  s0    *	#(   i    i    i   i   (   t   __doc__t   joint
   __author__t   __all__t   bisectR   t	   functoolsR   t	   itertoolsR   R-   R   t   syst   networkxR   R   R    R   R*   R   R   (    (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/generators/geometric.pyt   <module>   s$   		$K	SX