graph_bellman_ford.cc 5.04 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-2016 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
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// 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/bellman_ford_shortest_paths.hpp>

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

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


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

    template <class Edge, class Graph>
40
    void examine_edge(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
41
    {
42
        auto gp = retrieve_graph_view<Graph>(_gi, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
43
        _vis.attr("examine_edge")
44
            (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
45
46
47
    }

    template <class Edge, class Graph>
48
    void edge_relaxed(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
49
    {
50
        auto gp = retrieve_graph_view<Graph>(_gi, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
51
        _vis.attr("edge_relaxed")
52
            (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
53
54
55
    }

    template <class Edge, class Graph>
56
    void edge_not_relaxed(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
57
    {
58
        auto gp = retrieve_graph_view<Graph>(_gi, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
59
        _vis.attr("edge_not_relaxed")
60
            (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
61
62
63
    }

    template <class Edge, class Graph>
64
    void edge_minimized(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
65
    {
66
        auto gp = retrieve_graph_view<Graph>(_gi, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
67
        _vis.attr("edge_minimized")
68
            (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
69
70
71
    }

    template <class Edge, class Graph>
72
    void edge_not_minimized(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
73
    {
74
        auto gp = retrieve_graph_view<Graph>(_gi, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
75
        _vis.attr("edge_not_minimized")
76
            (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
77
78
79
    }

private:
80
81
    GraphInterface& _gi;
    python::object _vis;
Tiago Peixoto's avatar
Tiago Peixoto committed
82
83
84
85
86
87
};


class BFCmp
{
public:
88
    BFCmp() {}
Tiago Peixoto's avatar
Tiago Peixoto committed
89
90
91
92
93
    BFCmp(python::object cmp): _cmp(cmp) {}

    template <class Value1, class Value2>
    bool operator()(const Value1& v1, const Value2& v2) const
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
94
        return python::extract<bool>(_cmp(v1, v2));
Tiago Peixoto's avatar
Tiago Peixoto committed
95
96
97
98
99
100
101
102
103
    }

private:
    python::object _cmp;
};

class BFCmb
{
public:
104
    BFCmb() {}
Tiago Peixoto's avatar
Tiago Peixoto committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    BFCmb(python::object cmb): _cmb(cmb) {}

    template <class Value1, class Value2 >
    Value1 operator()(const Value1& v1, const Value2& v2) const
    {
        return python::extract<Value1>(_cmb(v1, v2));
    }

private:
    python::object _cmb;
};

struct do_bf_search
{
119
    template <class Graph, class DistanceMap>
120
121
122
123
    void operator()(Graph& g, size_t s, DistanceMap dist, boost::any pred_map,
                    boost::any aweight, BFVisitorWrapper vis, pair<BFCmp,
                    BFCmb> cm, pair<python::object, python::object> range,
                    bool& ret) const
Tiago Peixoto's avatar
Tiago Peixoto committed
124
125
126
127
128
129
    {
        typedef typename property_traits<DistanceMap>::value_type dtype_t;
        dtype_t z = python::extract<dtype_t>(range.first);
        dtype_t i = python::extract<dtype_t>(range.second);

        typedef typename property_map_type::
130
            apply<int64_t, decltype(get(vertex_index, g))>::type pred_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
131
        pred_t pred = any_cast<pred_t>(pred_map);
132
133
134
        typedef typename graph_traits<Graph>::edge_descriptor edge_t;
        DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
                                                       edge_properties());
Tiago Peixoto's avatar
Tiago Peixoto committed
135
136
137
138
139
140
141
142
143
144
145
146
        ret = bellman_ford_shortest_paths
            (g, HardNumVertices()(g),
             root_vertex(vertex(s, g)).visitor(vis).weight_map(weight).
             distance_map(dist).
             predecessor_map(pred).
             distance_compare(cm.first).
             distance_combine(cm.second).distance_inf(i).
             distance_zero(z));
    }
};


147
148
149
150
151
bool bellman_ford_search(GraphInterface& g, size_t source, boost::any dist_map,
                         boost::any pred_map, boost::any weight,
                         python::object vis, python::object cmp,
                         python::object cmb, python::object zero,
                         python::object inf)
Tiago Peixoto's avatar
Tiago Peixoto committed
152
153
{
    bool ret = false;
154
    run_action<graph_tool::all_graph_views,mpl::true_>()
155
156
        (g, std::bind(do_bf_search(),  std::placeholders::_1, source,
                      std::placeholders::_2, pred_map, weight,
157
                      BFVisitorWrapper(g, vis),
Tiago Peixoto's avatar
Tiago Peixoto committed
158
159
                      make_pair(BFCmp(cmp), BFCmb(cmb)), make_pair(zero, inf),
                      std::ref(ret)),
160
161
         writable_vertex_properties())
        (dist_map);
Tiago Peixoto's avatar
Tiago Peixoto committed
162
163
164
165
166
167
168
169
    return ret;
}

void export_bellman_ford()
{
    using namespace boost::python;
    def("bellman_ford_search", &bellman_ford_search);
}