File: vf2_sub_graph_iso_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (376 lines) | stat: -rw-r--r-- 12,205 bytes parent folder | download | duplicates (11)
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
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
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
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
192
193
194
195
196
197
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//=======================================================================
// Boost.Graph library vf2_sub_graph_iso test
// Adapted from isomorphism.cpp and mcgregor_subgraphs_test.cpp
//
// Copyright (C) 2012 Flavio De Lorenzi (fdlorenzi@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

// Revision History:
//   8 April 2013: Fixed a typo in random_functor. (Flavio De Lorenzi)

#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>
#include <boost/graph/random.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/graph/graphviz.hpp>

#ifndef BOOST_NO_CXX11_HDR_RANDOM
#include <random>
typedef std::mt19937 random_generator_type;
#else
typedef boost::mt19937 random_generator_type;
#endif

using namespace boost;

#ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
template < typename Generator > struct random_functor
{
    random_functor(Generator& g) : g(g) {}
    std::size_t operator()(std::size_t n)
    {
        boost::uniform_int< std::size_t > distrib(0, n - 1);
        boost::variate_generator< Generator&,
            boost::uniform_int< std::size_t > >
            x(g, distrib);
        return x();
    }
    Generator& g;
};
#endif

template < typename Graph1, typename Graph2 >
void randomly_permute_graph(Graph1& g1, const Graph2& g2)
{
    BOOST_TEST(num_vertices(g1) <= num_vertices(g2));
    BOOST_TEST(num_edges(g1) == 0);

    typedef typename graph_traits< Graph1 >::vertex_descriptor vertex1;
    typedef typename graph_traits< Graph2 >::vertex_descriptor vertex2;
    typedef typename graph_traits< Graph1 >::vertex_iterator vertex_iterator;
    typedef typename graph_traits< Graph2 >::edge_iterator edge_iterator;

    random_generator_type gen;
#ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
    random_functor< random_generator_type > rand_fun(gen);
#endif

    // Decide new order
    std::vector< vertex2 > orig_vertices;
    std::copy(vertices(g2).first, vertices(g2).second,
        std::back_inserter(orig_vertices));
#ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
    std::random_shuffle(orig_vertices.begin(), orig_vertices.end(), rand_fun);
#else
    std::shuffle(orig_vertices.begin(), orig_vertices.end(), gen);
#endif
    std::map< vertex2, vertex1 > vertex_map;

    std::size_t i = 0;
    for (vertex_iterator vi = vertices(g1).first; vi != vertices(g1).second;
         ++i, ++vi)
    {
        vertex_map[orig_vertices[i]] = *vi;
        put(vertex_name_t(), g1, *vi,
            get(vertex_name_t(), g2, orig_vertices[i]));
    }

    for (edge_iterator ei = edges(g2).first; ei != edges(g2).second; ++ei)
    {
        typename std::map< vertex2, vertex1 >::iterator si
            = vertex_map.find(source(*ei, g2)),
            ti = vertex_map.find(target(*ei, g2));
        if ((si != vertex_map.end()) && (ti != vertex_map.end()))
            add_edge(si->second, ti->second, get(edge_name_t(), g2, *ei), g1);
    }
}

template < typename Graph >
void generate_random_digraph(Graph& g, double edge_probability,
    int max_parallel_edges, double parallel_edge_probability, int max_edge_name,
    int max_vertex_name)
{

    BOOST_TEST((0 <= edge_probability) && (edge_probability <= 1));
    BOOST_TEST(
        (0 <= parallel_edge_probability) && (parallel_edge_probability <= 1));
    BOOST_TEST(0 <= max_parallel_edges);
    BOOST_TEST(0 <= max_edge_name);
    BOOST_TEST(0 <= max_vertex_name);

    typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
    random_generator_type random_gen;
    boost::uniform_real< double > dist_real(0.0, 1.0);
    boost::variate_generator< random_generator_type&,
        boost::uniform_real< double > >
        random_real_dist(random_gen, dist_real);

    for (vertex_iterator u = vertices(g).first; u != vertices(g).second; ++u)
    {
        for (vertex_iterator v = vertices(g).first; v != vertices(g).second;
             ++v)
        {
            if (random_real_dist() <= edge_probability)
            {
                add_edge(*u, *v, g);
                for (int i = 0; i < max_parallel_edges; ++i)
                {
                    if (random_real_dist() <= parallel_edge_probability)
                        add_edge(*u, *v, g);
                }
            }
        }
    }

    {
        boost::uniform_int< int > dist_int(0, max_edge_name);
        boost::variate_generator< random_generator_type&,
            boost::uniform_int< int > >
            random_int_dist(random_gen, dist_int);
        randomize_property< vertex_name_t >(g, random_int_dist);
    }

    {
        boost::uniform_int< int > dist_int(0, max_vertex_name);
        boost::variate_generator< random_generator_type&,
            boost::uniform_int< int > >
            random_int_dist(random_gen, dist_int);

        randomize_property< edge_name_t >(g, random_int_dist);
    }
}

template < typename Graph1, typename Graph2, typename EdgeEquivalencePredicate,
    typename VertexEquivalencePredicate >
struct test_callback
{

    test_callback(const Graph1& graph1, const Graph2& graph2,
        EdgeEquivalencePredicate edge_comp,
        VertexEquivalencePredicate vertex_comp, bool output)
    : graph1_(graph1)
    , graph2_(graph2)
    , edge_comp_(edge_comp)
    , vertex_comp_(vertex_comp)
    , output_(output)
    {
    }

    template < typename CorrespondenceMap1To2, typename CorrespondenceMap2To1 >
    bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1)
    {

        bool verified;
        BOOST_TEST(verified = verify_vf2_subgraph_iso(
                        graph1_, graph2_, f, edge_comp_, vertex_comp_));

        // Output (sub)graph isomorphism map
        if (!verified || output_)
        {
            std::cout << "Verfied: " << std::boolalpha << verified << std::endl;
            std::cout << "Num vertices: " << num_vertices(graph1_) << ' '
                      << num_vertices(graph2_) << std::endl;
            BGL_FORALL_VERTICES_T(v, graph1_, Graph1)
            std::cout << '(' << get(vertex_index_t(), graph1_, v) << ", "
                      << get(vertex_index_t(), graph2_, get(f, v)) << ") ";

            std::cout << std::endl;
        }

        return true;
    }

private:
    const Graph1& graph1_;
    const Graph2& graph2_;
    EdgeEquivalencePredicate edge_comp_;
    VertexEquivalencePredicate vertex_comp_;
    bool output_;
};

// we pretend this is something more complicated which calculates indices
// somehow
template < typename G > struct IndirectIndexMap
{
    typedef std::size_t value_type;
    typedef value_type reference;
    typedef typename boost::graph_traits< G >::vertex_descriptor key_type;
    typedef boost::readable_property_map_tag category;
    explicit IndirectIndexMap(const G& g) : g(g) {}

public:
    const G& g;
};

template < typename G >
std::size_t get(const IndirectIndexMap< G >& map,
    typename boost::graph_traits< G >::vertex_descriptor v)
{
    // we pretend this is something more complicated which calculates indices
    // somehow
    return get(vertex_index_t(), map.g, v);
}

void test_vf2_sub_graph_iso(int n1, int n2, double edge_probability,
    int max_parallel_edges, double parallel_edge_probability, int max_edge_name,
    int max_vertex_name, bool output)
{

    typedef property< edge_name_t, int > edge_property;
    typedef property< vertex_name_t, int, property< vertex_index_t, int > >
        vertex_property;

    typedef adjacency_list< listS, listS, bidirectionalS, vertex_property,
        edge_property >
        graph1;
    typedef adjacency_list< vecS, vecS, bidirectionalS, vertex_property,
        edge_property >
        graph2;

    graph1 g1(n1);
    graph2 g2(n2);
    generate_random_digraph(g2, edge_probability, max_parallel_edges,
        parallel_edge_probability, max_edge_name, max_vertex_name);
    randomly_permute_graph(g1, g2);

    int v_idx = 0;
    for (graph_traits< graph1 >::vertex_iterator vi = vertices(g1).first;
         vi != vertices(g1).second; ++vi)
    {
        put(vertex_index_t(), g1, *vi, v_idx++);
    }

    // Create vertex and edge predicates
    typedef property_map< graph1, vertex_name_t >::type vertex_name_map1;
    typedef property_map< graph2, vertex_name_t >::type vertex_name_map2;

    typedef property_map_equivalent< vertex_name_map1, vertex_name_map2 >
        vertex_predicate;
    vertex_predicate vertex_comp = make_property_map_equivalent(
        get(vertex_name, g1), get(vertex_name, g2));

    typedef property_map< graph1, edge_name_t >::type edge_name_map1;
    typedef property_map< graph2, edge_name_t >::type edge_name_map2;

    typedef property_map_equivalent< edge_name_map1, edge_name_map2 >
        edge_predicate;
    edge_predicate edge_comp
        = make_property_map_equivalent(get(edge_name, g1), get(edge_name, g2));

    std::clock_t start = std::clock();

    // Create callback
    test_callback< graph1, graph2, edge_predicate, vertex_predicate > callback(
        g1, g2, edge_comp, vertex_comp, output);

    std::cout << std::endl;
    BOOST_TEST(vf2_subgraph_iso(g1, g2, callback, vertex_order_by_mult(g1),
        edges_equivalent(edge_comp).vertices_equivalent(vertex_comp)));
    BOOST_TEST(vf2_subgraph_iso(g1, g2, callback,
        IndirectIndexMap< graph1 >(g1), IndirectIndexMap< graph2 >(g2),
        vertex_order_by_mult(g1), edge_comp, vertex_comp));

    std::clock_t end1 = std::clock();
    std::cout << "vf2_subgraph_iso: elapsed time (clock cycles): "
              << (end1 - start) << std::endl;

    if (num_vertices(g1) == num_vertices(g2))
    {
        std::cout << std::endl;
        BOOST_TEST(vf2_graph_iso(g1, g2, callback, vertex_order_by_mult(g1),
            edges_equivalent(edge_comp).vertices_equivalent(vertex_comp)));

        std::clock_t end2 = std::clock();
        std::cout << "vf2_graph_iso: elapsed time (clock cycles): "
                  << (end2 - end1) << std::endl;
    }

    if (output)
    {
        std::fstream file_graph1("graph1.dot", std::fstream::out);
        write_graphviz(file_graph1, g1,
            make_label_writer(get(boost::vertex_name, g1)),
            make_label_writer(get(boost::edge_name, g1)));

        std::fstream file_graph2("graph2.dot", std::fstream::out);
        write_graphviz(file_graph2, g2,
            make_label_writer(get(boost::vertex_name, g2)),
            make_label_writer(get(boost::edge_name, g2)));
    }
}

int main(int argc, char* argv[])
{

    int num_vertices_g1 = 10;
    int num_vertices_g2 = 20;
    double edge_probability = 0.4;
    int max_parallel_edges = 2;
    double parallel_edge_probability = 0.4;
    int max_edge_name = 5;
    int max_vertex_name = 5;
    bool output = false;

    if (argc > 1)
    {
        num_vertices_g1 = boost::lexical_cast< int >(argv[1]);
    }

    if (argc > 2)
    {
        num_vertices_g2 = boost::lexical_cast< int >(argv[2]);
    }

    if (argc > 3)
    {
        edge_probability = boost::lexical_cast< double >(argv[3]);
    }

    if (argc > 4)
    {
        max_parallel_edges = boost::lexical_cast< int >(argv[4]);
    }

    if (argc > 5)
    {
        parallel_edge_probability = boost::lexical_cast< double >(argv[5]);
    }

    if (argc > 6)
    {
        max_edge_name = boost::lexical_cast< int >(argv[6]);
    }

    if (argc > 7)
    {
        max_vertex_name = boost::lexical_cast< int >(argv[7]);
    }

    if (argc > 8)
    {
        output = boost::lexical_cast< bool >(argv[8]);
    }

    test_vf2_sub_graph_iso(num_vertices_g1, num_vertices_g2, edge_probability,
        max_parallel_edges, parallel_edge_probability, max_edge_name,
        max_vertex_name, output);

    return boost::report_errors();
}