1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2015-2017
6 * EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.)
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
18 #include <lemon/vf2.h>
19 #include <lemon/vf2pp.h>
20 #include <lemon/concepts/digraph.h>
21 #include <lemon/smart_graph.h>
22 #include <lemon/lgf_reader.h>
23 #include <lemon/concepts/maps.h>
25 #include <test/test_tools.h>
28 using namespace lemon;
148 SmartGraph petersen, c5, c7, c10, p10;
149 SmartGraph::NodeMap<int> petersen_col1(petersen);
150 SmartGraph::NodeMap<int> petersen_col2(petersen);
151 SmartGraph::NodeMap<int> c5_col(c5);
154 std::stringstream ss(petersen_lgf);
155 graphReader(petersen, ss)
156 .nodeMap("col1", petersen_col1)
157 .nodeMap("col2", petersen_col2)
163 //std::stringstream ss2(c5_lgf);
165 .nodeMap("col", c5_col)
171 graphReader(c7, ss).run();
176 graphReader(c10, ss).run();
181 graphReader(p10, ss).run();
187 bool operator==(const EqComparable&) {
192 template<class A, class B>
195 bool operator()(A, B){
200 class IntConvertible1 {
207 class IntConvertible2 {
214 template<class G1,class G2>
215 void checkVf2Compile() {
218 concepts::ReadWriteMap<typename G1::Node, typename G2::Node> r;
220 ::lemon::ignore_unused_variable_warning(succ);
222 succ = vf2(g,h).run();
223 succ = vf2(g,h).induced().run();
224 succ = vf2(g,h).iso().run();
225 succ = vf2(g,h).mapping(r).run();
227 Vf2<G1,G2,concepts::ReadWriteMap<typename G1::Node, typename G2::Node>,
228 EqClass<typename G1::Node,typename G2::Node> >
229 myVf2(g,h,r,EqClass<typename G1::Node,typename G2::Node>());
232 succ = vf2(g,h).induced().mapping(r).run();
233 succ = vf2(g,h).iso().mapping(r).run();
235 concepts::ReadMap<typename G1::Node, EqComparable> l1;
236 concepts::ReadMap<typename G2::Node, EqComparable> l2;
237 succ = vf2(g,h).nodeLabels(l1,l2).mapping(r).run();
238 succ = vf2(g,h).nodeEq(EqClass<typename G1::Node,typename G2::Node>())
243 checkVf2Compile<concepts::Graph,concepts::Graph>();
244 checkVf2Compile<concepts::Graph,SmartGraph>();
245 checkVf2Compile<SmartGraph,concepts::Graph>();
248 template<class G1,class G2>
249 void checkVf2ppCompile() {
252 concepts::ReadWriteMap<typename G1::Node, typename G2::Node> r;
254 ::lemon::ignore_unused_variable_warning(succ);
256 succ = vf2pp(g,h).run();
257 succ = vf2pp(g,h).induced().run();
258 succ = vf2pp(g,h).iso().run();
259 succ = vf2pp(g,h).mapping(r).run();
260 succ = vf2pp(g,h).induced().mapping(r).run();
261 succ = vf2pp(g,h).iso().mapping(r).run();
263 concepts::ReadMap<typename G1::Node, int> c1;
264 concepts::ReadMap<typename G2::Node, int> c2;
265 Vf2pp<G1,G2,concepts::ReadWriteMap<typename G1::Node, typename G2::Node>,
266 concepts::ReadMap<typename G1::Node, int>,
267 concepts::ReadMap<typename G2::Node, int> >
268 myVf2pp(g,h,r,c1,c2);
271 succ = vf2pp(g,h).nodeLabels(c1,c2).mapping(r).run();
272 succ = vf2pp(g,h).nodeLabels(c1,c2).mapping(r).run();
274 concepts::ReadMap<typename G1::Node, char> c1_c;
275 concepts::ReadMap<typename G2::Node, char> c2_c;
276 Vf2pp<G1,G2,concepts::ReadWriteMap<typename G1::Node, typename G2::Node>,
277 concepts::ReadMap<typename G1::Node, char>,
278 concepts::ReadMap<typename G2::Node, char> >
279 myVf2pp_c(g,h,r,c1_c,c2_c);
282 succ = vf2pp(g,h).nodeLabels(c1_c,c2_c).mapping(r).run();
283 succ = vf2pp(g,h).nodeLabels(c1_c,c2_c).mapping(r).run();
285 concepts::ReadMap<typename G1::Node, IntConvertible1> c1_IntConv;
286 concepts::ReadMap<typename G2::Node, IntConvertible2> c2_IntConv;
287 Vf2pp<G1,G2,concepts::ReadWriteMap<typename G1::Node, typename G2::Node>,
288 concepts::ReadMap<typename G1::Node, IntConvertible1>,
289 concepts::ReadMap<typename G2::Node, IntConvertible2> >
290 myVf2pp_IntConv(g,h,r,c1_IntConv,c2_IntConv);
291 myVf2pp_IntConv.find();
293 succ = vf2pp(g,h).nodeLabels(c1_IntConv,c2_IntConv).mapping(r).run();
294 succ = vf2pp(g,h).nodeLabels(c1_IntConv,c2_IntConv).mapping(r).run();
297 void vf2ppCompile() {
298 checkVf2ppCompile<concepts::Graph,concepts::Graph>();
299 checkVf2ppCompile<concepts::Graph,SmartGraph>();
300 checkVf2ppCompile<SmartGraph,concepts::Graph>();
303 template<class G1, class G2, class I>
304 void checkSubIso(const G1 &g1, const G2 &g2, const I &i) {
305 std::set<typename G2::Node> image;
306 for (typename G1::NodeIt n(g1);n!=INVALID;++n){
307 check(i[n]!=INVALID, "Wrong isomorphism: incomplete mapping.");
308 check(image.count(i[n])==0,"Wrong isomorphism: not injective.");
311 for (typename G1::EdgeIt e(g1);e!=INVALID;++e) {
312 check(findEdge(g2,i[g1.u(e)],i[g1.v(e)])!=INVALID,
313 "Wrong isomorphism: missing edge(checkSub).");
317 template<class G1, class G2, class I>
318 void checkIndIso(const G1 &g1, const G2 &g2, const I &i) {
319 std::set<typename G2::Node> image;
320 for (typename G1::NodeIt n(g1);n!=INVALID;++n) {
321 check(i[n]!=INVALID, "Wrong isomorphism: incomplete mapping.");
322 check(image.count(i[n])==0,"Wrong isomorphism: not injective.");
325 for (typename G1::NodeIt n(g1); n!=INVALID; ++n) {
326 for (typename G1::NodeIt m(g1); m!=INVALID; ++m) {
327 if((findEdge(g1,n,m)==INVALID) != (findEdge(g2,i[n],i[m])==INVALID)) {
328 std::cout << "Wrong isomorphism: edge mismatch";
335 template<class G1,class G2,class T>
336 bool checkSub(const G1 &g1, const G2 &g2, const T &vf2) {
337 typename G1:: template NodeMap<typename G2::Node> iso(g1,INVALID);
338 if (const_cast<T&>(vf2).mapping(iso).run()) {
339 checkSubIso(g1,g2,iso);
345 template<class G1,class G2,class T>
346 bool checkInd(const G1 &g1, const G2 &g2, const T &vf2) {
347 typename G1:: template NodeMap<typename G2::Node> iso(g1,INVALID);
348 if (const_cast<T&>(vf2).induced().mapping(iso).run()) {
349 checkIndIso(g1,g2,iso);
355 template<class G1,class G2,class T>
356 bool checkIso(const G1 &g1, const G2 &g2, const T &vf2) {
357 typename G1:: template NodeMap<typename G2::Node> iso(g1,INVALID);
358 if (const_cast<T&>(vf2).iso().mapping(iso).run()) {
359 check(countNodes(g1)==countNodes(g2),
360 "Wrong iso alg.: they are not isomophic.");
361 checkIndIso(g1,g2,iso);
367 template<class G1, class G2, class L1, class L2, class I>
368 void checkLabel(const G1 &g1, const G2 &,
369 const L1 &l1, const L2 &l2, const I &i) {
370 for (typename G1::NodeIt n(g1);n!=INVALID;++n) {
371 check(l1[n]==l2[i[n]],"Wrong isomorphism: label mismatch.");
375 template<class G1,class G2,class L1,class L2,class T>
376 bool checkSub(const G1 &g1, const G2 &g2, const L1 &l1, const L2 &l2, const T &vf2) {
377 typename G1:: template NodeMap<typename G2::Node> iso(g1,INVALID);
378 if (const_cast<T&>(vf2).nodeLabels(l1,l2).mapping(iso).run()){
379 checkSubIso(g1,g2,iso);
380 checkLabel(g1,g2,l1,l2,iso);
386 template<class G1,class G2>
387 void checkSub(const G1 &g1, const G2 &g2, bool expected, const char* msg) {
388 check(checkSub(g1,g2,vf2(g1,g2)) == expected, msg);
389 check(checkSub(g1,g2,vf2pp(g1,g2)) == expected, msg);
392 template<class G1,class G2>
393 void checkInd(const G1 &g1, const G2 &g2, bool expected, const char* msg) {
394 check(checkInd(g1,g2,vf2(g1,g2)) == expected, msg);
395 check(checkInd(g1,g2,vf2pp(g1,g2)) == expected, msg);
398 template<class G1,class G2>
399 void checkIso(const G1 &g1, const G2 &g2, bool expected, const char* msg) {
400 check(checkIso(g1,g2,vf2(g1,g2)) == expected, msg);
401 check(checkIso(g1,g2,vf2pp(g1,g2)) == expected, msg);
404 template<class G1,class G2,class L1,class L2>
405 void checkSub(const G1 &g1, const G2 &g2, const L1 &l1, const L2 &l2, bool expected, const char* msg) {
406 check(checkSub(g1,g2,l1,l2,vf2(g1,g2)) == expected, msg);
407 check(checkSub(g1,g2,l1,l2,vf2pp(g1,g2)) == expected, msg);
413 checkSub(c5,petersen,true,
414 "There should exist a C5->Petersen mapping.");
415 checkSub(c7,petersen,false,
416 "There should not exist a C7->Petersen mapping.");
417 checkSub(p10,petersen,true,
418 "There should exist a P10->Petersen mapping.");
419 checkSub(c10,petersen,false,
420 "There should not exist a C10->Petersen mapping.");
421 checkSub(petersen,petersen,true,
422 "There should exist a Petersen->Petersen mapping.");
424 checkInd(c5,petersen,true,
425 "There should exist a C5->Petersen spanned mapping.");
426 checkInd(c7,petersen,false,
427 "There should exist a C7->Petersen spanned mapping.");
428 checkInd(p10,petersen,false,
429 "There should not exist a P10->Petersen spanned mapping.");
430 checkInd(c10,petersen,false,
431 "There should not exist a C10->Petersen spanned mapping.");
432 checkInd(petersen,petersen,true,
433 "There should exist a Petersen->Petersen spanned mapping.");
435 checkSub(petersen,c10,false,
436 "There should not exist a Petersen->C10 mapping.");
437 checkSub(p10,c10,true,
438 "There should exist a P10->C10 mapping.");
439 checkInd(p10,c10,false,
440 "There should not exist a P10->C10 spanned mapping.");
441 checkSub(c10,p10,false,
442 "There should not exist a C10->P10 mapping.");
444 checkIso(p10,c10,false,
445 "P10 and C10 are not isomorphic.");
446 checkIso(c10,c10,true,
447 "C10 and C10 are isomorphic.");
449 checkSub(c5,petersen,c5_col,petersen_col1,false,
450 "There should exist a C5->Petersen mapping.");
451 checkSub(c5,petersen,c5_col,petersen_col2,true,
452 "There should exist a C5->Petersen mapping.");
454 check(!vf2(p10,c10).iso().run(), "P10 and C10 are not isomorphic.");
455 check(!vf2pp(p10,c10).iso().run(), "P10 and C10 are not isomorphic.");
457 check(vf2(c10,c10).iso().run(), "C10 and C10 are isomorphic.");
458 check(vf2pp(c10,c10).iso().run(), "C10 and C10 are isomorphic.");