我正在编写使用boost库进行图形挖掘的代码,我想知道如何测试:
如果两个图表使用同构相等(仅当图表具有相同的结构和相同的标签时才返回true)
如果图形是另一个图形的子图形?
这是图表文件:3test.txt
以下是我所做的源代码的一些部分:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/config.hpp>
#include <boost/graph/isomorphism.hpp>
#include <boost/graph/graph_utility.hpp>
#include <fstream>
#include <iostream>
#include <vector>
//for mmap:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
using namespace boost;
//==========STRUCTURES==========
// vertex
struct VertexProperties {
int id;
int label;
VertexProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
// edge
struct EdgeProperties {
unsigned label;
EdgeProperties(unsigned l = 0) :label(l) {}
};
// Graph
struct GraphProperties {
unsigned id;
unsigned label;
GraphProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
// adjency list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties,
GraphProperties> Graph;
// descriptors
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
// iterators
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
typedef std::pair<edge_iter, edge_iter> edge_pair;
//*********global variables*************
vector<Graph> dataG;
//=================callback used fro subgraph_iso=================================================================
// Default print_callback
template <typename Graph1,typename Graph2>
struct my_callback {
my_callback(const Graph1& graph1, const Graph2& graph2)
: graph1_(graph1), graph2_(graph2) {}
template <typename CorrespondenceMap1To2,
typename CorrespondenceMap2To1>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const {
return true;
}
private:
const Graph1& graph1_;
const Graph2& graph2_;
};
//==========handle_error==========
void handle_error(const char *msg) {
perror(msg);
exit(255);
}
//============READ ALL THE FILE AND RETURN A STRING===================
const char *readfromfile(const char *fname, size_t &length) {
int fd = open(fname, O_RDONLY);
if (fd == -1)
handle_error("open");
// obtain file size
struct stat sb;
if (fstat(fd, &sb) == -1)
handle_error("fstat");
length = sb.st_size;
const char *addr = static_cast<const char *>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0u));
if (addr == MAP_FAILED)
handle_error("mmap");
// TODO close fd at some point in time, call munmap(...)
return addr;
}
//==========SPLIT THE STRING BY NEWLINE (\n) ==========
vector<string> splitstringtolines(string const& str) {
std::vector<string> split_vector;
split(split_vector, str, is_any_of("\n"));
return split_vector;
}
//============Get a string starting from pos============
string getpos(int const& pos, string const& yy) {
size_t i = pos;
string str;
for (; ((yy[i] != ' ') && (i < yy.length())); i++) {str += yy[i];}
return str;
}
//==================read string vector and return graphs vector===================
std::vector<Graph> creategraphs(std::vector<string> const& fichlines) {
for (string yy : fichlines) {
switch (yy[0]) {
case 't': {
string str2 = getpos(4, yy);
unsigned gid = atoi(str2.c_str());
dataG.emplace_back(GraphProperties(gid, gid));
} break;
case 'v': {
assert(!dataG.empty()); // assert will terminate the program if its argument turns out to be false
// cout<<yy<<endl;
int vId, vLabel;
string vvv = getpos(2, yy);
vId = atoi(vvv.c_str());
string vvvv = getpos((int)vvv.length() + 3, yy);
// cout<<vvvv<<endl;
vLabel = atoi(vvvv.c_str());
boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
}
break;
case 'e': { // cout<<yy<<endl;
assert(!dataG.empty()); // assert will terminate the program if its argument turns out to be false
int fromId, toId, eLabel;
string eee = getpos(2, yy);
// cout<<eee<<endl;
fromId = atoi(eee.c_str());
string eee2 = getpos((int)eee.length() + 3, yy);
// cout<<eee2<<endl;
toId = atoi(eee2.c_str());
int c = (int)eee.length() + (int)eee2.length() + 4;
// cout<<c<<endl;
string eee3 = getpos(c, yy);
// cout<<eee3<<endl;
eLabel = atoi(eee3.c_str());
for (size_t i = 0; i < num_vertices(dataG.back()); ++i) // size_t vertice number in the graph
{
if(dataG.back()[i].id==fromId) fromId=i;
else if(dataG.back()[i].id==toId) toId=i;
}
boost::add_edge(fromId, toId, EdgeProperties(eLabel), dataG.back());
} break;
}
}
return dataG;
}
//==============================M A I N P R O G R A M =======================================
int main()
{
size_t length;
std::vector<Graph> dataG =creategraphs(splitstringtolines(readfromfile("3test.txt", length)));
cout<<"***PS***\n dataG[0] is a subgraph of dataG[1]\n dataG[2] is the same as dataG[0]\n dataG[3] is the same as dataG[0] but with other labels.\n"<<endl;
my_callback<Graph, Graph> my_callback(dataG[0], dataG[3]);
cout<<"equal(dataG[0], dataG[3],my_callback)="<<vf2_graph_iso(dataG[0], dataG[3],my_callback)<<endl;
mcgregor_common_subgraphs(dataG[0], dataG[3], true, my_callback);
}
不幸的是,当我运行它时,我遇到了这些错误:
/usr/include/boost/graph/mcgregor_common_subgraphs.hpp||In instantiation of ‘bool boost::detail::mcgregor_common_subgraphs_internal(const GraphFirst&, const GraphSecond&, const VertexIndexMapFirst&, const VertexIndexMapSecond&, CorrespondenceMapFirstToSecond, CorrespondenceMapSecondToFirst, VertexStackFirst&, EdgeEquivalencePredicate, VertexEquivalencePredicate, bool, SubGraphInternalCallback) [with GraphFirst = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; GraphSecond = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; VertexIndexMapFirst = boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int>; VertexIndexMapSecond = boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int>; CorrespondenceMapFirstToSecond = boost::shared_array_property_map<unsigned int, boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int> >; CorrespondenceMapSecondToFirst = boost::shared_array_property_map<unsigned int, boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int> >; VertexStackFirst = std::stack<unsigned int, std::deque<unsigned int, std::allocator<unsigned int> > >; EdgeEquivalencePredicate = boost::always_equivalent; VertexEquivalencePredicate = boost::always_equivalent; SubGraphInternalCallback = my_callback<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties> >]’:|
/usr/include/boost/graph/mcgregor_common_subgraphs.hpp|433|required from ‘void boost::detail::mcgregor_common_subgraphs_internal_init(const GraphFirst&, const GraphSecond&, VertexIndexMapFirst, VertexIndexMapSecond, EdgeEquivalencePredicate, VertexEquivalencePredicate, bool, SubGraphInternalCallback) [with GraphFirst = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; GraphSecond = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; VertexIndexMapFirst = boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int>; VertexIndexMapSecond = boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int>; EdgeEquivalencePredicate = boost::always_equivalent; VertexEquivalencePredicate = boost::always_equivalent; SubGraphInternalCallback = my_callback<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties> >]’|
/usr/include/boost/graph/mcgregor_common_subgraphs.hpp|484|required from ‘void boost::mcgregor_common_subgraphs(const GraphFirst&, const GraphSecond&, bool, SubGraphCallback) [with GraphFirst = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; GraphSecond = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; SubGraphCallback = my_callback<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties> >]’|
/home/mohsenuss91/Bureau/NouvelleApprocheBOOST/stackoverflow.cpp|211|required from here|
/usr/include/boost/graph/mcgregor_common_subgraphs.hpp|337|error: no match for call to ‘(my_callback<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties> >) (boost::shared_array_property_map<unsigned int, boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int> >&, boost::shared_array_property_map<unsigned int, boost::vec_adj_list_vertex_id_map<VertexProperties, unsigned int> >&, VertexSizeFirst&)’|
/home/mohsenuss91/Bureau/NouvelleApprocheBOOST/stackoverflow.cpp|69|note: candidate is:|
/home/mohsenuss91/Bureau/NouvelleApprocheBOOST/stackoverflow.cpp|76|note: template<class CorrespondenceMap1To2, class CorrespondenceMap2To1> bool my_callback<Graph1, Graph2>::operator()(CorrespondenceMap1To2, CorrespondenceMap2To1) const [with CorrespondenceMap1To2 = CorrespondenceMap1To2; CorrespondenceMap2To1 = CorrespondenceMap2To1; Graph1 = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>; Graph2 = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties>]|
/home/mohsenuss91/Bureau/NouvelleApprocheBOOST/stackoverflow.cpp|76|note: template argument deduction/substitution failed:|
/usr/include/boost/graph/mcgregor_common_subgraphs.hpp|337|note: candidate expects 2 arguments, 3 provided|
||=== Build failed: 1 error(s), 4 warning(s) (0 minute(s), 6 second(s)) ===|
答案 0 :(得分:2)
文档:
OUT:
SubGraphCallback
user_callback
在发现子图时将调用的函数对象。
operator()
必须具有以下格式:template <typename CorrespondenceMapFirstToSecond, typename CorrespondenceMapSecondToFirst> bool operator()( CorrespondenceMapFirstToSecond correspondence_map_1_to_2, CorrespondenceMapSecondToFirst correspondence_map_2_to_1, typename graph_traits<GraphFirst>::vertices_size_type subgraph_size );
所以至少让仿函数采取额外的论点:
template <typename Graph1, typename Graph2>
struct my_callback {
my_callback(const Graph1 &graph1, const Graph2 &graph2) : graph1_(graph1), graph2_(graph2) {}
template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const {
// vf2_graph_iso
return true;
}
template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1, typename X>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1, X) const {
// mcgregor_common_subgraphs
return true;
}
private:
const Graph1 &graph1_;
const Graph2 &graph2_;
};
当然,这/只是/修复了编译错误。它看起来并不像回调做有用的工作。
输出:
***PS***
dataG[0] is a subgraph of dataG[1]
dataG[2] is the same as dataG[0]
dataG[3] is the same as dataG[0] but with other labels.
equal(dataG[0], dataG[3],my_callback)=1
工作样本:
<强> Live On Coliru 强>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>
#include <boost/graph/mcgregor_common_subgraphs.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/config.hpp>
#include <boost/graph/isomorphism.hpp>
#include <boost/graph/graph_utility.hpp>
#include <fstream>
#include <iostream>
#include <vector>
// for mmap:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
using namespace boost;
//==========STRUCTURES==========
// vertex
struct VertexProperties {
int id;
int label;
VertexProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
// edge
struct EdgeProperties {
unsigned label;
EdgeProperties(unsigned l = 0) : label(l) {}
};
// Graph
struct GraphProperties {
unsigned id;
unsigned label;
GraphProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
// adjency list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties,
GraphProperties> Graph;
// descriptors
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
// iterators
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
typedef std::pair<edge_iter, edge_iter> edge_pair;
//*********global variables*************
vector<Graph> dataG;
//=================callback used fro subgraph_iso=================================================================
// Default print_callback
template <typename Graph1, typename Graph2>
struct my_callback {
my_callback(const Graph1 &graph1, const Graph2 &graph2) : graph1_(graph1), graph2_(graph2) {}
template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const {
// vf2_graph_iso
return true;
}
template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1, typename X>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1, X) const {
// mcgregor_common_subgraphs
return true;
}
private:
const Graph1 &graph1_;
const Graph2 &graph2_;
};
//==========handle_error==========
void handle_error(const char *msg) {
perror(msg);
exit(255);
}
//============READ ALL THE FILE AND RETURN A STRING===================
const char *readfromfile(const char *fname, size_t &length) {
int fd = open(fname, O_RDONLY);
if (fd == -1)
handle_error("open");
// obtain file size
struct stat sb;
if (fstat(fd, &sb) == -1)
handle_error("fstat");
length = sb.st_size;
const char *addr = static_cast<const char *>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0u));
if (addr == MAP_FAILED)
handle_error("mmap");
// TODO close fd at some point in time, call munmap(...)
return addr;
}
//==========SPLIT THE STRING BY NEWLINE (\n) ==========
vector<string> splitstringtolines(string const &str) {
std::vector<string> split_vector;
split(split_vector, str, is_any_of("\n"));
return split_vector;
}
//============Get a string starting from pos============
string getpos(int const &pos, string const &yy) {
size_t i = pos;
string str;
for (; ((yy[i] != ' ') && (i < yy.length())); i++) {
str += yy[i];
}
return str;
}
//==================read string vector and return graphs vector===================
std::vector<Graph> creategraphs(std::vector<string> const &fichlines) {
for (string yy : fichlines) {
switch (yy[0]) {
case 't': {
string str2 = getpos(4, yy);
unsigned gid = atoi(str2.c_str());
dataG.emplace_back(GraphProperties(gid, gid));
} break;
case 'v': {
assert(!dataG.empty()); // assert will terminate the program if its argument turns out to be false
// cout<<yy<<endl;
int vId, vLabel;
string vvv = getpos(2, yy);
vId = atoi(vvv.c_str());
string vvvv = getpos((int)vvv.length() + 3, yy);
// cout<<vvvv<<endl;
vLabel = atoi(vvvv.c_str());
boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
}
break;
case 'e': { // cout<<yy<<endl;
assert(!dataG.empty()); // assert will terminate the program if its argument turns out to be false
int fromId, toId, eLabel;
string eee = getpos(2, yy);
// cout<<eee<<endl;
fromId = atoi(eee.c_str());
string eee2 = getpos((int)eee.length() + 3, yy);
// cout<<eee2<<endl;
toId = atoi(eee2.c_str());
int c = (int)eee.length() + (int)eee2.length() + 4;
// cout<<c<<endl;
string eee3 = getpos(c, yy);
// cout<<eee3<<endl;
eLabel = atoi(eee3.c_str());
for (size_t i = 0; i < num_vertices(dataG.back()); ++i) // size_t vertice number in the graph
{
if (dataG.back()[i].id == fromId)
fromId = i;
else if (dataG.back()[i].id == toId)
toId = i;
}
boost::add_edge(fromId, toId, EdgeProperties(eLabel), dataG.back());
} break;
}
}
return dataG;
}
//==============================M A I N P R O G R A M =======================================
int main() {
size_t length;
std::vector<Graph> dataG = creategraphs(splitstringtolines(readfromfile("3test.txt", length)));
cout << "***PS***\n dataG[0] is a subgraph of dataG[1]\n dataG[2] is the same as dataG[0]\n dataG[3] is the same "
"as dataG[0] but with other labels.\n" << endl;
my_callback<Graph, Graph> my_callback(dataG[0], dataG[3]);
cout << "equal(dataG[0], dataG[3],my_callback)=" << vf2_graph_iso(dataG[0], dataG[3], my_callback) << endl;
mcgregor_common_subgraphs(dataG[0], dataG[3], true, my_callback);
}