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
|
// Boost.Graph library isomorphism test
// Copyright (C) 2001 Doug Gregor (gregod@cs.rpi.edu)
//
// Permission to copy, use, sell and distribute this software is granted
// provided this copyright notice appears in all copies.
// Permission to modify the code and to distribute modified code is granted
// provided this copyright notice appears in all copies, and a notice
// that the code was modified is included with the copyright notice.
//
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
// 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.
#define BOOST_INCLUDE_MAIN
#include <boost/test/test_tools.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/isomorphism.hpp>
#include <boost/property_map.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace boost;
// Verify that the given mapping iso_map from the vertices of g1 to the
// vertices of g2 describes an isomorphism.
// Note: this could be made much faster by specializing based on the graph
// concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,
// O(n^4) won't hurt us.
template<typename Graph1, typename Graph2, typename IsoMap>
inline bool verify_isomorphism(const Graph1& g1, const Graph2& g2,
IsoMap iso_map)
{
if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
return false;
for (typename graph_traits<Graph1>::edge_iterator e1 = edges(g1).first;
e1 != edges(g1).second; ++e1) {
bool found_edge = false;
for (typename graph_traits<Graph2>::edge_iterator e2 = edges(g2).first;
e2 != edges(g2).second && !found_edge; ++e2) {
if (source(*e2, g2) == get(iso_map, source(*e1, g1)) &&
target(*e2, g2) == get(iso_map, target(*e1, g1))) {
found_edge = true;
}
}
if (!found_edge)
return false;
}
return true;
}
template <typename Generator>
struct random_functor {
random_functor(Generator& g) : g(g) { }
std::size_t operator()(std::size_t n) {
boost::uniform_int<boost::mt19937, std::size_t> x(g, 0, n-1);
return x();
}
Generator& g;
};
template<typename Graph1, typename Graph2>
void randomly_permute_graph(const Graph1& g1, Graph2& g2)
{
// Need a clean graph to start with
assert(num_vertices(g2) == 0);
assert(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;
boost::mt19937 gen;
random_functor<boost::mt19937> rand_fun(gen);
// Decide new order
std::vector<vertex1> orig_vertices(vertices(g1).first, vertices(g1).second);
std::random_shuffle(orig_vertices.begin(), orig_vertices.end(), rand_fun);
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);
}
}
template<typename Graph>
void generate_random_digraph(Graph& g, double edge_probability)
{
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
boost::mt19937 random_gen;
boost::uniform_real<boost::mt19937, double>
random_dist(random_gen, 0.0, 1.0);
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_isomorphism(int n, double edge_probability)
{
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 = 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;
}
}
}
}
int test_main(int argc, char* argv[])
{
if (argc < 3) {
test_isomorphism(30, 0.45);
return 0;
}
int n = atoi(argv[1]);
double edge_prob = atof(argv[2]);
test_isomorphism(n, edge_prob);
return 0;
}
|