two_graphs_common_spanning_trees_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2012, Michele Caini.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Two Graphs Common Spanning Trees Algorithm
  6. // Based on academic article of Mint, Read and Tarjan
  7. // Efficient Algorithm for Common Spanning Tree Problem
  8. // Electron. Lett., 28 April 1983, Volume 19, Issue 9, p.346-347
  9. #include <boost/type_traits.hpp>
  10. #include <boost/concept/requires.hpp>
  11. #include <boost/assign/std/vector.hpp>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/compressed_pair.hpp>
  14. #include <boost/test/minimal.hpp>
  15. #include <boost/graph/two_graphs_common_spanning_trees.hpp>
  16. #include <vector>
  17. using namespace boost::assign;
  18. namespace boost
  19. {
  20. typedef
  21. boost::adjacency_list
  22. <
  23. boost::vecS, // OutEdgeList
  24. boost::vecS, // VertexList
  25. boost::undirectedS, // Directed
  26. boost::no_property, // VertexProperties
  27. boost::no_property, // EdgeProperties
  28. boost::no_property, // GraphProperties
  29. boost::listS // EdgeList
  30. >
  31. Graph
  32. ;
  33. typedef
  34. boost::graph_traits<Graph>::vertex_descriptor
  35. vertex_descriptor;
  36. typedef
  37. boost::graph_traits<Graph>::edge_descriptor
  38. edge_descriptor;
  39. typedef
  40. boost::graph_traits<Graph>::vertex_iterator
  41. vertex_iterator;
  42. typedef
  43. boost::graph_traits<Graph>::edge_iterator
  44. edge_iterator;
  45. template < typename Coll, typename Seq >
  46. struct check_edge
  47. {
  48. public:
  49. BOOST_CONCEPT_ASSERT((RandomAccessContainer<Coll>));
  50. BOOST_CONCEPT_ASSERT((RandomAccessContainer<Seq>));
  51. typedef typename Coll::value_type coll_value_type;
  52. typedef typename Seq::value_type seq_value_type;
  53. BOOST_STATIC_ASSERT((is_same<coll_value_type, Seq>::value));
  54. BOOST_STATIC_ASSERT((is_same<seq_value_type, bool>::value));
  55. void operator()(Coll& coll, Seq& seq)
  56. {
  57. bool found = false;
  58. for(typename Coll::iterator iterator = coll.begin();
  59. !found && iterator != coll.end(); ++iterator)
  60. {
  61. Seq& coll_seq = *iterator;
  62. BOOST_REQUIRE(coll_seq.size() == seq.size());
  63. found = true;
  64. for(typename Seq::size_type pos = 0; found && pos < seq.size(); ++pos) {
  65. found &= coll_seq[pos] == seq[pos];
  66. }
  67. }
  68. BOOST_REQUIRE(found);
  69. }
  70. };
  71. void two_graphs_common_spanning_trees_test()
  72. {
  73. Graph iG, vG;
  74. std::vector< edge_descriptor > iG_o;
  75. std::vector< edge_descriptor > vG_o;
  76. iG_o.push_back(boost::add_edge(0, 1, iG).first);
  77. iG_o.push_back(boost::add_edge(1, 3, iG).first);
  78. iG_o.push_back(boost::add_edge(3, 2, iG).first);
  79. iG_o.push_back(boost::add_edge(1, 5, iG).first);
  80. iG_o.push_back(boost::add_edge(5, 4, iG).first);
  81. iG_o.push_back(boost::add_edge(5, 6, iG).first);
  82. iG_o.push_back(boost::add_edge(5, 3, iG).first);
  83. iG_o.push_back(boost::add_edge(3, 1, iG).first);
  84. iG_o.push_back(boost::add_edge(1, 3, iG).first);
  85. vG_o.push_back(boost::add_edge(0, 2, vG).first);
  86. vG_o.push_back(boost::add_edge(0, 4, vG).first);
  87. vG_o.push_back(boost::add_edge(0, 5, vG).first);
  88. vG_o.push_back(boost::add_edge(5, 1, vG).first);
  89. vG_o.push_back(boost::add_edge(5, 3, vG).first);
  90. vG_o.push_back(boost::add_edge(5, 6, vG).first);
  91. vG_o.push_back(boost::add_edge(5, 4, vG).first);
  92. vG_o.push_back(boost::add_edge(5, 2, vG).first);
  93. vG_o.push_back(boost::add_edge(2, 6, vG).first);
  94. std::vector< std::vector<bool> > coll;
  95. boost::tree_collector<
  96. std::vector< std::vector<bool> >, std::vector<bool>
  97. > collector(coll);
  98. std::vector<bool> inL(iG_o.size(), false);
  99. boost::two_graphs_common_spanning_trees(iG, iG_o, vG, vG_o, collector, inL);
  100. check_edge< std::vector< std::vector<bool> >, std::vector<bool> > checker;
  101. std::vector<bool> check;
  102. check.clear();
  103. check += true, true, true, true, true, true, false, false, false;
  104. checker(coll, check);
  105. check.clear();
  106. check += true, true, true, true, true, true, false, false, false;
  107. checker(coll, check);
  108. }
  109. }
  110. int test_main ( int argc, char** argv )
  111. {
  112. boost::two_graphs_common_spanning_trees_test();
  113. return EXIT_SUCCESS;
  114. }