graph_motifs.hh 14.1 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-2013 Tiago de Paula Peixoto <tiago@skewed.de>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// 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/>.

#ifndef GRAPH_MOTIFS_HH
#define GRAPH_MOTIFS_HH

#include <boost/graph/isomorphism.hpp>
22
23
#include "tr1_include.hh"
#include TR1_HEADER(unordered_set)
24
#include <boost/functional/hash.hpp>
25
26
#include <algorithm>

27
28
#include "random.hh"

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace graph_tool
{

template <class Value>
void insert_sorted(vector<Value>& v, const Value& val)
{
    typeof(v.begin()) iter = lower_bound(v.begin(), v.end(), val);
    if (iter != v.end() && *iter == val)
        return; // no repetitions
    v.insert(iter, val);
}

template <class Value>
bool has_val(vector<Value>& v, const Value& val)
{
    typeof(v.begin()) iter = lower_bound(v.begin(), v.end(), val);
    if (iter == v.end())
        return false;
    return *iter == val;
}

50
// gets all the subgraphs starting from vertex v and store it in subgraphs.
51
template <class Graph, class Sampler>
52
void get_subgraphs(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
                   size_t n,
                   vector<vector<typename graph_traits<Graph>::vertex_descriptor> >& subgraphs,
                   Sampler sampler)
{
    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;

    // extension and subgraph stack
    vector<vector<vertex_t> > ext_stack(1);
    vector<vector<vertex_t> > sub_stack(1);
    vector<vector<vertex_t> > sub_neighbours_stack(1);

    sub_stack[0].push_back(v);
    typename graph_traits<Graph>::out_edge_iterator e, e_end;
    for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
    {
        typename graph_traits<Graph>::vertex_descriptor u = target(*e, g);
        if (u > v && !has_val(ext_stack[0], u))
        {
            insert_sorted(ext_stack[0], u);
            insert_sorted(sub_neighbours_stack[0],u);
        }
    }

    while (!sub_stack.empty())
    {
        vector<vertex_t>& ext = ext_stack.back();
        vector<vertex_t>& sub = sub_stack.back();
        vector<vertex_t>& sub_neighbours = sub_neighbours_stack.back();

        if (sub.size() == n)
        {
            // found a subgraph of the desired size; put it in the list and go
            // back a level
            subgraphs.push_back(sub);
            sub_stack.pop_back();
            ext_stack.pop_back();
            sub_neighbours_stack.pop_back();
            continue;
        }

        if (ext.empty())
        {
            // no where else to go
            ext_stack.pop_back();
            sub_stack.pop_back();
            sub_neighbours_stack.pop_back();
            continue;
        }
        else
        {
            // extend subgraph
            vector<vertex_t> new_ext, new_sub = sub,
                new_sub_neighbours = sub_neighbours;

            // remove w from ext
            vertex_t w = ext.back();
            ext.pop_back();

            // insert w in subgraph
            insert_sorted(new_sub, w);

            // update new_ext
            new_ext = ext;
            for (tie(e, e_end) = out_edges(w, g); e != e_end; ++e)
            {
                vertex_t u = target(*e,g);
                if (u > v)
                {
                    if (!has_val(sub_neighbours, u))
                        insert_sorted(new_ext, u);
                    insert_sorted(new_sub_neighbours, u);
                }
            }

            sampler(new_ext, ext_stack.size());

            ext_stack.push_back(new_ext);
            sub_stack.push_back(new_sub);
            sub_neighbours_stack.push_back(new_sub_neighbours);
        }
    }
}

136
137
// sampling selectors

138
139
140
141
142
143
144
145
146
147
148
149
150
151
struct sample_all
{
    template <class val_type>
    void operator()(vector<val_type>&, size_t) {}
};

struct sample_some
{
    sample_some(vector<double>& p, rng_t& rng): _p(&p), _rng(&rng) {}
    sample_some() {}

    template <class val_type>
    void operator()(vector<val_type>& extend, size_t d)
    {
152
        typedef tr1::uniform_real<double> rdist_t;
153
        tr1::variate_generator<rng_t&, rdist_t> random(*_rng, rdist_t());
154

155
156
157
158
159
160
161
162
163
164
        double pd = (*_p)[d+1];
        size_t nc = extend.size();
        double u = nc*pd - floor(nc*pd);
        size_t n;
        double r;
        {
            #pragma omp critical
            r = random();
        }
        if (r < u)
165
            n = size_t(ceil(nc*pd));
166
        else
167
            n = size_t(floor(nc*pd));
168
169
170
171
172
173
174
175
176

        if (n == extend.size())
            return;
        if (n == 0)
        {
            extend.clear();
            return;
        }

177
        typedef tr1::uniform_int<size_t> idist_t;
178
179
        for (size_t i = 0; i < n; ++i)
        {
180
            tr1::variate_generator<rng_t&, idist_t>
181
                random_v(*_rng, idist_t(0, extend.size()-i-1));
182
183
184
            size_t j;
            {
                #pragma omp critical
185
                j = i + random_v();
186
187
188
189
190
191
192
193
194
195
196
            }
            swap(extend[i], extend[j]);
        }
        extend.resize(n);
    }

    vector<double>* _p;
    rng_t* _rng;
};


197
// build the actual induced subgraph from the vertex list
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
template <class Graph, class GraphSG>
void make_subgraph
    (vector<typename graph_traits<Graph>::vertex_descriptor>& vlist,
     Graph& g, GraphSG& sub)
{
    for (size_t i = 0; i < vlist.size(); ++i)
        add_vertex(sub);
    for (size_t i = 0; i < vlist.size(); ++i)
    {
        typename graph_traits<Graph>::vertex_descriptor ov = vlist[i], ot;
        typename graph_traits<GraphSG>::vertex_descriptor nv = vertex(i,sub);
        typename graph_traits<Graph>::out_edge_iterator e, e_end;
        for (tie(e, e_end) = out_edges(ov, g); e != e_end; ++e)
        {
            ot = target(*e, g);
            typeof(vlist.begin()) viter =
                lower_bound(vlist.begin(), vlist.end(), ot);
            size_t ot_index = viter - vlist.begin();
            if (viter != vlist.end() && vlist[ot_index] == ot &&
                (is_directed::apply<Graph>::type::value || ot < ov))
                add_edge(nv, vertex(ot_index, sub), sub);
        }
    }
}

223
// compare two graphs for labeled exactness (not isomorphism)
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
template <class Graph>
bool graph_cmp(Graph& g1, Graph& g2)
{
    if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
        return false;

    typename graph_traits<Graph>::vertex_iterator v1, v1_end;
    typename graph_traits<Graph>::vertex_iterator v2, v2_end;
    tie(v2, v2_end) = vertices(g2);
    for (tie(v1, v1_end) = vertices(g1); v1 != v1_end; ++v1)
    {
        if (out_degree(*v1, g1) != out_degree(*v2, g2))
            return false;
        if (in_degreeS()(*v1, g1) != in_degreeS()(*v2, g2))
            return false;

240
        vector<typename graph_traits<Graph>::vertex_descriptor> out1, out2;
241
242
243
244
245
246
247
248
249
250
251
252
253
        typename graph_traits<Graph>::out_edge_iterator e, e_end;
        for (tie(e, e_end) = out_edges(*v1, g1); e != e_end; ++e)
            out1.push_back(target(*e, g1));
        for (tie(e, e_end) = out_edges(*v2, g2); e != e_end; ++e)
            out2.push_back(target(*e, g2));
        sort(out1.begin(), out1.end());
        sort(out2.begin(), out2.end());
        if (out1 != out2)
            return false;
    }
    return true;
}

254
// short hand for both types of subgraphs
255
256
typedef adj_list<size_t> d_graph_t;
typedef adj_list<size_t> u_graph_t;
257

258
// we need this wrap to use the UndirectedAdaptor only on directed graphs
259
260
261
262
263
264
265
266
267
268
269
struct wrap_undirected
{
    template <class Graph>
    struct apply
    {
        typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
                                  UndirectedAdaptor<Graph>,
                                  Graph&>::type type;
    };
};

Tiago Peixoto's avatar
Tiago Peixoto committed
270
// get the signature of the graph: sorted degree sequence
271
272
273
template <class Graph>
void get_sig(Graph& g, vector<size_t>& sig)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
274
275
276
277
278
    sig.clear();
    size_t N = num_vertices(g);
    if (N > 0)
        sig.resize(is_directed::apply<Graph>::type::value ? 2 * N : N);
    for (size_t i = 0; i < N; ++i)
279
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
280
281
        typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
        sig[i] = out_degree(v, g);
282
        if(is_directed::apply<Graph>::type::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
283
            sig[i + N] = in_degreeS()(v, g);
284
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
285
    sort(sig.begin(), sig.end());
286
287
}

288
// gets (or samples) all the subgraphs in graph g
289
290
struct get_all_motifs
{
291
292
293
294
295
296
297
298
299
300
301
    get_all_motifs(bool collect_vmaps, double p, bool comp_iso, bool fill_list,
                   rng_t& rng)
        : collect_vmaps(collect_vmaps), p(p),
          comp_iso(comp_iso), fill_list(fill_list), rng(rng) {}
    bool collect_vmaps;
    double p;
    bool comp_iso;
    bool fill_list;
    rng_t& rng;

    template <class Graph, class Sampler, class VMap>
302
    void operator()(Graph& g, size_t k, boost::any& list,
303
304
                    vector<size_t>& hist, vector<vector<VMap> >& vmaps,
                    Sampler sampler) const
305
306
307
308
309
    {
        typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
                                  d_graph_t,
                                  u_graph_t>::type graph_sg_t;

310
        // the main subgraph lists
311
312
313
        vector<graph_sg_t>& subgraph_list =
            any_cast<vector<graph_sg_t>&>(list);

314
315
316
317
318
319
        // this hashes subgraphs according to their signature
        tr1::unordered_map<vector<size_t>,
                           vector<pair<size_t, graph_sg_t> >,
                           hash<vector<size_t> > > sub_list;
        vector<size_t> sig; // current signature

320
        for (size_t i = 0; i < subgraph_list.size(); ++i)
321
322
323
324
        {
            get_sig(subgraph_list[i], sig);
            sub_list[sig].push_back(make_pair(i,subgraph_list[i]));
        }
325
326

        // the subgraph count
327
328
        hist.resize(subgraph_list.size());

329
330
331
        typedef tr1::uniform_real<double> rdist_t;
        tr1::variate_generator<rng_t&, rdist_t> random(rng, rdist_t());

332
        // the set of vertices V to be sampled (filled only if p < 1)
333
        vector<size_t> V;
334
        if (p < 1)
335
        {
336
337
338
339
            typename graph_traits<Graph>::vertex_iterator v, v_end;
            for (tie(v, v_end) = vertices(g); v != v_end; ++v)
                V.push_back(*v);

340
            size_t n;
341
            if (random() < p)
342
                n = size_t(ceil(V.size()*p));
343
            else
344
                n = size_t(floor(V.size()*p));
345

346
            typedef tr1::uniform_int<size_t> idist_t;
347
348
            for (size_t i = 0; i < n; ++i)
            {
349
                tr1::variate_generator<rng_t&, idist_t>
350
351
352
                    random_v(rng, idist_t(0, V.size()-i-1));

                size_t j = i + random_v();
353
354
355
356
357
                swap(V[i], V[j]);
            }
            V.resize(n);
        }

358
        int i, N = (p < 1) ? V.size() : num_vertices(g);
359
360
        #pragma omp parallel for default(shared) private(i, sig) \
            schedule(dynamic)
361
362
        for (i = 0; i < N; ++i)
        {
363
364
365
366
367
368
            vector<vector<typename graph_traits<Graph>::vertex_descriptor> >
                subgraphs;
            typename graph_traits<Graph>::vertex_descriptor v =
                (p < 1) ? V[i] : vertex(i, g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
369
370
371
372

            typename wrap_undirected::apply<Graph>::type ug(g);
            get_subgraphs(ug, v, k, subgraphs, sampler);

Tiago Peixoto's avatar
Tiago Peixoto committed
373
374
            #pragma omp critical

375
376
377
378
            for (size_t j = 0; j < subgraphs.size(); ++j)
            {
                graph_sg_t sub;
                make_subgraph(subgraphs[j], g, sub);
379
                get_sig(sub, sig);
380

381
382
383
384
385
                typeof(sub_list.begin()) iter = sub_list.find(sig);
                if(iter == sub_list.end())
                {
                    if (!fill_list)
                        continue; // avoid inserting an element in sub_list
Tiago Peixoto's avatar
Tiago Peixoto committed
386
                    sub_list[sig].clear();
387
388
                }

389
                bool found = false;
390
                size_t pos;
Tiago Peixoto's avatar
Tiago Peixoto committed
391
392
                typeof(sub_list.begin()) sl = sub_list.find(sig);
                if (sl != sub_list.end())
393
                {
Tiago Peixoto's avatar
Tiago Peixoto committed
394
                    for (size_t l = 0; l < sl->second.size(); ++l)
395
                    {
Tiago Peixoto's avatar
Tiago Peixoto committed
396
397
398
                        graph_sg_t& motif = sl->second[l].second;
                        if (comp_iso)
                        {
399
400
401
                            if (isomorphism(motif, sub,
                                            vertex_index1_map(get(vertex_index, motif)).
                                            vertex_index2_map(get(vertex_index, sub))))
Tiago Peixoto's avatar
Tiago Peixoto committed
402
403
404
405
406
407
408
409
410
                                found = true;
                        }
                        else
                        {
                            if (graph_cmp(motif, sub))
                                found = true;
                        }
                        if (found)
                        {
411
412
                            pos = sl->second[l].first;
                            hist[pos]++;
Tiago Peixoto's avatar
Tiago Peixoto committed
413
414
                            break;
                        }
415
416
417
418
419
420
                    }
                }

                if (found == false && fill_list)
                {
                    subgraph_list.push_back(sub);
Tiago Peixoto's avatar
Tiago Peixoto committed
421
422
423
                    sub_list[sig].push_back(make_pair(subgraph_list.size() - 1,
                                                      sub));
                    hist.push_back(1);
424
425
426
427
428
429
430
431
432
433
434
                    pos = hist.size() - 1;
                    found = true;
                }

                if (found && collect_vmaps)
                {
                    if (pos >= vmaps.size())
                        vmaps.resize(pos + 1);
                    vmaps[pos].push_back(VMap(get(boost::vertex_index,sub)));
                    for (size_t vi = 0; vi < num_vertices(sub); ++vi)
                        vmaps[pos].back()[vertex(vi, sub)] = subgraphs[j][vi];
435
436
437
438
439
440
441
442
443
                }
            }
        }
    }
};

} //graph-tool namespace

#endif // GRAPH_MOTIFS_HH