ó
ú£Õ\c           @  s  d  Z  d d l m 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.
iÿÿÿÿ(   t   print_functions   
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_graph(   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  Return the random geometric graph in the unit cube.

    The random geometric graph model places n nodes uniformly at random 
    in the unit cube  Two nodes `u,v` are connected with an edge if
    `d(u,v)<=r` where `d` is the Euclidean distance and `r` is a radius 
    threshold.

    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
    --------
    >>> G = nx.random_geometric_graph(20,0.1)

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

    The pos keyword can be used to specify node positions so you can create
    an arbitrary distribution and domain for positions.  If you need a distance
    function other than Euclidean you'll have to hack the algorithm.

    E.g to use a 2d Gaussian distribution of node positions with mean (0,0)
    and std. dev. 2

    >>> import random
    >>> n=20
    >>> p=dict((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(    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pys	   <genexpr>c   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(    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pyR   !   s"    1	:	

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(   s’  Return 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,v` are connected with 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 an the exponential distribution with rate parameter `\lambda=1`.
    To specify a weights from a different distribution assign them to a 
    dictionary and pass it as the weight= keyword

    >>> import random
    >>> n = 20
    >>> w=dict((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    (    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pyR   h   s    ?&!: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(   NR	   R(   R   c         s  s#   |  ] \ } } | | d  Vq d S(   i   N(    (   R
   R   R   (    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pys	   <genexpr>Ä   s    (   R   R   R   t   matht   sqrtR   R   (   R   R,   R-   R   R!   R"   t   wuR#   R$   R%   t   wvR&   t   r(    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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(   sf  Return a Waxman random graph.

    The Waxman random graph models place n nodes uniformly at random
    in a rectangular domain. Two nodes u,v are connected with an edge
    with probability

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

    This function implements both Waxman models.            

    Waxman-1:  `L` not specified
       The distance `d` is the Euclidean distance between the nodes u and v.
       `L` is the maximum distance between all nodes in the graph.

    Waxman-2: `L` specified
       The distance `d` is chosen randomly in `[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 : tuple of numbers, optional
         Domain size (xmin, ymin, xmax, ymax)

    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$   R2   (    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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(
   sx  Return a navigable small-world graph.

    A navigable small-world graph is a directed grid with additional
    long-range connections that are chosen randomly.  From [1]_:

    Begin with a set of nodes that are identified with the set of lattice
    points in an `n \times n` square, `{(i,j): i\in {1,2,\ldots,n}, j\in {1,2,\ldots,n}}`
    and 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`, the node `u` has a directed edge to every other 
    node within lattice distance `p` (local contacts) .

    For universal constants `q\ge 0` and `r\ge 0` construct directed edges from `u` to `q`
    other nodes (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}`.

    Parameters
    ----------
    n : int
        The number of nodes.
    p : int
        The diameter of short range connections. Each node is connected
        to every other node within lattice distance p.
    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   (    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pys	   <genexpr>W  s    iÿÿÿÿN(   R   t   NetworkXExceptionR   R   t   seedt   DiGraphR3   R   R   R   R   R   t   appendt   utilst   cumulative_sumR   t   uniform(   R   t   pt   qR2   R   RG   R   R   t   p1t   probst   p2R'   t   cdft   _t   target(    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pyR      s0    (	#(   i    i    i   i   (   t   __doc__t
   __future__R    t   joint
   __author__t   __all__t   bisectR   t	   functoolsR   t	   itertoolsR   R.   R   t   syst   networkxR   R   R   R   R+   R   R   (    (    (    sU   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/generators/geometric.pyt   <module>   s&   		$GPX