diff --git a/src/python/basic_graph.cpp b/src/python/basic_graph.cpp index f0a97ed8..0934e92d 100644 --- a/src/python/basic_graph.cpp +++ b/src/python/basic_graph.cpp @@ -126,6 +126,25 @@ basic_graph::basic_graph(const std::string& filename, } } +template +basic_graph::basic_graph(erdos_renyi er, int seed) + : stored_minstd_rand(seed), + inherited(er_iterator(this->gen, er.n, er.p), er_iterator(), er.n) { } + +template +basic_graph::basic_graph(power_law_out_degree plod, int seed) + : stored_minstd_rand(seed), + inherited(sf_iterator(this->gen, plod.n, plod.alpha, plod.beta), + sf_iterator(), + plod.n) +{ } + +template +basic_graph::basic_graph(small_world sw, int seed) + : stored_minstd_rand(seed), + inherited(sw_iterator(this->gen, sw.n, sw.k, sw.p), sw_iterator(), sw.n) +{ } + // ---------------------------------------------------------- // Incidence basic_graph concept // ---------------------------------------------------------- @@ -481,6 +500,7 @@ void export_basic_graph(const char* name) using boost::python::range; using boost::python::scope; using boost::python::self; + using boost::python::arg; typedef basic_graph Graph; typedef typename Graph::Vertex Vertex; @@ -493,6 +513,12 @@ void export_basic_graph(const char* name) class_ >(name) // Constructors .def(init()) + .def(init( + (arg("generator"), arg("seed") = 1))) + .def(init( + (arg("generator"), arg("seed") = 1))) + .def(init( + (arg("generator"), arg("seed") = 1))) .def("is_directed", &Graph::is_directed) // Vertex List Graph concept .def("num_vertices", &Graph::num_vertices) diff --git a/src/python/basic_graph.hpp b/src/python/basic_graph.hpp index d1428757..f2b58969 100644 --- a/src/python/basic_graph.hpp +++ b/src/python/basic_graph.hpp @@ -14,8 +14,13 @@ #include #include #include +#include +#include +#include +#include #include #include "point2d.hpp" +#include "generators.hpp" namespace boost { namespace graph { namespace python { @@ -157,9 +162,17 @@ get(const basic_index_map& pm, enum graph_file_kind { gfk_adjlist, gfk_graphviz }; +struct stored_minstd_rand +{ + stored_minstd_rand(int seed = 1) : gen(seed) { } + + minstd_rand gen; +}; + template class basic_graph - : public adjacency_list, property > { @@ -180,6 +193,13 @@ class basic_graph typedef typename traits::vertex_descriptor base_vertex_descriptor; typedef typename traits::edge_descriptor base_edge_descriptor; + typedef erdos_renyi_iterator > + er_iterator; + typedef plod_iterator > + sf_iterator; + typedef small_world_iterator > + sw_iterator; + public: typedef basic_descriptor Vertex; @@ -209,6 +229,9 @@ class basic_graph basic_graph(); basic_graph(const std::string& filename, graph_file_kind kind); + basic_graph(erdos_renyi, int seed); + basic_graph(power_law_out_degree, int seed); + basic_graph(small_world, int seed); bool is_directed() const { return is_convertible::value; } diff --git a/src/python/generators.hpp b/src/python/generators.hpp new file mode 100644 index 00000000..02f8e263 --- /dev/null +++ b/src/python/generators.hpp @@ -0,0 +1,45 @@ +// Copyright 2004-5 The Trustees of Indiana University. + +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Authors: Douglas Gregor +// Andrew Lumsdaine +#ifndef BOOST_GRAPH_PYTHON_GENERATORS_HPP +#define BOOST_GRAPH_PYTHON_GENERATORS_HPP + +#include + +namespace boost { namespace graph { namespace python { + +struct erdos_renyi +{ + erdos_renyi(std::size_t n, double p) : n(n), p(p) { } + + std::size_t n; + double p; +}; + +struct power_law_out_degree +{ + power_law_out_degree(std::size_t n, double alpha, double beta) + : n(n), alpha(alpha), beta(beta) { } + + std::size_t n; + double alpha; + double beta; +}; + +struct small_world +{ + small_world(std::size_t n, std::size_t k, double p) : n(n), k(k), p(p) { } + + std::size_t n; + std::size_t k; + double p; +}; + +} } } // end namespace boost::graph::python + +#endif // BOOST_GRAPH_PYTHON_GENERATORS_HPP diff --git a/src/python/module.cpp b/src/python/module.cpp index 7301f4e0..02615d65 100644 --- a/src/python/module.cpp +++ b/src/python/module.cpp @@ -11,6 +11,7 @@ #include "digraph.hpp" #include #include "point2d.hpp" +#include "generators.hpp" namespace boost { namespace graph { namespace python { @@ -61,6 +62,9 @@ BOOST_PYTHON_MODULE(bgl) { using boost::python::class_; using boost::python::enum_; + using boost::python::no_init; + using boost::python::init; + using boost::python::arg; enum_("file_kind") .value("adjlist", gfk_adjlist) @@ -78,6 +82,21 @@ BOOST_PYTHON_MODULE(bgl) .def_readwrite("y", &point2d::y) ; + class_("ErdosRenyi", no_init) + .def(init( + (arg("n"), arg("probability") = 1))) + ; + + class_("PowerLawOutDegree", no_init) + .def(init( + (arg("n"), arg("alpha"), arg("beta")))) + ; + + class_("SmallWorld", no_init) + .def(init + ((arg("n"), arg("k"), arg("probability")))) + ; + export_Graph(); export_Digraph(); export_betweenness_centrality();