two_graphs_common_spanning_trees.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/graph/adjacency_list.hpp>
  10. #include <boost/graph/two_graphs_common_spanning_trees.hpp>
  11. #include <exception>
  12. #include <vector>
  13. using namespace std;
  14. typedef
  15. boost::adjacency_list
  16. <
  17. boost::vecS, // OutEdgeList
  18. boost::vecS, // VertexList
  19. boost::undirectedS, // Directed
  20. boost::no_property, // VertexProperties
  21. boost::no_property, // EdgeProperties
  22. boost::no_property, // GraphProperties
  23. boost::listS // EdgeList
  24. >
  25. Graph
  26. ;
  27. typedef
  28. boost::graph_traits<Graph>::vertex_descriptor
  29. vertex_descriptor;
  30. typedef
  31. boost::graph_traits<Graph>::edge_descriptor
  32. edge_descriptor;
  33. typedef
  34. boost::graph_traits<Graph>::vertex_iterator
  35. vertex_iterator;
  36. typedef
  37. boost::graph_traits<Graph>::edge_iterator
  38. edge_iterator;
  39. int main(int argc, char **argv)
  40. {
  41. Graph iG, vG;
  42. vector< edge_descriptor > iG_o;
  43. vector< edge_descriptor > vG_o;
  44. iG_o.push_back(boost::add_edge(0, 1, iG).first);
  45. iG_o.push_back(boost::add_edge(0, 2, iG).first);
  46. iG_o.push_back(boost::add_edge(0, 3, iG).first);
  47. iG_o.push_back(boost::add_edge(0, 4, iG).first);
  48. iG_o.push_back(boost::add_edge(1, 2, iG).first);
  49. iG_o.push_back(boost::add_edge(3, 4, iG).first);
  50. vG_o.push_back(boost::add_edge(1, 2, vG).first);
  51. vG_o.push_back(boost::add_edge(2, 0, vG).first);
  52. vG_o.push_back(boost::add_edge(2, 3, vG).first);
  53. vG_o.push_back(boost::add_edge(4, 3, vG).first);
  54. vG_o.push_back(boost::add_edge(0, 3, vG).first);
  55. vG_o.push_back(boost::add_edge(0, 4, vG).first);
  56. vector<bool> inL(iG_o.size(), false);
  57. std::vector< std::vector<bool> > coll;
  58. boost::tree_collector<
  59. std::vector< std::vector<bool> >,
  60. std::vector<bool>
  61. > tree_collector(coll);
  62. boost::two_graphs_common_spanning_trees
  63. (
  64. iG,
  65. iG_o,
  66. vG,
  67. vG_o,
  68. tree_collector,
  69. inL
  70. );
  71. std::vector< std::vector<bool> >::iterator it;
  72. for(it = coll.begin(); it != coll.end(); ++it) {
  73. // Here you can play with the trees that the algorithm has found.
  74. }
  75. return 0;
  76. }