graph_laplacian.hh 9.99 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2020 Tiago de Paula Peixoto <tiago@skewed.de>
4
//
5
6
7
8
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
9
//
10
11
12
13
// 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 Lesser General Public License for more
// details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
16
17
18
19
20
21
22
23
24
25
26
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_LAPLACIAN_HH
#define GRAPH_LAPLACIAN_HH

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"

namespace graph_tool
{
Tiago Peixoto's avatar
Tiago Peixoto committed
27
using namespace boost;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

enum deg_t
{
    IN_DEG,
    OUT_DEG,
    TOTAL_DEG
};

template <class Graph, class Weight, class EdgeSelector>
typename property_traits<Weight>::value_type
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
           Weight w, EdgeSelector)
{
    typename property_traits<Weight>::value_type sum = 0;
    typename EdgeSelector::type e, e_end;
    for(tie(e, e_end) = EdgeSelector::get_edges(v, g); e != e_end; ++e)
        sum += get(w, *e);
    return sum;
}

48
template <class Graph, class EdgeSelector, class Val>
49
50
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
51
           UnityPropertyMap<Val, GraphInterface::edge_t>, all_edges_iteratorS<Graph>)
52
53
54
55
{
    return total_degreeS()(v, g);
}

56
template <class Graph, class EdgeSelector, class Val>
57
58
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
59
           UnityPropertyMap<Val, GraphInterface::edge_t>, in_edge_iteratorS<Graph>)
60
61
62
63
{
    return in_degreeS()(v, g);
}

64
template <class Graph, class EdgeSelector, class Val>
65
66
double
sum_degree(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
67
           UnityPropertyMap<Val, GraphInterface::edge_t>, out_edge_iteratorS<Graph>)
68
69
70
71
72
73
74
75
76
77
78
79
80
{
    return out_degreeS()(v, g);
}

struct get_laplacian
{
    template <class Graph, class Index, class Weight>
    void operator()(const Graph& g, Index index, Weight weight, deg_t deg,
                    multi_array_ref<double,1>& data,
                    multi_array_ref<int32_t,1>& i,
                    multi_array_ref<int32_t,1>& j) const
    {
        int pos = 0;
81
        for (const auto& e : edges_range(g))
82
        {
83
            if (source(e, g) == target(e, g))
84
85
                continue;

86
87
88
            data[pos] = -get(weight, e);
            i[pos] = get(index, target(e, g));
            j[pos] = get(index, source(e, g));
89
90

            ++pos;
91
            if (!graph_tool::is_directed(g))
92
            {
93
94
95
                data[pos] = -get(weight, e);
                i[pos] = get(index, source(e, g));
                j[pos] = get(index, target(e, g));
96
97
98
99
                ++pos;
            }
        }

100
        for (auto v : vertices_range(g))
101
102
103
104
105
        {
            double k = 0;
            switch (deg)
            {
            case OUT_DEG:
106
                k = sum_degree(g, v, weight, out_edge_iteratorS<Graph>());
107
108
                break;
            case IN_DEG:
109
                k = sum_degree(g, v, weight, in_edge_iteratorS<Graph>());
110
111
                break;
            case TOTAL_DEG:
112
                k = sum_degree(g, v, weight, all_edges_iteratorS<Graph>());
113
114
            }
            data[pos] = k;
115
            i[pos] = j[pos] = get(index, v);
116
117
118
119
120
121
            ++pos;
        }

    }
};

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
template <class Graph, class Vindex, class Weight, class Deg, class V>
void lap_matvec(Graph& g, Vindex index, Weight w, Deg d, V& x, V& ret)
{
    parallel_vertex_loop
        (g,
         [&](auto v)
         {
             std::remove_reference_t<decltype(ret[v])> y = 0;
             if constexpr (!std::is_same_v<Weight, UnityPropertyMap<double, GraphInterface::edge_t>>)
             {
                 for (auto e : in_or_out_edges_range(v, g))
                 {
                     auto u = target(e, g);
                     if (u == v)
                         continue;
                     auto w_e = get(w, e);
                     y += w_e * x[get(index, u)];
                 }
             }
             else
             {
                 for (auto u : in_or_out_neighbors_range(v, g))
                 {
                     if (u == v)
                         continue;
                     y += x[get(index, u)];
                 }
             }
             ret[get(index, v)] = d[v] * x[get(index, v)] - y;
         });
}

template <class Graph, class Vindex, class Weight, class Deg, class Mat>
void lap_matmat(Graph& g, Vindex index, Weight w, Deg d, Mat& x, Mat& ret)
{
    size_t M = x.shape()[1];
    parallel_vertex_loop
        (g,
         [&](auto v)
         {
             auto vi = get(index, v);
             auto y = ret[vi];
             if constexpr (!std::is_same_v<Weight, UnityPropertyMap<double, GraphInterface::edge_t>>)
             {
                 for (auto e : in_or_out_edges_range(v, g))
                 {
                     auto u = target(e, g);
                     if (u == v)
                         continue;
                     auto w_e = get(w, e);
                     auto ui = get(index, u);
                     for (size_t i = 0; i < M; ++i)
                         y[i] += w_e * x[ui][i];
                 }
             }
             else
             {
                 for (auto u : in_or_out_neighbors_range(v, g))
                 {
                     if (u == v)
                         continue;
                     auto ui = get(index, u);
                     for (size_t i = 0; i < M; ++i)
                         y[i] += x[ui][i];
                 }
             }
             for (size_t i = 0; i < M; ++i)
                 ret[vi][i] = d[v] * x[vi][i] - y[i];
         });
}
192
193
194
195
196
197
198
199
200
201
202


struct get_norm_laplacian
{
    template <class Graph, class Index, class Weight>
    void operator()(const Graph& g, Index index, Weight weight, deg_t deg,
                    multi_array_ref<double,1>& data,
                    multi_array_ref<int32_t,1>& i,
                    multi_array_ref<int32_t,1>& j) const
    {
        int pos = 0;
203
        std::vector<double> degs(num_vertices(g));
204
        for (auto v : vertices_range(g))
205
        {
206
            double k = 0;
207
208
209
            switch (deg)
            {
            case OUT_DEG:
210
                k = sum_degree(g, v, weight, out_edge_iteratorS<Graph>());
211
212
                break;
            case IN_DEG:
213
                k = sum_degree(g, v, weight, in_edge_iteratorS<Graph>());
214
215
                break;
            case TOTAL_DEG:
216
                k = sum_degree(g, v, weight, all_edges_iteratorS<Graph>());
217
            }
218
219
            degs[v] = sqrt(k);
        }
220

221
222
223
        for (auto v : vertices_range(g))
        {
            double ks = degs[v];
224
            for(const auto& e : out_edges_range(v, g))
225
            {
226
                if (source(e, g) == target(e, g))
227
                    continue;
228
                double kt = degs[target(e, g)];
229
                if (ks * kt > 0)
230
                    data[pos] = -get(weight, e) / (ks * kt);
231
232
                i[pos] = get(index, target(e, g));
                j[pos] = get(index, source(e, g));
233
234
235
236
237
238

                ++pos;
            }

            if (ks > 0)
                data[pos] = 1;
239
            i[pos] = j[pos] = get(index, v);
240
241
242
243
244
245
            ++pos;
        }

    }
};

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
template <class Graph, class Vindex, class Weight, class Deg, class V>
void nlap_matvec(Graph& g, Vindex index, Weight w, Deg id, V& x, V& ret)
{
    parallel_vertex_loop
        (g,
         [&](auto v)
         {
             auto vi = get(index, v);
             std::remove_reference_t<decltype(ret[v])> y = 0;
             if constexpr (!std::is_same_v<Weight, UnityPropertyMap<double, GraphInterface::edge_t>>)
             {
                 for (auto e : in_or_out_edges_range(v, g))
                 {
                     auto u = target(e, g);
                     if (u == v)
                         continue;
                     auto w_e = get(w, e);
                     y += w_e * x[get(index, u)] * id[u];
                 }
             }
             else
             {
                 for (auto u : in_or_out_neighbors_range(v, g))
                 {
                     if (u == v)
                         continue;
                     y += x[get(index, u)] * id[u];
                 }
             }
             if (id[v] > 0)
                 ret[vi] = x[vi] - y * id[v];
         });
}

template <class Graph, class Vindex, class Weight, class Deg, class Mat>
void nlap_matmat(Graph& g, Vindex index, Weight w, Deg id, Mat& x, Mat& ret)
{
    size_t M = x.shape()[1];
    parallel_vertex_loop
        (g,
         [&](auto v)
         {
             auto vi = get(index, v);
             auto y = ret[vi];
             if constexpr (!std::is_same_v<Weight, UnityPropertyMap<double, GraphInterface::edge_t>>)
             {
                 for (auto e : in_or_out_edges_range(v, g))
                 {
                     auto u = target(e, g);
                     if (u == v)
                         continue;
                     auto w_e = get(w, e);
                     auto ui = get(index, u);
                     for (size_t i = 0; i < M; ++i)
                         y[i] += w_e * x[ui][i] * id[u];
                 }
             }
             else
             {
                 for (auto u : in_or_out_neighbors_range(v, g))
                 {
                     if (u == v)
                         continue;
                     auto ui = get(index, u);
                     for (size_t i = 0; i < M; ++i)
                         y[i] += x[ui][i] * id[u];
                 }
             }

             if (id[v] > 0)
             {
                 for (size_t i = 0; i < M; ++i)
                     y[i] = x[vi][i] - y[i] * id[v];
             }
         });
}

323
324
325
326

} // namespace graph_tool

#endif // GRAPH_LAPLACIAN_HH