graph_bfs.cc 5.54 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) 2006-2015 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// 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/>.

#include "graph_filtering.hh"
#include "graph_python_interface.hh"

#include <boost/python.hpp>
#include <boost/graph/breadth_first_search.hpp>
23
#include <boost/coroutine/all.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
24
25
26
27
28
29
30
31
32
33
34
35

#include "graph.hh"
#include "graph_selectors.hh"
#include "graph_util.hh"

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

class BFSVisitorWrapper
{
public:
36
    BFSVisitorWrapper(GraphInterface& gi, python::object vis)
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
39
        : _gi(gi), _vis(vis) {}

    template <class Vertex, class Graph>
40
    void initialize_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
41
    {
42
43
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("initialize_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
44
45
46
    }

    template <class Vertex, class Graph>
47
    void discover_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
48
    {
49
50
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("discover_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
51
52
53
    }

    template <class Vertex, class Graph>
54
    void examine_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
55
    {
56
57
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("examine_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
58
59
60
    }

    template <class Edge, class Graph>
61
    void examine_edge(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
62
    {
63
64
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("examine_edge")(PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
    }

    template <class Edge, class Graph>
68
    void tree_edge(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
69
    {
70
71
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("tree_edge")(PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
72
73
74
    }

    template <class Edge, class Graph>
75
    void non_tree_edge(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
76
    {
77
78
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("non_tree_edge")(PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
79
80
81
    }

    template <class Edge, class Graph>
82
    void gray_target(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
83
    {
84
85
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("gray_target")(PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
86
87
88
    }

    template <class Edge, class Graph>
89
    void black_target(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
90
    {
91
92
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("black_target")(PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
93
94
95
    }

    template <class Vertex, class Graph>
96
    void finish_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
97
    {
98
99
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _vis.attr("finish_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
100
101
102
    }

private:
103
104
    GraphInterface& _gi;
    boost::python::object _vis;
Tiago Peixoto's avatar
Tiago Peixoto committed
105
106
107
108
};

struct do_bfs
{
109
110
    template <class Graph, class Visitor>
    void operator()(Graph& g, size_t s, Visitor vis) const
Tiago Peixoto's avatar
Tiago Peixoto committed
111
112
113
114
115
    {
        breadth_first_search(g, vertex(s, g), visitor(vis));
    }
};

116
void bfs_search(GraphInterface& g, size_t s, python::object vis)
Tiago Peixoto's avatar
Tiago Peixoto committed
117
118
{
    run_action<graph_tool::detail::all_graph_views,mpl::true_>()
Tiago Peixoto's avatar
Tiago Peixoto committed
119
        (g, std::bind(do_bfs(), placeholders::_1, s,
120
                      BFSVisitorWrapper(g, vis)))();
Tiago Peixoto's avatar
Tiago Peixoto committed
121
122
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
174
175
typedef boost::coroutines::asymmetric_coroutine<boost::python::object> coro_t;

class BFSGeneratorVisitor : public bfs_visitor<>
{
public:
    BFSGeneratorVisitor(GraphInterface& gi,
                        coro_t::push_type& yield)
        : _gi(gi), _yield(yield) {}

    template <class Edge, class Graph>
    void tree_edge(const Edge& e, Graph& g)
    {
        std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(_gi, g);
        _yield(boost::python::object(PythonEdge<Graph>(gp, e)));
    }

private:
    GraphInterface& _gi;
    coro_t::push_type& _yield;
};

class BFSGenerator
{
public:
    template <class Dispatch>
    BFSGenerator(Dispatch& dispatch)
        : _coro(std::make_shared<coro_t::pull_type>(dispatch)),
          _iter(begin(*_coro)), _end(end(*_coro)) {}
    boost::python::object next()
    {
        if (_iter == _end)
            boost::python::objects::stop_iteration_error();
        boost::python::object oe = *_iter;
        ++_iter;
        return oe;
    }
private:
    std::shared_ptr<coro_t::pull_type> _coro;
    coro_t::pull_type::iterator _iter;
    coro_t::pull_type::iterator _end;
};

boost::python::object bfs_search_generator(GraphInterface& g, size_t s)
{
    auto dispatch = [&](auto& yield)
        {
            BFSGeneratorVisitor vis(g, yield);
            run_action<graph_tool::detail::all_graph_views,mpl::true_>()
                (g, std::bind(do_bfs(), placeholders::_1, s, vis))();
        };
    return boost::python::object(BFSGenerator(dispatch));
}

Tiago Peixoto's avatar
Tiago Peixoto committed
176
177
178
179
void export_bfs()
{
    using namespace boost::python;
    def("bfs_search", &bfs_search);
180
181
182
183
184
    def("bfs_search_generator", &bfs_search_generator);
    class_<BFSGenerator>("BFSGenerator", no_init)
        .def("__iter__", objects::identity_function())
        .def("next", &BFSGenerator::next)
        .def("__next__", &BFSGenerator::next);
Tiago Peixoto's avatar
Tiago Peixoto committed
185
}