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