ó ŸÃÒYc@ s}dZddlmZddlmZddlmZddlmZddlm Z ddlZddl Z ddl m Z ydd l m ZWnek r«eZnXeZddlZdd lmZd d d dgZd„Zd„Zd„Zedƒdddd„ƒZedƒdddddd„ƒZedƒdddddd„ƒZdddddd„ZdS(s!Generators for geometric graphs. iÿÿÿÿ(tdivision(t bisect_left(t combinations(tproduct(tsqrtN(tuniform(tcKDTree(tnodes_or_numbertgeographical_threshold_grapht waxman_graphtnavigable_small_world_graphtrandom_geometric_graphcC s#ttd„t||ƒDƒƒƒS(s´Returns the Euclidean distance between the vectors ``x`` and ``y``. Each of ``x`` and ``y`` can be any iterable of numbers. The iterables must be of the same length. cs s#|]\}}||dVqdS(iN((t.0tatb((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys -s(Rtsumtzip(txty((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyt euclidean&sc sutj|dƒ}tt|jƒŒƒ\‰}t|ƒ}|j||ƒ}‡fd†|Dƒ}|j|ƒdS(sUConstruct edges for random geometric graph. Requires scipy to be installed. tposc3 s)|]\}}ˆ|ˆ|fVqdS(N((R tutv(tnodes(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys 9sN(tnxtget_node_attributestlistRtitemstKDTreet query_pairstadd_edges_from(tGtradiustpRtcoordstkdtreet edge_indexestedges((Rss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyt_fast_construct_edges0s  c s~xwt|jddƒdƒD]Z\\}}\}}t‡fd†t||ƒDƒƒ|ˆkr|j||ƒqqWdS(s`Construct edges for random geometric graph. Works without scipy, but in `O(n^2)` time. tdataRic3 s)|]\}}t||ƒˆVqdS(N(tabs(R R R(R!(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys DsN(RRRRtadd_edge(RR R!RtpuRtpv((R!ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyt_slow_construct_edges=s4/iic sŠ|\}}tjƒ}|j|ƒ|dkrJ‡fd†|Dƒ}ntj||dƒtrvt|||ƒnt|||ƒ|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 distance between the nodes is at most `radius`. Edges are determined using a KDTree when SciPy is available. This reduces the time complexity from $O(n^2)$ to $O(n)$. Parameters ---------- n : int or iterable Number of nodes or iterable 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. p : float Which Minkowski distance metric to use. `p` has to meet the condition ``1 <= p <= infinity``. If this argument is not specified, the $L^2$ metric (the Euclidean distance metric) is used. This should not be confused with the `p` of an ErdÅ‘s-Rényi random graph, which represents probability. Returns ------- Graph A random geometric graph, undirected and without self-loops. Each node has a node attribute ``'pos'`` that stores the position of that node in Euclidean space as provided by the ``pos`` keyword argument or, if ``pos`` was not provided, as generated by this function. 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 uses a *k*-d tree to build the graph. 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. c s8i|].}gtˆƒD]}tjƒ^q|“qS((trangetrandom(R Rti(tdim(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys šs RN(RtGraphtadd_nodes_fromtNonetset_node_attributest_is_scipy_availableR&R,(tnR R0RR!tn_nameRR((R0ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyR HsL    c  sâ|\}}tjƒ} | j|ƒˆdkrDd„| Dƒ‰nˆdkri‡fd†|Dƒ‰nˆdkr~t‰ntj| ˆdƒtj| ˆdƒ‡‡‡‡‡fd†} | jt| t| dƒƒƒ| S(sÅ 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 distance between $u$ and $v$, and $\theta$, $\alpha$ are parameters. Parameters ---------- n : int or iterable Number of nodes or iterable 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. metric : function A metric on vectors of numbers (represented as lists or tuples). This must be a function that accepts two lists (or tuples) as input and yields a number as output. The function must also satisfy the four requirements of a `metric`_. Specifically, if $d$ is the function and $x$, $y$, and $z$ are vectors in the graph, then $d$ must satisfy 1. $d(x, y) \ge 0$, 2. $d(x, y) = 0$ if and only if $x = y$, 3. $d(x, y) = d(y, x)$, 4. $d(x, z) \le d(x, y) + d(y, z)$. If this argument is not specified, the Euclidean distance metric is used. .. _metric: https://en.wikipedia.org/wiki/Metric_%28mathematics%29 Returns ------- Graph A random geographic threshold graph, undirected and without self-loops. Each node has a node attribute ``pos`` that stores the position of that node in Euclidean space as provided by the ``pos`` keyword argument or, if ``pos`` was not provided, as generated by this function. Similarly, each node has a node attribute ``weight`` that stores the weight of that node as provided or as generated. Examples -------- Specify an alternate distance metric using the ``metric`` keyword argument. For example, to use the `taxicab metric`_ instead of the default `Euclidean metric`_:: >>> dist = lambda x, y: sum(abs(a - b) for a, b in zip(x, y)) >>> G = nx.geographical_threshold_graph(10, 0.1, metric=dist) .. _taxicab metric: https://en.wikipedia.org/wiki/Taxicab_geometry .. _Euclidean metric: https://en.wikipedia.org/wiki/Euclidean_distance 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 cS s"i|]}tjdƒ|“qS(i(R.t expovariate(R R((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys s c s8i|].}gtˆƒD]}tjƒ^q|“qS((R-R.(R RR/(R0(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys s tweightRc sU|\}}ˆ|ˆ|}}ˆ|ˆ|}}ˆˆ||ƒˆ||kS(N((tpairRRtu_postv_postu_weighttv_weight(talphatmetricRtthetaR9(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyt should_joins iN( RR1R2R3RR4RtfilterR( R6RAR?R0RR9R@R7RRRB((R?R0R@RRAR9ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyR¥sc       gš™™™™™Ù?gš™™™™™¹?ic  s|\}}tjƒ}|j|ƒ|\‰‰ ‰‰‡‡‡‡ fd†|Dƒ‰tj|ˆdƒˆdkr~t‰nˆdkrÊt‡fd†tˆjƒdƒDƒƒ‰‡‡fd†‰n‡fd†‰‡‡‡‡fd†} |j t | t|dƒƒƒ|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 distance `d` is joined by an edge with probability .. math:: p = \beta \exp(-d / \alpha 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 or iterable Number of nodes or iterable of nodes beta: float Model parameter alpha: 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)`. metric : function A metric on vectors of numbers (represented as lists or tuples). This must be a function that accepts two lists (or tuples) as input and yields a number as output. The function must also satisfy the four requirements of a `metric`_. Specifically, if $d$ is the function and $x$, $y$, and $z$ are vectors in the graph, then $d$ must satisfy 1. $d(x, y) \ge 0$, 2. $d(x, y) = 0$ if and only if $x = y$, 3. $d(x, y) = d(y, x)$, 4. $d(x, z) \le d(x, y) + d(y, z)$. If this argument is not specified, the Euclidean distance metric is used. .. _metric: https://en.wikipedia.org/wiki/Metric_%28mathematics%29 Returns ------- Graph A random Waxman graph, undirected and without self-loops. Each node has a node attribute ``'pos'`` that stores the position of that node in Euclidean space as generated by this function. Examples -------- Specify an alternate distance metric using the ``metric`` keyword argument. For example, to use the "`taxicab metric`_" instead of the default `Euclidean metric`_:: >>> dist = lambda x, y: sum(abs(a - b) for a, b in zip(x, y)) >>> G = nx.waxman_graph(10, 0.5, 0.1, metric=dist) .. _taxicab metric: https://en.wikipedia.org/wiki/Taxicab_geometry .. _Euclidean metric: https://en.wikipedia.org/wiki/Euclidean_distance Notes ----- Starting in NetworkX 2.0 the parameters alpha and beta align with their usual roles in the probability distribution. In earlier versions their positions in the expresssion were reversed. Their position in the calling sequence reversed as well to minimize backward incompatibility. References ---------- .. [1] B. M. Waxman, *Routing of multipoint connections*. IEEE J. Select. Areas Commun. 6(9),(1988) 1617--1622. c s1i|]'}tˆˆƒtˆˆƒf|“qS((R(R R(txmaxtxmintymaxtymin(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys ~s Rc3 s$|]\}}ˆ||ƒVqdS(N((R RR(R@(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys Šsic sˆˆ|ˆ|ƒS(N((RR(R@R(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyt‹sc stjƒˆS(N(R.(RR(tL(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyRHsc s,tjƒˆtjˆ|Œ ˆˆƒkS(N(R.tmathtexp(R:(RIR?tbetatdist(ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyRBsN( RR1R2R4R3RtmaxRtvaluesRRC( R6RLR?RItdomainR@R7RRRB(( RIR?RLRMR@RRDRERFRGss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyR &sS      +c C s“|dkrtjdƒ‚n|dkr<tjdƒ‚n|dkrZtjdƒ‚n|d k rvtj|ƒntjƒ}ttt|ƒd|ƒƒ}xì|D]ä}dg} xp|D]h} || krÕq½nt d„t || ƒDƒƒ} | |kr|j || ƒn| j | | ƒq½Wttj j| ƒƒ} xGt|ƒD]9} |t| tjd| dƒƒ}|j ||ƒqNWq§W|S( s7Return 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 \times 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 >= 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 >= 0$ and $r >= 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 length of one side of the lattice; the number of nodes in the graph is therefore $n^2$. 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. isp must be >= 1isq must be >= 0sr must be >= 1trepeatcs s%|]\}}t||ƒVqdS(N(R((R R R((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pys ÓsiÿÿÿÿN(RtNetworkXExceptionR3R.tseedtDiGraphRRR-RRR)tappendtutilst accumulateRR(R6R!tqtrR0RSRRtp1tprobstp2tdtcdft_ttarget((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyR —s0-          #(iiii( t__doc__t __future__RtbisectRt itertoolsRRRJRR.Rt scipy.spatialRRt ImportErrortFalseR5tTruetnetworkxRtnetworkx.utilsRt__all__RR&R,R3R RR R (((ss/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/geometric.pyts:       \    o