__init__.py 38.6 KB
 Tiago Peixoto committed Apr 10, 2008 1 #! /usr/bin/env python Tiago Peixoto committed Mar 07, 2010 2 # -*- coding: utf-8 -*- Tiago Peixoto committed Apr 10, 2008 3 # Tiago Peixoto committed Mar 07, 2010 4 5 # graph_tool -- a general graph manipulation python module # Tiago Peixoto committed Feb 10, 2011 6 # Copyright (C) 2007-2011 Tiago de Paula Peixoto Tiago Peixoto committed Apr 10, 2008 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . Tiago Peixoto committed Jul 15, 2009 21 """ Tiago Peixoto committed Oct 05, 2009 22 graph_tool.generation - Random graph generation Tiago Peixoto committed Jul 15, 2009 23 --------------------------------------------------- Tiago Peixoto committed Oct 05, 2009 24 25 26 27 28 29 30 31 32 33 34 35 Summary +++++++ .. autosummary:: :nosignatures: random_graph random_rewire predecessor_tree line_graph graph_union Tiago Peixoto committed Dec 06, 2009 36 triangulation Tiago Peixoto committed Oct 04, 2010 37 38 lattice geometric_graph Tiago Peixoto committed Nov 13, 2010 39 price_network Tiago Peixoto committed Oct 05, 2009 40 41 42 Contents ++++++++ Tiago Peixoto committed Jul 15, 2009 43 44 """ Tiago Peixoto committed Oct 26, 2008 45 46 from .. dl_import import dl_import dl_import("import libgraph_tool_generation") Tiago Peixoto committed Apr 10, 2008 47 Tiago Peixoto committed Aug 24, 2011 48 from .. import Graph, GraphView, _check_prop_scalar, _prop, _limit_args Tiago Peixoto committed Feb 20, 2010 49 from .. stats import label_parallel_edges, label_self_loops Tiago Peixoto committed Oct 05, 2009 50 import sys, numpy, numpy.random Tiago Peixoto committed Apr 10, 2008 51 Tiago Peixoto committed Sep 06, 2009 52 __all__ = ["random_graph", "random_rewire", "predecessor_tree", "line_graph", Tiago Peixoto committed Nov 13, 2010 53 54 "graph_union", "triangulation", "lattice", "geometric_graph", "price_network"] Tiago Peixoto committed Apr 10, 2008 55 Tiago Peixoto committed May 03, 2010 56 Tiago Peixoto committed Apr 10, 2008 57 def random_graph(N, deg_sampler, deg_corr=None, directed=True, Tiago Peixoto committed Feb 20, 2010 58 parallel_edges=False, self_loops=False, random=True, Tiago Peixoto committed Aug 24, 2011 59 mix_time=10, verbose=False): Tiago Peixoto committed Aug 02, 2009 60 61 62 63 64 65 66 67 68 69 70 71 72 73 r""" Generate a random graph, with a given degree distribution and correlation. Parameters ---------- N : int Number of vertices in the graph. deg_sampler : function A degree sampler function which is called without arguments, and returns a tuple of ints representing the in and out-degree of a given vertex (or a single int for undirected graphs, representing the out-degree). This function is called once per vertex, but may be called more times, if the degree sequence cannot be used to build a graph. deg_corr : function (optional, default: None) Tiago Peixoto committed Dec 21, 2009 74 A function which gives the degree correlation of the graph. It should be Tiago Peixoto committed Aug 02, 2009 75 76 77 78 79 80 81 callable with two parameters: the in,out-degree pair of the source vertex an edge, and the in,out-degree pair of the target of the same edge (for undirected graphs, both parameters are single values). The function should return a number proportional to the probability of such an edge existing in the generated graph. directed : bool (optional, default: True) Whether the generated graph should be directed. Tiago Peixoto committed Feb 20, 2010 82 parallel_edges : bool (optional, default: False) Tiago Peixoto committed Aug 02, 2009 83 84 85 If True, parallel edges are allowed. self_loops : bool (optional, default: False) If True, self-loops are allowed. Tiago Peixoto committed Feb 20, 2010 86 87 random : bool (optional, default: True) If True, the returned graph is randomized. Tiago Peixoto committed Aug 24, 2011 88 89 90 91 mix_time : int (optional, default: 10) Number of edge sweeps to perform in order to mix the graph. This value is ignored if parallel_edges == self_loops == True and strat != "probabilistic". Tiago Peixoto committed Feb 20, 2010 92 93 verbose : bool (optional, default: False) If True, verbose information is displayed. Tiago Peixoto committed Aug 02, 2009 94 95 96 Returns ------- Tiago Peixoto committed Oct 05, 2009 97 random_graph : :class:~graph_tool.Graph Tiago Peixoto committed Aug 02, 2009 98 99 100 101 102 103 104 105 The generated graph. See Also -------- random_rewire: in place graph shuffling Notes ----- Tiago Peixoto committed Feb 20, 2010 106 107 108 The algorithm makes sure the degree sequence is graphical (i.e. realizable) and keeps re-sampling the degrees if is not. With a valid degree sequence, the edges are placed deterministically, and later the graph is shuffled with Tiago Peixoto committed Aug 24, 2011 109 110 the :func:~graph_tool.generation.random_rewire function, with the mix_time parameter passed as n_iter. Tiago Peixoto committed Feb 20, 2010 111 Tiago Peixoto committed Aug 24, 2011 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 The complexity is :math:O(V + E) if parallel edges are allowed, and :math:O(V + E \times\text{mix_time}) if parallel edges are not allowed. .. note :: If parallel_edges == False this algorithm only guarantees that the returned graph will be a random sample from the desired ensemble if mix_time is sufficiently large. The algorithm implements an efficient Markov chain based on edge swaps, with a mixing time which depends on the degree distribution and correlations desired. If degree correlations are provided, the mixing time tends to be larger. If strat == "probabilistic", the Markov chain still needs to be mixed, even if parallel edges and self-loops are allowed. In this case the Markov chain is implemented using a mixture of the alias method [ripley-stochastic-1987]_ for direct sampling of target degrees, and Metropolis-Hastings [metropolis-equations-1953]_ [hastings-monte-carlo-1970]_ acceptance/rejection sampling of edge swaps. Tiago Peixoto committed Aug 02, 2009 132 Tiago Peixoto committed Feb 20, 2010 133 134 References ---------- Tiago Peixoto committed Aug 24, 2011 135 136 137 138 139 140 141 142 143 144 145 .. [deg-sequence] http://en.wikipedia.org/wiki/Degree_%28graph_theory%29#Degree_sequence .. [metropolis-equations-1953] Metropolis, N.; Rosenbluth, A.W.; Rosenbluth, M.N.; Teller, A.H.; Teller, E. "Equations of State Calculations by Fast Computing Machines". Journal of Chemical Physics 21 (6): 1087–1092 (1953). :doi:10.1063/1.1699114 .. [hastings-monte-carlo-1970] Hastings, W.K. "Monte Carlo Sampling Methods Using Markov Chains and Their Applications". Biometrika 57 (1): 97–109 (1970). :doi:10.1093/biomet/57.1.97 .. [ripley-stochastic-1987] B. D Ripley, Stochastic simulation, vol. 183 (Wiley Online Library, 1987). :doi:10.1002/9780470316726.fmatter Tiago Peixoto committed Aug 02, 2009 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 Examples -------- >>> from numpy.random import randint, random, seed, poisson >>> from pylab import * >>> seed(42) This is a degree sampler which uses rejection sampling to sample from the distribution :math:P(k)\propto 1/k, up to a maximum. >>> def sample_k(max): ... accept = False ... while not accept: ... k = randint(1,max+1) ... accept = random() < 1.0/k ... return k ... The following generates a random undirected graph with degree distribution :math:P(k)\propto 1/k (with k_max=40) and an *assortative* degree correlation of the form: .. math:: P(i,k) \propto \frac{1}{1+|i-k|} >>> g = gt.random_graph(1000, lambda: sample_k(40), Tiago Peixoto committed Aug 24, 2011 174 175 ... lambda i, k: 1.0 / (1 + abs(i - k)), directed=False, ... mix_time=100) Tiago Peixoto committed Aug 02, 2009 176 >>> gt.scalar_assortativity(g, "out") Tiago Peixoto committed Aug 24, 2011 177 (0.5149626775363764, 0.01291310404121864) Tiago Peixoto committed Aug 02, 2009 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 The following samples an in,out-degree pair from the joint distribution: .. math:: p(j,k) = \frac{1}{2}\frac{e^{-m_1}m_1^j}{j!}\frac{e^{-m_1}m_1^k}{k!} + \frac{1}{2}\frac{e^{-m_2}m_2^j}{j!}\frac{e^{-m_2}m_2^k}{k!} with :math:m_1 = 4 and :math:m_2 = 20. >>> def deg_sample(): ... if random() > 0.5: ... return poisson(4), poisson(4) ... else: ... return poisson(20), poisson(20) ... The following generates a random directed graph with this distribution, and plots the combined degree correlation. >>> g = gt.random_graph(20000, deg_sample) >>> >>> hist = gt.combined_corr_hist(g, "in", "out") >>> imshow(hist[0], interpolation="nearest") <...> >>> colorbar() <...> Tiago Peixoto committed Oct 05, 2009 205 >>> xlabel("in-degree") Tiago Peixoto committed Aug 02, 2009 206 <...> Tiago Peixoto committed Oct 05, 2009 207 >>> ylabel("out-degree") Tiago Peixoto committed Aug 02, 2009 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 <...> >>> savefig("combined-deg-hist.png") .. figure:: combined-deg-hist.png :align: center Combined degree histogram. A correlated directed graph can be build as follows. Consider the following degree correlation: .. math:: P(j',k'|j,k)=\frac{e^{-k}k^{j'}}{j'!} \frac{e^{-(20-j)}(20-j)^{k'}}{k'!} i.e., the in->out correlation is "disassortative", the out->in correlation is "assortative", and everything else is uncorrelated. We will use a flat degree distribution in the range [1,20). >>> p = scipy.stats.poisson >>> g = gt.random_graph(20000, lambda: (sample_k(19), sample_k(19)), Tiago Peixoto committed Aug 24, 2011 230 231 232 ... lambda a,b: (p.pmf(a[0], b[1]) * ... p.pmf(a[1], 20 - b[0])), ... mix_time=100) Tiago Peixoto committed Aug 02, 2009 233 234 235 Lets plot the average degree correlations to check. Tiago Peixoto committed Dec 06, 2009 236 237 238 239 >>> figure(figsize=(6,3)) <...> >>> axes([0.1,0.15,0.63,0.8]) <...> Tiago Peixoto committed Aug 02, 2009 240 >>> corr = gt.avg_neighbour_corr(g, "in", "in") Tiago Peixoto committed Jul 13, 2010 241 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 242 ... label=r"$\left<\text{in}\right>$ vs in") Tiago Peixoto committed Aug 02, 2009 243 244 (...) >>> corr = gt.avg_neighbour_corr(g, "in", "out") Tiago Peixoto committed Jul 13, 2010 245 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 246 ... label=r"$\left<\text{out}\right>$ vs in") Tiago Peixoto committed Aug 02, 2009 247 248 (...) >>> corr = gt.avg_neighbour_corr(g, "out", "in") Tiago Peixoto committed Jul 13, 2010 249 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 250 ... label=r"$\left<\text{in}\right>$ vs out") Tiago Peixoto committed Aug 02, 2009 251 252 (...) >>> corr = gt.avg_neighbour_corr(g, "out", "out") Tiago Peixoto committed Jul 13, 2010 253 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 254 ... label=r"$\left<\text{out}\right>$ vs out") Tiago Peixoto committed Aug 02, 2009 255 (...) Tiago Peixoto committed Dec 06, 2009 256 >>> legend(loc=(1.05,0.5)) Tiago Peixoto committed Aug 02, 2009 257 258 259 260 261 262 263 264 265 266 267 268 <...> >>> xlabel("source degree") <...> >>> ylabel("average target degree") <...> >>> savefig("deg-corr-dir.png") .. figure:: deg-corr-dir.png :align: center Average nearest neighbour correlations. """ Tiago Peixoto committed Aug 24, 2011 269 Tiago Peixoto committed Oct 05, 2009 270 seed = numpy.random.randint(0, sys.maxint) Tiago Peixoto committed Apr 10, 2008 271 272 273 274 275 g = Graph() if deg_corr == None: uncorrelated = True else: uncorrelated = False Tiago Peixoto committed Aug 24, 2011 276 277 278 279 280 281 282 libgraph_tool_generation.gen_graph(g._Graph__graph, N, deg_sampler, uncorrelated, not parallel_edges, not self_loops, not directed, seed, verbose, True) if parallel_edges and self_loops and strat != "probabilistic": mix_time = 1 Tiago Peixoto committed Apr 10, 2008 283 g.set_directed(directed) Tiago Peixoto committed Feb 20, 2010 284 if random: Tiago Peixoto committed Aug 24, 2011 285 286 if deg_corr is not None: random_rewire(g, strat="probabilistic", n_iter=mix_time, Tiago Peixoto committed May 03, 2010 287 288 parallel_edges=parallel_edges, deg_corr=deg_corr, self_loops=self_loops, verbose=verbose) Tiago Peixoto committed Aug 24, 2011 289 290 291 292 else: random_rewire(g, parallel_edges=parallel_edges, n_iter=mix_time, self_loops=self_loops, verbose=verbose) Tiago Peixoto committed Apr 10, 2008 293 return g Tiago Peixoto committed Aug 04, 2009 294 Tiago Peixoto committed May 03, 2010 295 Tiago Peixoto committed May 03, 2010 296 @_limit_args({"strat": ["erdos", "correlated", "uncorrelated", "probabilistic"]}) Tiago Peixoto committed Aug 24, 2011 297 298 299 def random_rewire(g, strat="uncorrelated", n_iter=1, edge_sweep=True, parallel_edges=False, self_loops=False, deg_corr=None, ret_fail=False, verbose=False): Tiago Peixoto committed Aug 07, 2009 300 r""" Tiago Peixoto committed Aug 24, 2011 301 302 303 304 305 306 307 308 Shuffle the graph in-place. If strat != "erdos", the degrees (either in or out) of each vertex are always the same, but otherwise the edges are randomly placed. If strat == "correlated", the degree correlations are also maintained: The new source and target of each edge both have the same in and out-degree. If strat == "probabilistic", then edges are rewired according to the degree correlation given by the parameter deg_corr. Tiago Peixoto committed Aug 07, 2009 309 310 311 Parameters ---------- Tiago Peixoto committed Oct 05, 2009 312 g : :class:~graph_tool.Graph Tiago Peixoto committed Aug 07, 2009 313 Graph to be shuffled. The graph will be modified. Tiago Peixoto committed Aug 24, 2011 314 315 316 317 strat : string (optional, default: "uncorrelated") If strat == "erdos", the resulting graph will be entirely random. If strat == "uncorrelated" only the degrees of the vertices will be maintained, nothing else. If strat == "correlated", additionally the Tiago Peixoto committed Dec 21, 2009 318 new source and target of each edge both have the same in and out-degree. Tiago Peixoto committed Aug 24, 2011 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 If strat == "probabilistic", than edges are rewired according to the degree correlation given by the parameter deg_corr. n_iter : int (optional, default: 1) Number of iterations. If edge_sweep == True, each iteration corresponds to an entire "sweep" over all edges. Otherwise this corresponds to the total number of edges which are randomly chosen for a swap attempt (which may repeat). edge_sweep : bool (optional, default: True) If True, each iteration will perform an entire "sweep" over the edges, where each edge is visited once in random order, and a edge swap is attempted. parallel : bool (optional, default: False) If True, parallel edges are allowed. self_loops : bool (optional, default: False) If True, self-loops are allowed. deg_corr : function (optional, default: None) Tiago Peixoto committed Feb 20, 2010 335 336 337 338 339 340 A function which gives the degree correlation of the graph. It should be callable with two parameters: the in,out-degree pair of the source vertex an edge, and the in,out-degree pair of the target of the same edge (for undirected graphs, both parameters are single values). The function should return a number proportional to the probability of such an edge existing in the generated graph. This parameter is ignored, Tiago Peixoto committed Aug 24, 2011 341 342 343 344 345 346 347 348 349 350 351 352 unless strat == "probabilistic". ret_fail : bool (optional, default: False) If True, the number of failed edge moves (due to parallel edges or self-loops) is returned. verbose : bool (optional, default: False) If True, verbose information is displayed. Returns ------- fail_count : int Number of failed edge moves (due to parallel edges or self-loops). Tiago Peixoto committed Aug 07, 2009 353 354 355 356 357 358 359 See Also -------- random_graph: random graph generation Notes ----- Tiago Peixoto committed Feb 20, 2010 360 This algorithm iterates through all the edges in the network and tries to Tiago Peixoto committed May 21, 2010 361 swap its target or source with the target or source of another edge. Tiago Peixoto committed Feb 20, 2010 362 363 .. note:: Tiago Peixoto committed Aug 07, 2009 364 Tiago Peixoto committed Aug 24, 2011 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 If parallel_edges = False, parallel edges are not placed during rewiring. In this case, the returned graph will be a uncorrelated sample from the desired ensemble only if n_iter is sufficiently large. The algorithm implements an efficient Markov chain based on edge swaps, with a mixing time which depends on the degree distribution and correlations desired. If degree probabilistic correlations are provided, the mixing time tends to be larger. If strat == "probabilistic", the Markov chain still needs to be mixed, even if parallel edges and self-loops are allowed. In this case the Markov chain is implemented using a mixture of the alias method [ripley-stochastic-1987]_ for direct sampling of target degrees, and Metropolis-Hastings [metropolis-equations-1953]_ [hastings-monte-carlo-1970]_ acceptance/rejection sampling of edge swaps. Tiago Peixoto committed Feb 20, 2010 381 Tiago Peixoto committed Aug 24, 2011 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 Each edge is tentatively swapped once per iteration, so the overall complexity is :math:O(V + E \times \text{n_iter}). If edge_sweep == False, the complexity becomes :math:O(V + E + \text{n_iter}). References ---------- .. [metropolis-equations-1953] Metropolis, N.; Rosenbluth, A.W.; Rosenbluth, M.N.; Teller, A.H.; Teller, E. "Equations of State Calculations by Fast Computing Machines". Journal of Chemical Physics 21 (6): 1087–1092 (1953). :doi:10.1063/1.1699114 .. [hastings-monte-carlo-1970] Hastings, W.K. "Monte Carlo Sampling Methods Using Markov Chains and Their Applications". Biometrika 57 (1): 97–109 (1970). :doi:10.1093/biomet/57.1.97 .. [ripley-stochastic-1987] B. D Ripley, Stochastic simulation, vol. 183 (Wiley Online Library, 1987). :doi:10.1002/9780470316726.fmatter Tiago Peixoto committed Aug 07, 2009 397 398 399 400 401 402 Examples -------- Some small graphs for visualization. Tiago Peixoto committed Dec 06, 2009 403 >>> from numpy.random import random, seed Tiago Peixoto committed Aug 07, 2009 404 405 >>> from pylab import * >>> seed(42) Tiago Peixoto committed Dec 06, 2009 406 >>> g, pos = gt.triangulation(random((1000,2))) Tiago Peixoto committed Oct 05, 2009 407 >>> gt.graph_draw(g, layout="arf", output="rewire_orig.png", size=(6,6)) Tiago Peixoto committed Sep 03, 2009 408 <...> Tiago Peixoto committed Aug 07, 2009 409 >>> gt.random_rewire(g, "correlated") Tiago Peixoto committed Oct 05, 2009 410 >>> gt.graph_draw(g, layout="arf", output="rewire_corr.png", size=(6,6)) Tiago Peixoto committed Sep 03, 2009 411 <...> Tiago Peixoto committed Aug 07, 2009 412 >>> gt.random_rewire(g) Tiago Peixoto committed Oct 05, 2009 413 >>> gt.graph_draw(g, layout="arf", output="rewire_uncorr.png", size=(6,6)) Tiago Peixoto committed Sep 03, 2009 414 <...> Tiago Peixoto committed Dec 21, 2009 415 416 417 >>> gt.random_rewire(g, "erdos") >>> gt.graph_draw(g, layout="arf", output="rewire_erdos.png", size=(6,6)) <...> Tiago Peixoto committed Aug 07, 2009 418 Tiago Peixoto committed Oct 05, 2009 419 Some ridiculograms _ : Tiago Peixoto committed Aug 07, 2009 420 Tiago Peixoto committed Oct 05, 2009 421 422 423 .. image:: rewire_orig.png .. image:: rewire_corr.png .. image:: rewire_uncorr.png Tiago Peixoto committed Dec 21, 2009 424 .. image:: rewire_erdos.png Tiago Peixoto committed Aug 07, 2009 425 Tiago Peixoto committed Aug 24, 2011 426 427 *From left to right:* Original graph --- Shuffled graph, with degree correlations --- Shuffled graph, without degree correlations --- Shuffled graph, Tiago Peixoto committed Dec 21, 2009 428 with random degrees. Tiago Peixoto committed Aug 07, 2009 429 430 431 We can try some larger graphs to get better statistics. Tiago Peixoto committed Dec 06, 2009 432 433 >>> figure() <...> Tiago Peixoto committed Dec 21, 2009 434 >>> g = gt.random_graph(30000, lambda: sample_k(20), Tiago Peixoto committed Aug 24, 2011 435 436 ... lambda i, j: exp(abs(i-j)), directed=False, ... mix_time=100) Tiago Peixoto committed Aug 07, 2009 437 >>> corr = gt.avg_neighbour_corr(g, "out", "out") Tiago Peixoto committed Jul 13, 2010 438 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label="original") Tiago Peixoto committed Aug 07, 2009 439 440 441 (...) >>> gt.random_rewire(g, "correlated") >>> corr = gt.avg_neighbour_corr(g, "out", "out") Tiago Peixoto committed Jul 13, 2010 442 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="*", label="correlated") Tiago Peixoto committed Aug 07, 2009 443 444 445 (...) >>> gt.random_rewire(g) >>> corr = gt.avg_neighbour_corr(g, "out", "out") Tiago Peixoto committed Jul 13, 2010 446 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label="uncorrelated") Tiago Peixoto committed Aug 07, 2009 447 (...) Tiago Peixoto committed Dec 21, 2009 448 449 >>> gt.random_rewire(g, "erdos") >>> corr = gt.avg_neighbour_corr(g, "out", "out") Tiago Peixoto committed Jul 13, 2010 450 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label="Erdos") Tiago Peixoto committed Dec 21, 2009 451 (...) Tiago Peixoto committed Aug 07, 2009 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 >>> xlabel("$k$") <...> >>> ylabel(r"$\left$") <...> >>> legend(loc="best") <...> >>> savefig("shuffled-stats.png") .. figure:: shuffled-stats.png :align: center Average degree correlations for the different shuffled and non-shuffled graphs. The shuffled graph with correlations displays exactly the same correlation as the original graph. Now let's do it for a directed graph. See :func:~graph_tool.generation.random_graph for more details. >>> p = scipy.stats.poisson >>> g = gt.random_graph(20000, lambda: (sample_k(19), sample_k(19)), Tiago Peixoto committed Aug 24, 2011 472 473 ... lambda a, b: (p.pmf(a[0], b[1]) * p.pmf(a[1], 20 - b[0])), ... mix_time=100) Tiago Peixoto committed Dec 06, 2009 474 475 476 477 >>> figure(figsize=(6,3)) <...> >>> axes([0.1,0.15,0.6,0.8]) <...> Tiago Peixoto committed Aug 07, 2009 478 >>> corr = gt.avg_neighbour_corr(g, "in", "out") Tiago Peixoto committed Jul 13, 2010 479 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 480 ... label=r"$\left<\text{o}\right>$ vs i") Tiago Peixoto committed Aug 07, 2009 481 482 (...) >>> corr = gt.avg_neighbour_corr(g, "out", "in") Tiago Peixoto committed Jul 13, 2010 483 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 484 ... label=r"$\left<\text{i}\right>$ vs o") Tiago Peixoto committed Aug 07, 2009 485 486 487 (...) >>> gt.random_rewire(g, "correlated") >>> corr = gt.avg_neighbour_corr(g, "in", "out") Tiago Peixoto committed Jul 13, 2010 488 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 489 ... label=r"$\left<\text{o}\right>$ vs i, corr.") Tiago Peixoto committed Aug 07, 2009 490 491 (...) >>> corr = gt.avg_neighbour_corr(g, "out", "in") Tiago Peixoto committed Jul 13, 2010 492 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 493 ... label=r"$\left<\text{i}\right>$ vs o, corr.") Tiago Peixoto committed Aug 07, 2009 494 495 496 (...) >>> gt.random_rewire(g, "uncorrelated") >>> corr = gt.avg_neighbour_corr(g, "in", "out") Tiago Peixoto committed Jul 13, 2010 497 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 498 ... label=r"$\left<\text{o}\right>$ vs i, uncorr.") Tiago Peixoto committed Aug 07, 2009 499 500 (...) >>> corr = gt.avg_neighbour_corr(g, "out", "in") Tiago Peixoto committed Jul 13, 2010 501 >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", Tiago Peixoto committed Oct 05, 2009 502 ... label=r"$\left<\text{i}\right>$ vs o, uncorr.") Tiago Peixoto committed Aug 07, 2009 503 (...) Tiago Peixoto committed Dec 06, 2009 504 >>> legend(loc=(1.05,0.45)) Tiago Peixoto committed Aug 07, 2009 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 <...> >>> xlabel("source degree") <...> >>> ylabel("average target degree") <...> >>> savefig("shuffled-deg-corr-dir.png") .. figure:: shuffled-deg-corr-dir.png :align: center Average degree correlations for the different shuffled and non-shuffled directed graphs. The shuffled graph with correlations displays exactly the same correlation as the original graph. """ Tiago Peixoto committed Oct 05, 2009 520 seed = numpy.random.randint(0, sys.maxint) Tiago Peixoto committed Aug 07, 2009 521 Tiago Peixoto committed Feb 20, 2010 522 523 524 525 526 527 528 529 530 531 532 533 534 if not parallel_edges: p = label_parallel_edges(g) if p.a.max() != 0: raise ValueError("Parallel edge detected. Can't rewire " + "graph without parallel edges if it " + "already contains parallel edges!") if not self_loops: l = label_self_loops(g) if l.a.max() != 0: raise ValueError("Self-loop detected. Can't rewire graph " + "without self-loops if it already contains" + " self-loops!") Tiago Peixoto committed Aug 24, 2011 535 if deg_corr is not None and not g.is_directed(): Tiago Peixoto committed May 03, 2010 536 corr = lambda i, j: deg_corr(i[1], j[1]) Tiago Peixoto committed Feb 20, 2010 537 538 539 else: corr = deg_corr Tiago Peixoto committed Aug 24, 2011 540 541 542 543 544 545 546 547 if strat != "probabilistic": g = GraphView(g, reversed=False) pcount = libgraph_tool_generation.random_rewire(g._Graph__graph, strat, n_iter, not edge_sweep, self_loops, parallel_edges, corr, seed, verbose) if ret_fail: return pcount Tiago Peixoto committed Aug 17, 2009 548 Tiago Peixoto committed May 03, 2010 549 Tiago Peixoto committed Aug 17, 2009 550 def predecessor_tree(g, pred_map): Tiago Peixoto committed Nov 13, 2010 551 """Return a graph from a list of predecessors given by the pred_map vertex property.""" Tiago Peixoto committed Aug 17, 2009 552 553 554 555 556 557 558 _check_prop_scalar(pred_map, "pred_map") pg = Graph() libgraph_tool_generation.predecessor_graph(g._Graph__graph, pg._Graph__graph, _prop("v", g, pred_map)) return pg Tiago Peixoto committed Aug 28, 2009 559 Tiago Peixoto committed May 03, 2010 560 Tiago Peixoto committed Aug 28, 2009 561 def line_graph(g): Tiago Peixoto committed Oct 05, 2009 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 """Return the line graph of the given graph g. Notes ----- Given an undirected graph G, its line graph L(G) is a graph such that * each vertex of L(G) represents an edge of G; and * two vertices of L(G) are adjacent if and only if their corresponding edges share a common endpoint ("are adjacent") in G. For a directed graph, the second criterion becomes: * Two vertices representing directed edges from u to v and from w to x in G are connected by an edge from uv to wx in the line digraph when v = w. References ---------- .. [line-wiki] http://en.wikipedia.org/wiki/Line_graph """ Tiago Peixoto committed Aug 28, 2009 582 583 584 585 586 587 588 589 lg = Graph(directed=g.is_directed()) vertex_map = lg.new_vertex_property("int64_t") libgraph_tool_generation.line_graph(g._Graph__graph, lg._Graph__graph, _prop("v", lg, vertex_map)) return lg, vertex_map Tiago Peixoto committed Sep 06, 2009 590 Tiago Peixoto committed May 03, 2010 591 592 def graph_union(g1, g2, props=None, include=False): Tiago Peixoto committed Oct 05, 2009 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 """Return the union of graphs g1 and g2, composed of all edges and vertices of g1 and g2, without overlap. Parameters ---------- g1 : :class:~graph_tool.Graph First graph in the union. g2 : :class:~graph_tool.Graph Second graph in the union. props : list of tuples of :class:~graph_tool.PropertyMap (optional, default: []) Each element in this list must be a tuple of two PropertyMap objects. The first element must be a property of g1, and the second of g2. The values of the property maps are propagated into the union graph, and returned. include : bool (optional, default: False) If true, graph g2 is inserted into g1 which is modified. If false, a new graph is created, and both graphs remain unmodified. Returns ------- ug : :class:~graph_tool.Graph The union graph props : list of :class:~graph_tool.PropertyMap objects List of propagated properties. This is only returned if props is not empty. Tiago Peixoto committed Dec 06, 2009 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 Examples -------- >>> from numpy.random import random, seed >>> seed(42) >>> g = gt.triangulation(random((300,2)))[0] >>> ug = gt.graph_union(g, g) >>> uug = gt.graph_union(g, ug) >>> gt.graph_draw(g, layout="arf", size=(8,8), output="graph_original.png") <...> >>> gt.graph_draw(ug, layout="arf", size=(8,8), output="graph_union.png") <...> >>> gt.graph_draw(uug, layout="arf", size=(8,8), output="graph_union2.png") <...> .. image:: graph_original.png .. image:: graph_union.png .. image:: graph_union2.png Tiago Peixoto committed Oct 05, 2009 638 """ Tiago Peixoto committed May 03, 2010 639 640 if props == None: props = [] Tiago Peixoto committed Sep 06, 2009 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 if not include: g1 = Graph(g1) g1.stash_filter(directed=True) g1.set_directed(True) g2.stash_filter(directed=True) g2.set_directed(True) n_props = [] try: vmap, emap = libgraph_tool_generation.graph_union(g1._Graph__graph, g2._Graph__graph) for p in props: p1, p2 = p if not include: p1 = g1.copy_property(p1) if p2.value_type() != p1.value_type(): p2 = g2.copy_property(p2, value_type=p1.value_type()) if p1.key_type() == 'v': libgraph_tool_generation.\ vertex_property_union(g1._Graph__graph, g2._Graph__graph, vmap, emap, _prop(p1.key_type(), g1, p1), _prop(p2.key_type(), g2, p2)) else: libgraph_tool_generation.\ edge_property_union(g1._Graph__graph, g2._Graph__graph, vmap, emap, _prop(p1.key_type(), g1, p1), _prop(p2.key_type(), g2, p2)) n_props.append(p1) finally: g1.pop_filter(directed=True) g2.pop_filter(directed=True) if len(n_props) > 0: return g1, n_props else: return g1 Tiago Peixoto committed Dec 06, 2009 679 Tiago Peixoto committed May 03, 2010 680 681 @_limit_args({"type": ["simple", "delaunay"]}) Tiago Peixoto committed Jan 11, 2010 682 def triangulation(points, type="simple", periodic=False): Tiago Peixoto committed Dec 06, 2009 683 684 685 686 687 688 689 690 691 692 r""" Generate a 2D or 3D triangulation graph from a given point set. Parameters ---------- points : :class:~numpy.ndarray Point set for the triangulation. It may be either a N x d array, where N is the number of points, and d is the space dimension (either 2 or 3). type : string (optional, default: 'simple') Type of triangulation. May be either 'simple' or 'delaunay'. Tiago Peixoto committed Jan 11, 2010 693 694 695 periodic : bool (optional, default: False) If True, periodic boundary conditions will be used. This is parameter is valid only for type="delaunay", and is otherwise ignored. Tiago Peixoto committed Dec 06, 2009 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 Returns ------- triangulation_graph : :class:~graph_tool.Graph The generated graph. pos : :class:~graph_tool.PropertyMap Vertex property map with the Cartesian coordinates. See Also -------- random_graph: random graph generation Notes ----- Tiago Peixoto committed Dec 22, 2009 711 A triangulation [cgal-triang]_ is a division of the convex hull of a point Tiago Peixoto committed Dec 22, 2009 712 set into triangles, using only that set as triangle vertices. Tiago Peixoto committed Dec 06, 2009 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 In simple triangulations (type="simple"), the insertion of a point is done by locating a face that contains the point, and splitting this face into three new faces (the order of insertion is therefore important). If the point falls outside the convex hull, the triangulation is restored by flips. Apart from the location, insertion takes a time O(1). This bound is only an amortized bound for points located outside the convex hull. Delaunay triangulations (type="delaunay") have the specific empty sphere property, that is, the circumscribing sphere of each cell of such a triangulation does not contain any other vertex of the triangulation in its interior. These triangulations are uniquely defined except in degenerate cases where five points are co-spherical. Note however that the CGAL implementation computes a unique triangulation even in these cases. Examples -------- >>> from numpy.random import seed, random >>> seed(42) Tiago Peixoto committed Oct 04, 2010 732 >>> points = random((500, 2)) * 4 Tiago Peixoto committed Dec 06, 2009 733 >>> g, pos = gt.triangulation(points) Tiago Peixoto committed Dec 22, 2009 734 735 736 737 738 739 740 741 742 >>> weight = g.new_edge_property("double") # Edge weights corresponding to ... # Euclidean distances >>> for e in g.edges(): ... weight[e] = sqrt(sum((array(pos[e.source()]) - ... array(pos[e.target()]))**2)) >>> b = gt.betweenness(g, weight=weight) >>> b[1].a *= 100 >>> gt.graph_draw(g, pos=pos, pin=True, size=(8,8), vsize=0.07, vcolor=b[0], ... eprops={"penwidth":b[1]}, output="triang.png") Tiago Peixoto committed Dec 06, 2009 743 744 <...> >>> g, pos = gt.triangulation(points, type="delaunay") Tiago Peixoto committed Dec 22, 2009 745 746 747 748 749 750 751 752 >>> weight = g.new_edge_property("double") >>> for e in g.edges(): ... weight[e] = sqrt(sum((array(pos[e.source()]) - ... array(pos[e.target()]))**2)) >>> b = gt.betweenness(g, weight=weight) >>> b[1].a *= 120 >>> gt.graph_draw(g, pos=pos, pin=True, size=(8,8), vsize=0.07, vcolor=b[0], ... eprops={"penwidth":b[1]}, output="triang-delaunay.png") Tiago Peixoto committed Dec 06, 2009 753 754 755 756 757 758 759 <...> 2D triangulation of random points: .. image:: triang.png .. image:: triang-delaunay.png Tiago Peixoto committed Dec 22, 2009 760 761 762 *Left:* Simple triangulation. *Right:* Delaunay triangulation. The vertex colors and the edge thickness correspond to the weighted betweenness centrality. Tiago Peixoto committed Dec 06, 2009 763 764 765 References ---------- Tiago Peixoto committed Dec 22, 2009 766 .. [cgal-triang] http://www.cgal.org/Manual/last/doc_html/cgal_manual/Triangulation_3/Chapter_main.html Tiago Peixoto committed Dec 06, 2009 767 768 769 """ Tiago Peixoto committed May 03, 2010 770 if points.shape[1] not in [2, 3]: Tiago Peixoto committed Dec 06, 2009 771 772 773 774 775 776 777 778 779 780 raise ValueError("points array must have shape N x d, with d either 2 or 3.") # copy points to ensure continuity and correct data type points = numpy.array(points, dtype='float64') if points.shape[1] == 2: npoints = numpy.zeros((points.shape[0], 3)) npoints[:,:2] = points points = npoints g = Graph(directed=False) pos = g.new_vertex_property("vector") libgraph_tool_generation.triangulation(g._Graph__graph, points, Tiago Peixoto committed Jan 11, 2010 781 _prop("v", g, pos), type, periodic) Tiago Peixoto committed Dec 06, 2009 782 return g, pos Tiago Peixoto committed Oct 04, 2010 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 def lattice(shape, periodic=False): r""" Generate a N-dimensional square lattice. Parameters ---------- shape : list or :class:~numpy.ndarray List of sizes in each dimension. periodic : bool (optional, default: False) If True, periodic boundary conditions will be used. Returns ------- lattice_graph : :class:~graph_tool.Graph The generated graph. See Also -------- triangulation: 2D or 3D triangulation random_graph: random graph generation Examples -------- >>> g = gt.lattice([10,10]) >>> gt.graph_draw(g, size=(8,8), output="lattice.png") <...> >>> g = gt.lattice([10,20], periodic=True) >>> gt.graph_draw(g, size=(8,8), output="lattice_periodic.png") <...> >>> g = gt.lattice([10,10,10]) >>> gt.graph_draw(g, size=(8,8), output="lattice_3d.png") <...> .. image:: lattice.png .. image:: lattice_periodic.png .. image:: lattice_3d.png *Left:* 10x10 2D lattice. *Middle:* 10x20 2D periodic lattice (torus). *Right:* 10x10x10 3D lattice. References ---------- .. [lattice] http://en.wikipedia.org/wiki/Square_lattice """ g = Graph(directed=False) libgraph_tool_generation.lattice(g._Graph__graph, shape, periodic) return g def geometric_graph(points, radius, ranges=None): r""" Generate a geometric network form a set of N-dimensional points. Parameters ---------- points : list or :class:~numpy.ndarray List of points. This must be a two-dimensional array, where the rows are coordinates in a N-dimensional space. radius : float Pairs of points with an euclidean distance lower than this parameters will be connected. ranges : list or :class:~numpy.ndarray (optional, default: None) If provided, periodic boundary conditions will be assumed, and the values of this parameter it will be used as the ranges in all dimensions. It must be a two-dimensional array, where each row will cointain the lower and upper bound of each dimension. Returns ------- geometric_graph : :class:~graph_tool.Graph The generated graph. pos : :class:~graph_tool.PropertyMap A vertex property map with the position of each vertex. Notes ----- A geometric graph [geometric-graph]_ is generated by connecting points embedded in a N-dimensional euclidean space which are at a distance equal to or smaller than a given radius. See Also -------- triangulation: 2D or 3D triangulation random_graph: random graph generation lattice : N-dimensional square lattice Examples -------- >>> from numpy.random import seed, random >>> seed(42) >>> points = random((500, 2)) * 4 >>> g, pos = gt.geometric_graph(points, 0.3) >>> gt.graph_draw(g, pos=pos, pin=True, size=(8,8), output="geometric.png") <...> >>> g, pos = gt.geometric_graph(points, 0.3, [(0,4), (0,4)]) >>> gt.graph_draw(g, size=(8,8), output="geometric_periodic.png") <...> .. image:: geometric.png .. image:: geometric_periodic.png *Left:* Geometric network with random points. *Right:* Same network, but with periodic boundary conditions. References ---------- .. [geometric-graph] Jesper Dall and Michael Christensen, "Random geometric Tiago Peixoto committed Dec 21, 2010 894 graphs", Phys. Rev. E 66, 016121 (2002), :doi:10.1103/PhysRevE.66.016121 Tiago Peixoto committed Oct 04, 2010 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 """ g = Graph(directed=False) pos = g.new_vertex_property("vector") if type(points) != numpy.ndarray: points = numpy.array(points) if len(points.shape) < 2: raise ValueError("points list must be a two-dimensional array!") if ranges is not None: periodic = True if type(ranges) != numpy.ndarray: ranges = numpy.array(ranges, dtype="float") else: ranges = array(ranges, dtype="float") else: periodic = False ranges = () libgraph_tool_generation.geometric(g._Graph__graph, points, float(radius), ranges, periodic, _prop("v", g, pos)) return g, pos Tiago Peixoto committed Nov 13, 2010 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 def price_network(N, m=1, c=None, gamma=1, directed=True, seed_graph=None): r"""A generalized version of Price's -- or Barabási-Albert if undirected -- preferential attachment network model. Parameters ---------- N : int Size of the network. m : int (optional, default: 1) Out-degree of newly added vertices. c : float (optional, default: 1 if directed==True else 0) Constant factor added to the probability of a vertex receiving an edge (see notes below). gamma : float (optional, default: 1) Preferential attachment power (see notes below). directed : bool (optional, default: True) If True, a Price network is generated. If False, a Barabási-Albert network is generated. seed_graph : :class:~graph_tool.Graph (optional, default: None) If provided, this graph will be used as the starting point of the algorithm. Returns ------- price_graph : :class:~graph_tool.Graph The generated graph. Notes ----- The (generalized) [price]_ network is either a directed or undirected graph (the latter is called a Barabási-Albert network), generated dynamically by at each step adding a new vertex, and connecting it to :math:m other Tiago Peixoto committed May 06, 2011 952 vertices, chosen with probability :math:\pi defined as: Tiago Peixoto committed Nov 13, 2010 953 954 955 .. math:: Tiago Peixoto committed May 06, 2011 956 \pi \propto k^\gamma + c Tiago Peixoto committed Nov 13, 2010 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 where :math:k is the in-degree of the vertex (or simply the degree in the undirected case). If :math:\gamma=1, the tail of resulting in-degree distribution of the directed case is given by .. math:: P_{k_\text{in}} \sim k_\text{in}^{-(2 + c/m)}, or for the undirected case .. math:: P_{k} \sim k^{-(3 + c/m)}. However, if :math:\gamma \ne 1, the in-degree distribution is not scale-free (see [dorogovtsev-evolution]_ for details). Tiago Peixoto committed May 06, 2011 975 976 977 978 979 980 981 Note that if seed_graph is not given, the algorithm will *always* start with one node if :math:c > 0, or with two nodes with a link between them otherwise. If :math:m > 1, the degree of the newly added vertices will be vary dynamically as :math:m'(t) = \min(m, N(t)), where :math:N(t) is the number of vertices added so far. If this behaviour is undesired, a proper seed graph with :math:N \ge m vertices must be provided. Tiago Peixoto committed Nov 13, 2010 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 This algorithm runs in :math:O(N\log N) time. See Also -------- triangulation: 2D or 3D triangulation random_graph: random graph generation lattice : N-dimensional square lattice geometric_graph : N-dimensional geometric network Examples -------- >>> from numpy.random import seed, random >>> seed(42) >>> g = gt.price_network(100000) >>> gt.graph_draw(g, layout="sfdp", size=(12,12), vcolor=g.vertex_index, ... output="price-network.png") <...> >>> g = gt.price_network(100000, c=0.1) >>> gt.graph_draw(g, layout="sfdp", size=(12,12), vcolor=g.vertex_index, ... output="price-network-broader.png") <...> .. image:: price-network.png .. image:: price-network-broader.png Price networks with :math:N=10^5 nodes. **Left:** :math:c=1, **Right:** :math:c=0.1. The colors represent the order in which vertices were added. References ---------- .. [yule] Yule, G. U. "A Mathematical Theory of Evolution, based on the Conclusions of Dr. J. C. Willis, F.R.S.". Philosophical Transactions of the Royal Society of London, Ser. B 213: 21–87, 1925, Tiago Peixoto committed Dec 21, 2010 1017 :doi:10.1098/rstb.1925.0002 Tiago Peixoto committed Nov 13, 2010 1018 1019 1020 .. [price] Derek De Solla Price, "A general theory of bibliometric and other cumulative advantage processes", Journal of the American Society for Information Science, Volume 27, Issue 5, pages 292–306, September 1976, Tiago Peixoto committed Dec 21, 2010 1021 :doi:10.1002/asi.4630270505 Tiago Peixoto committed Nov 13, 2010 1022 .. [barabasi-albert] Barabási, A.-L., and Albert, R., "Emergence of Tiago Peixoto committed Dec 21, 2010 1023 1024 scaling in random networks", Science, 286, 509, 1999, :doi:10.1126/science.286.5439.509 Tiago Peixoto committed Nov 13, 2010 1025 1026 .. [dorogovtsev-evolution] S. N. Dorogovtsev and J. F. F. Mendes, "Evolution of networks", Advances in Physics, 2002, Vol. 51, No. 4, 1079-1187, Tiago Peixoto committed Dec 21, 2010 1027 :doi:10.1080/00018730110112519 Tiago Peixoto committed Nov 13, 2010 1028 1029 1030 1031 1032 1033 """ if c is None: c = 1 if directed else 0 if seed_graph is None: Tiago Peixoto committed May 06, 2011 1034 1035 1036 g = Graph(directed=directed) if c > 0: g.add_vertex() Tiago Peixoto committed Nov 13, 2010 1037 else: Tiago Peixoto committed May 06, 2011 1038 1039 g.add_vertex(2) g.add_edge(g.vertex(1), g.vertex(0)) Tiago Peixoto committed Nov 13, 2010 1040 1041 1042 1043 1044 1045 N -= g.num_vertices() else: g = seed_graph seed = numpy.random.randint(0, sys.maxint) libgraph_tool_generation.price(g._Graph__graph, N, gamma, c, m, seed) return g