File: isomorphism.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (415 lines) | stat: -rw-r--r-- 13,227 bytes parent folder | download | duplicates (5)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Boost.Graph library isomorphism test

// Copyright (C) 2001-20044 Douglas Gregor (dgregor at cs dot indiana dot edu)
//
// 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)

// For more information, see http://www.boost.org
//
// Revision History:
//
// 29 Nov 2001    Jeremy Siek
//      Changed to use Boost.Random.
// 29 Nov 2001    Doug Gregor
//      Initial checkin.

#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <time.h> // clock used without std:: qualifier?
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/isomorphism.hpp>
#include <boost/property_map/property_map.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>

#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< random_generator_type&,
            boost::uniform_int< std::size_t > >
            x(g, distrib);
        return x();
    }
    Generator& g;
};
#endif

template < typename Graph1, typename Graph2 >
std::map<
    typename graph_traits< Graph1 >::vertex_descriptor,
    typename graph_traits< Graph2 >::vertex_descriptor
>
randomly_permute_graph(const Graph1& g1, Graph2& g2)
{
    // Need a clean graph to start with
    BOOST_TEST(num_vertices(g2) == 0);
    BOOST_TEST(num_edges(g2) == 0);

    typedef typename graph_traits< Graph1 >::vertex_descriptor vertex1;
    typedef typename graph_traits< Graph2 >::vertex_descriptor vertex2;
    typedef typename graph_traits< Graph1 >::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< vertex1 > orig_vertices;
    std::copy(vertices(g1).first, vertices(g1).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< vertex1, vertex2 > vertex_map;

    for (std::size_t i = 0; i < num_vertices(g1); ++i)
    {
        vertex_map[orig_vertices[i]] = add_vertex(g2);
    }

    for (edge_iterator e = edges(g1).first; e != edges(g1).second; ++e)
    {
        add_edge(vertex_map[source(*e, g1)], vertex_map[target(*e, g1)], g2);
    }

    return vertex_map;
}

template < typename Graph >
void generate_random_digraph(Graph& g, double edge_probability)
{
    typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
    random_generator_type random_gen;
    boost::uniform_real< double > distrib(0.0, 1.0);
    boost::variate_generator< random_generator_type&,
        boost::uniform_real< double > >
        random_dist(random_gen, distrib);

    for (vertex_iterator u = vertices(g).first; u != vertices(g).second; ++u)
    {
        vertex_iterator v = u;
        ++v;
        for (; v != vertices(g).second; ++v)
        {
            if (random_dist() <= edge_probability)
                add_edge(*u, *v, g);
        }
    }
}

void test_isomorphism2()
{
    using namespace boost::graph::keywords;
    typedef adjacency_list< vecS, vecS, bidirectionalS > graph1;
    typedef adjacency_list< listS, listS, bidirectionalS,
        property< vertex_index_t, int > >
        graph2;

    graph1 g1(2);
    add_edge(vertex(0, g1), vertex(1, g1), g1);
    add_edge(vertex(1, g1), vertex(1, g1), g1);
    graph2 g2;
    randomly_permute_graph(g1, g2);

    int v_idx = 0;
    for (graph2::vertex_iterator v = vertices(g2).first;
         v != vertices(g2).second; ++v)
    {
        put(vertex_index_t(), g2, *v, v_idx++);
    }

    std::map< graph1::vertex_descriptor, graph2::vertex_descriptor > mapping;

    bool isomorphism_correct;
    clock_t start = clock();
    BOOST_TEST(isomorphism_correct = boost::graph::isomorphism(g1, g2,
                    _vertex_index1_map = get(vertex_index, g1),
                    _isomorphism_map = make_assoc_property_map(mapping)));
    clock_t end = clock();

    std::cout << "Elapsed time (clock cycles): " << (end - start) << std::endl;

    bool verify_correct;
    BOOST_TEST(verify_correct
        = verify_isomorphism(g1, g2, make_assoc_property_map(mapping)));

    if (!isomorphism_correct || !verify_correct)
    {
        // Output graph 1
        {
            std::ofstream out("isomorphism_failure.bg1");
            out << num_vertices(g1) << std::endl;
            for (graph1::edge_iterator e = edges(g1).first;
                 e != edges(g1).second; ++e)
            {
                out << get(vertex_index_t(), g1, source(*e, g1)) << ' '
                    << get(vertex_index_t(), g1, target(*e, g1)) << std::endl;
            }
        }

        // Output graph 2
        {
            std::ofstream out("isomorphism_failure.bg2");
            out << num_vertices(g2) << std::endl;
            for (graph2::edge_iterator e = edges(g2).first;
                 e != edges(g2).second; ++e)
            {
                out << get(vertex_index_t(), g2, source(*e, g2)) << ' '
                    << get(vertex_index_t(), g2, target(*e, g2)) << std::endl;
            }
        }
    }
}

void test_isomorphism(int n, double edge_probability)
{
    using namespace boost::graph::keywords;
    typedef adjacency_list< vecS, vecS, bidirectionalS > graph1;
    typedef adjacency_list< listS, listS, bidirectionalS,
        property< vertex_index_t, int > >
        graph2;

    graph1 g1(n);
    generate_random_digraph(g1, edge_probability);
    graph2 g2;
    randomly_permute_graph(g1, g2);

    int v_idx = 0;
    for (graph2::vertex_iterator v = vertices(g2).first;
         v != vertices(g2).second; ++v)
    {
        put(vertex_index_t(), g2, *v, v_idx++);
    }

    std::map< graph1::vertex_descriptor, graph2::vertex_descriptor > mapping;

    bool isomorphism_correct;
    clock_t start = clock();
    BOOST_TEST(isomorphism_correct = boost::graph::isomorphism(g1, g2,
                    _isomorphism_map = make_assoc_property_map(mapping)));
    clock_t end = clock();

    std::cout << "Elapsed time (clock cycles): " << (end - start) << std::endl;

    bool verify_correct;
    BOOST_TEST(verify_correct
        = verify_isomorphism(g1, g2, make_assoc_property_map(mapping)));

    if (!isomorphism_correct || !verify_correct)
    {
        // Output graph 1
        {
            std::ofstream out("isomorphism_failure.bg1");
            out << num_vertices(g1) << std::endl;
            for (graph1::edge_iterator e = edges(g1).first;
                 e != edges(g1).second; ++e)
            {
                out << get(vertex_index_t(), g1, source(*e, g1)) << ' '
                    << get(vertex_index_t(), g1, target(*e, g1)) << std::endl;
            }
        }

        // Output graph 2
        {
            std::ofstream out("isomorphism_failure.bg2");
            out << num_vertices(g2) << std::endl;
            for (graph2::edge_iterator e = edges(g2).first;
                 e != edges(g2).second; ++e)
            {
                out << get(vertex_index_t(), g2, source(*e, g2)) << ' '
                    << get(vertex_index_t(), g2, target(*e, g2)) << std::endl;
            }
        }
    }
}

template<typename Graph>
struct ColorFunctor {
    typedef typename graph_traits< Graph >::vertex_descriptor argument_type;
    typedef int result_type;

    ColorFunctor(const Graph& g) : graph(g) {}

    inline result_type operator() (argument_type v) const {
        return get(vertex_color_t(), graph)(v);
    }

    inline result_type max() const {
      result_type max_result = std::numeric_limits<result_type>::min();
      typedef typename graph_traits< Graph >::vertex_iterator vertex_iter;
      for(vertex_iter iter = vertices(graph).first; iter != vertices(graph).second; ++iter) {
        max_result = std::max(max_result, this->operator()(*iter));
      }
      return max_result + 1;
    }

    const Graph& graph;
};

void test_colored_isomorphism(int n, double edge_probability)
{
    using namespace boost::graph::keywords;
    typedef adjacency_list< vecS, vecS, bidirectionalS, property< vertex_color_t, int > > graph1;
    typedef adjacency_list< listS, listS, bidirectionalS,
        property< vertex_index_t, int, property< vertex_color_t, int >
        >
    > graph2;

    typedef graph_traits< graph1 >::vertex_descriptor vertex1_t;
    typedef graph_traits< graph2 >::vertex_descriptor vertex2_t;
    typedef std::map< vertex1_t, vertex2_t > vertex_map_t;

    graph1 g1(n);
    generate_random_digraph(g1, edge_probability);

    graph2 g2;
    vertex_map_t vertex_map = randomly_permute_graph(g1, g2);

    std::vector< int > colors(n);
    typedef std::vector< int >::iterator colors_iter;
    colors_iter midpoint = colors.begin() + n / 2;
    std::fill(colors.begin(), midpoint, 0);
    std::fill(midpoint, colors.end(), 1);

    random_generator_type gen;

#ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
    random_functor< random_generator_type > rand_fun(gen);
    std::random_shuffle(colors.begin(), colors.end(), rand_fun);
#else
    std::shuffle(colors.begin(), colors.end(), gen);
#endif

    int v_idx = 0;
    for (graph1::vertex_iterator v = vertices(g1).first;
         v != vertices(g1).second; ++v)
    {
        put(get(vertex_color_t(), g1), *v, colors[v_idx]);
        put(get(vertex_color_t(), g2), vertex_map[*v], colors[v_idx]);
        v_idx += 1;
    }

    v_idx = 0;
    for (graph2::vertex_iterator v = vertices(g2).first;
         v != vertices(g2).second; ++v)
    {
        put(get(vertex_index_t(), g2), *v, v_idx);
        v_idx += 1;
    }

    typedef std::map< graph1::vertex_descriptor, graph2::vertex_descriptor > iso_map;
    iso_map mapping;

    bool isomorphism_correct;
    clock_t start = clock();
    BOOST_TEST(
        isomorphism_correct = isomorphism(
            g1,
            g2,
            isomorphism_map(make_assoc_property_map(mapping))
            .vertex_invariant1(ColorFunctor<graph1>(g1))
            .vertex_invariant2(ColorFunctor<graph2>(g2))
        )
    );
    clock_t end = clock();

    std::cout << "Elapsed time (clock cycles): " << (end - start) << std::endl;

    bool verify_correct;
    BOOST_TEST(verify_correct
        = verify_isomorphism(g1, g2, make_assoc_property_map(mapping)));

    if (!isomorphism_correct || !verify_correct)
    {
        // Output graph 1
        {
            std::ofstream out("colored_isomorphism_failure.bg1");
            out << num_vertices(g1) << std::endl;
            for (graph1::edge_iterator e = edges(g1).first;
                 e != edges(g1).second; ++e)
            {
                out << get(vertex_index_t(), g1, source(*e, g1)) << ' '
                    << get(vertex_index_t(), g1, target(*e, g1)) << std::endl;
            }
        }

        // Output graph 2
        {
            std::ofstream out("colored_isomorphism_failure.bg2");
            out << num_vertices(g2) << std::endl;
            for (graph2::edge_iterator e = edges(g2).first;
                 e != edges(g2).second; ++e)
            {
                out << get(vertex_index_t(), g2, source(*e, g2)) << ' '
                    << get(vertex_index_t(), g2, target(*e, g2)) << std::endl;
            }
        }
    }

    bool map_contains_null_vertices = false;
    {
        typedef graph_traits< graph2 >::vertex_descriptor vertex2_t;
        const vertex2_t g2_null_vertex = graph2::null_vertex();

        typedef iso_map::iterator map_iter;
        const map_iter end = mapping.end();
        for(map_iter iter = mapping.begin(); iter != end; ++iter) {
            if(iter->second == g2_null_vertex) {
                map_contains_null_vertices = true;
                break;
            }
        }
    }

    BOOST_TEST(!map_contains_null_vertices);

    // Map is bijective if each vertex of the second graph occurs only once
    {
      typedef graph_traits< graph2 >::vertex_descriptor vertex2_t;
      std::set< vertex2_t > vertex_set;

      typedef iso_map::iterator map_iter;
      const map_iter end = mapping.end();
      for(map_iter iter = mapping.begin(); iter != end; ++iter) {
        vertex_set.insert(iter->second);
      }

      BOOST_TEST(vertex_set.size() == mapping.size());
    }
}

int main(int argc, char* argv[])
{
    int n = argc < 3 ? 30 : boost::lexical_cast< int >(argv[1]);
    double edge_prob = argc < 3 ? 0.45 : boost::lexical_cast< double >(argv[2]);
    test_isomorphism(n, edge_prob);
    test_colored_isomorphism(n, edge_prob);

    return boost::report_errors();
}