__init__.py 1.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/env python
# graph_tool.py -- a general graph manipulation python module
#
# Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
#
# 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 <http://www.gnu.org/licenses/>.

19
20
21
22
23
"""
``graph_tool.generation`` - Random Graph Generation
---------------------------------------------------
"""

Tiago Peixoto's avatar
Tiago Peixoto committed
24
25
from .. dl_import import dl_import
dl_import("import libgraph_tool_generation")
26
27

from .. core import Graph
Tiago Peixoto's avatar
Tiago Peixoto committed
28
import sys, numpy
29
30
31

__all__ = ["random_graph"]

32
33
34
def _corr_wrap(i, j, corr):
    return corr(i[1], j[1])

35
36
37
def random_graph(N, deg_sampler, deg_corr=None, directed=True,
                 parallel=False, self_loops=False,
                 seed=0, verbose=False):
Tiago Peixoto's avatar
Tiago Peixoto committed
38
39
    if seed == 0:
        seed = numpy.random.randint(0, sys.maxint)
40
41
42
43
44
    g = Graph()
    if deg_corr == None:
        uncorrelated = True
    else:
        uncorrelated = False
45
46
47
48
    if not directed and deg_corr != None:
        corr = lambda i,j: _corr_wrap(i, j, deg_corr)
    else:
        corr = deg_corr
49
    libgraph_tool_generation.gen_random_graph(g._Graph__graph, N,
50
                                              deg_sampler, corr,
51
52
53
54
55
                                              uncorrelated, not parallel,
                                              not self_loops, not directed,
                                              seed, verbose)
    g.set_directed(directed)
    return g