graph_generation.cc 4.33 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007-2011 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// 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
16
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

Tiago Peixoto's avatar
Tiago Peixoto committed
18
#include "graph.hh"
19
20
21
22
#include "graph_util.hh"
#include "graph_filtering.hh"
#include "graph_generation.hh"
#include <boost/python.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
23
24
25
26
27

using namespace std;
using namespace boost;
using namespace graph_tool;

28
typedef tr1::mt19937 rng_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
29

30
class PythonFuncWrap
Tiago Peixoto's avatar
Tiago Peixoto committed
31
{
32
public:
33
    PythonFuncWrap(python::object o): _o(o) {}
34

35
    pair<size_t, size_t> operator()() const
Tiago Peixoto's avatar
Tiago Peixoto committed
36
    {
37
38
        python::object ret = _o();
        return python::extract<pair<size_t,size_t> >(ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
    }

41
    size_t operator()(bool) const
Tiago Peixoto's avatar
Tiago Peixoto committed
42
    {
43
44
        python::object ret = _o();
        return python::extract<size_t>(ret);
45
46
    }

47
private:
48
49
50
    python::object _o;
};

51
52
53
54
void generate_random_graph(GraphInterface& gi, size_t N,
                           python::object deg_sample,
                           bool uncorrelated, bool no_parallel,
                           bool no_self_loops, bool undirected,
55
                           size_t seed, bool verbose, bool verify)
Tiago Peixoto's avatar
Tiago Peixoto committed
56
{
57
58
59
60
61
62
63
64
65
66
67
    typedef graph_tool::detail::get_all_graph_views::apply<
    graph_tool::detail::scalar_pairs, mpl::bool_<false>,
        mpl::bool_<false>, mpl::bool_<false>,
        mpl::bool_<true>, mpl::bool_<true> >::type graph_views;

    if (undirected)
        gi.SetDirected(false);

    if (uncorrelated)
    {
        run_action<graph_views>()
Tiago Peixoto's avatar
Tiago Peixoto committed
68
            (gi, bind<void>(gen_random_graph(), _1, N,
69
70
                            PythonFuncWrap(deg_sample),
                            no_parallel, no_self_loops,
71
                            seed, verbose, verify))();
72
73
74
75
    }
    else
    {
        run_action<graph_views>()
Tiago Peixoto's avatar
Tiago Peixoto committed
76
            (gi, bind<void>(gen_random_graph(), _1, N,
77
78
                            PythonFuncWrap(deg_sample),
                            no_parallel, no_self_loops,
79
                            seed, verbose, verify))();
80
81
82
    }
    gi.ReIndexEdges();
}
83

84
void random_rewire(GraphInterface& gi, string strat, bool self_loops,
Tiago Peixoto's avatar
Tiago Peixoto committed
85
86
                   bool parallel_edges, python::object corr_prob, size_t seed,
                   bool verbose);
Tiago Peixoto's avatar
Tiago Peixoto committed
87
88
void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
                       boost::any pred_map);
89
90
void line_graph(GraphInterface& gi, GraphInterface& lgi,
                boost::any edge_index);
Tiago Peixoto's avatar
Tiago Peixoto committed
91
92
93
94
95
96
97
python::tuple graph_union(GraphInterface& ugi, GraphInterface& gi);
void vertex_property_union(GraphInterface& ugi, GraphInterface& gi,
                           boost::any p_vprop, boost::any p_eprop,
                           boost::any uprop, boost::any prop);
void edge_property_union(GraphInterface& ugi, GraphInterface& gi,
                         boost::any p_vprop, boost::any p_eprop,
                         boost::any uprop, boost::any prop);
98
void triangulation(GraphInterface& gi, python::object points, boost::any pos,
99
                   string type, bool periodic);
100
101
102
void lattice(GraphInterface& gi, python::object oshape, bool periodic);
void geometric(GraphInterface& gi, python::object opoints, double r,
               python::object orange, bool periodic, boost::any pos);
103
104
void price(GraphInterface& gi, size_t N, double gamma, double c, size_t m,
           size_t seed);
105
106
107

using namespace boost::python;

108
109
110
BOOST_PYTHON_MODULE(libgraph_tool_generation)
{
    def("gen_random_graph", &generate_random_graph);
111
    def("random_rewire", &random_rewire);
Tiago Peixoto's avatar
Tiago Peixoto committed
112
    def("predecessor_graph", &predecessor_graph);
113
    def("line_graph", &line_graph);
Tiago Peixoto's avatar
Tiago Peixoto committed
114
115
116
    def("graph_union", &graph_union);
    def("vertex_property_union", &vertex_property_union);
    def("edge_property_union", &edge_property_union);
117
    def("triangulation", &triangulation);
118
119
    def("lattice", &lattice);
    def("geometric", &geometric);
120
    def("price", &price);
Tiago Peixoto's avatar
Tiago Peixoto committed
121
}