graph_properties_imp2.cc 6.22 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
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
36
37
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2015 Tiago de Paula Peixoto <tiago@skewed.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/>.

#include "graph_python_interface.hh"
#include "graph.hh"
#include "graph_properties.hh"
#include "graph_filtering.hh"
#include "graph_selectors.hh"
#include "graph_util.hh"

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

template <class Val1, class Val2>
void operator+=(std::vector<Val1>& v1, const std::vector<Val2>& v2)
{
    if (v2.size() > v1.size())
        v1.resize(v2.size());
    for (size_t i = 0; i < v2.size(); ++i)
        v1[i] += v2[i];
}

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
template <class Val1, class Val2>
void operator*=(std::vector<Val1>& v1, const std::vector<Val2>& v2)
{
    if (v2.size() > v1.size())
        v1.resize(v2.size());
    for (size_t i = 0; i < v2.size(); ++i)
        v1[i] *= v2[i];
}

void operator*=(std::string& v1, const string& v2)
{
    throw GraphException("Cannot multiply strings.");
}

template <class Val1, class Val2>
bool operator<(const std::vector<Val1>& v1, const std::vector<Val2>& v2)
{
    if (v1.size() != v2.size())
        return v1.size() < v2.size();
    for (size_t i = 0; i < v2.size(); ++i)
    {
        if (v1[i] < v2[i])
            return true;
    }
    return false;
}

struct SumOp
{
    template <class Graph, class Vertex, class EProp, class VProp>
    void operator()(Vertex v, EProp& eprop, VProp& vprop, Graph& g) const
    {
        typedef typename property_traits<EProp>::value_type eval_t;
        typedef typename property_traits<VProp>::value_type vval_t;

        convert<vval_t, eval_t> conv;
        size_t count = 0;
        for (auto e : out_edges_range(v, g))
        {
            if (count == 0)
                vprop[v] = conv(eprop[e]);
            else
                vprop[v] += conv(eprop[e]);
            ++count;
        }
    }
};

struct ProdOp
{
    template <class Graph, class Vertex, class EProp, class VProp>
    void operator()(Vertex v, EProp& eprop, VProp& vprop, Graph& g) const
    {
        typedef typename property_traits<EProp>::value_type eval_t;
        typedef typename property_traits<VProp>::value_type vval_t;

        convert<vval_t, eval_t> conv;
        size_t count = 0;
        for (auto e : out_edges_range(v, g))
        {
            if (count == 0)
                vprop[v] = conv(eprop[e]);
            else
                vprop[v] *= conv(eprop[e]);
            ++count;
        }
    }
};


struct MinOp
Tiago Peixoto's avatar
Tiago Peixoto committed
109
{
110
111
    template <class Graph, class Vertex, class EProp, class VProp>
    void operator()(Vertex v, EProp& eprop, VProp& vprop, Graph& g) const
Tiago Peixoto's avatar
Tiago Peixoto committed
112
    {
113
114
115
116
        typedef typename property_traits<EProp>::value_type eval_t;
        typedef typename property_traits<VProp>::value_type vval_t;

        convert<vval_t, eval_t> conv;
Tiago Peixoto's avatar
Tiago Peixoto committed
117

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        for (auto e : out_edges_range(v, g))
        {
            vprop[v] = conv(eprop[e]);
            break;
        }

        for (auto e : out_edges_range(v, g))
            vprop[v] = std::min(vprop[v], conv(eprop[e]));
    }
};

struct MaxOp
{
    template <class Graph, class Vertex, class EProp, class VProp>
    void operator()(Vertex v, EProp& eprop, VProp& vprop, Graph& g) const
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
134
135
136
137
138
        typedef typename property_traits<EProp>::value_type eval_t;
        typedef typename property_traits<VProp>::value_type vval_t;

        convert<vval_t, eval_t> conv;

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
        for (auto e : out_edges_range(v, g))
        {
            vprop[v] = conv(eprop[e]);
            break;
        }

        for (auto e : out_edges_range(v, g))
            vprop[v] = std::max(vprop[v], conv(eprop[e]));
    }
};

struct do_out_edges_op
{
    template <class Graph, class EProp, class OP>
    void operator()(Graph& g, EProp eprop, boost::any avprop, OP op) const
    {
        typedef typename property_traits<EProp>::value_type eval_t;
        typedef typename property_map_type::apply<
            typename mpl::if_<std::is_same<eval_t, size_t>,
                              int64_t, eval_t>::type,
            GraphInterface::vertex_index_map_t>::type VProp;
        typedef typename property_traits<VProp>::value_type vval_t;

        auto vprop = boost::any_cast<VProp>(avprop).get_unchecked(num_vertices(g));

Tiago Peixoto's avatar
Tiago Peixoto committed
164
165
166
167
168
169
170
171
        int i, N = num_vertices(g);
        #pragma omp parallel for default(shared) private(i)     \
            schedule(runtime) if (N > 100)
        for (i = 0; i < N; ++i)
        {
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
172
            op(v, eprop, vprop, g);
Tiago Peixoto's avatar
Tiago Peixoto committed
173
174
175
176
        }
    }
};

177
178
void out_edges_op(GraphInterface& gi, boost::any eprop, boost::any vprop,
                  std::string op)
Tiago Peixoto's avatar
Tiago Peixoto committed
179
{
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    if (op == "sum")
    {
        run_action<>()(gi, std::bind(do_out_edges_op(), placeholders::_1,
                                     placeholders::_2, vprop, SumOp()),
                       edge_properties())
            (eprop);
    }
    else if (op == "prod")
    {
        run_action<>()(gi, std::bind(do_out_edges_op(), placeholders::_1,
                                     placeholders::_2, vprop, ProdOp()),
                       edge_properties())
            (eprop);
    }
    else if (op == "min")
    {
        run_action<>()(gi, std::bind(do_out_edges_op(), placeholders::_1,
                                     placeholders::_2, vprop, MinOp()),
                       edge_properties())
            (eprop);
    }
    else if (op == "max")
    {
        run_action<>()(gi, std::bind(do_out_edges_op(), placeholders::_1,
                                     placeholders::_2, vprop, MaxOp()),
                       edge_properties())
            (eprop);
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
208
}