two_graphs_common_spanning_trees.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. #ifndef BOOST_GRAPH_TWO_GRAPHS_COMMON_SPANNING_TREES_HPP
  10. #define BOOST_GRAPH_TWO_GRAPHS_COMMON_SPANNING_TREES_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/bimap.hpp>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/concept/requires.hpp>
  15. #include <boost/graph/graph_traits.hpp>
  16. #include <boost/graph/undirected_dfs.hpp>
  17. #include <boost/graph/connected_components.hpp>
  18. #include <boost/graph/filtered_graph.hpp>
  19. #include <vector>
  20. #include <stack>
  21. #include <map>
  22. namespace boost
  23. {
  24. namespace detail {
  25. template
  26. <
  27. typename TreeMap,
  28. typename PredMap,
  29. typename DistMap,
  30. typename LowMap,
  31. typename Buffer
  32. >
  33. struct bridges_visitor: public default_dfs_visitor
  34. {
  35. bridges_visitor(
  36. TreeMap tree,
  37. PredMap pred,
  38. DistMap dist,
  39. LowMap low,
  40. Buffer& buffer
  41. ): mTree(tree), mPred(pred), mDist(dist), mLow(low), mBuffer(buffer)
  42. { mNum = -1; }
  43. template <typename Vertex, typename Graph>
  44. void initialize_vertex(const Vertex& u, const Graph& g)
  45. {
  46. put(mPred, u, u);
  47. put(mDist, u, -1);
  48. }
  49. template <typename Vertex, typename Graph>
  50. void discover_vertex(const Vertex& u, const Graph& g)
  51. {
  52. put(mDist, u, ++mNum);
  53. put(mLow, u, get(mDist, u));
  54. }
  55. template <typename Edge, typename Graph>
  56. void tree_edge(const Edge& e, const Graph& g)
  57. {
  58. put(mPred, target(e, g), source(e, g));
  59. put(mTree, target(e, g), e);
  60. }
  61. template <typename Edge, typename Graph>
  62. void back_edge(const Edge& e, const Graph& g)
  63. {
  64. put(mLow, source(e, g),
  65. (std::min)(get(mLow, source(e, g)), get(mDist, target(e, g))));
  66. }
  67. template <typename Vertex, typename Graph>
  68. void finish_vertex(const Vertex& u, const Graph& g)
  69. {
  70. Vertex parent = get(mPred, u);
  71. if(get(mLow, u) > get(mDist, parent))
  72. mBuffer.push(get(mTree, u));
  73. put(mLow, parent,
  74. (std::min)(get(mLow, parent), get(mLow, u)));
  75. }
  76. TreeMap mTree;
  77. PredMap mPred;
  78. DistMap mDist;
  79. LowMap mLow;
  80. Buffer& mBuffer;
  81. int mNum;
  82. };
  83. template <typename Buffer>
  84. struct cycle_finder: public base_visitor< cycle_finder<Buffer> >
  85. {
  86. typedef on_back_edge event_filter;
  87. cycle_finder(): mBuffer(0) { }
  88. cycle_finder(Buffer* buffer)
  89. : mBuffer(buffer) { }
  90. template <typename Edge, typename Graph>
  91. void operator()(const Edge& e, const Graph& g)
  92. {
  93. if(mBuffer)
  94. mBuffer->push(e);
  95. }
  96. Buffer* mBuffer;
  97. };
  98. template <typename DeletedMap>
  99. struct deleted_edge_status
  100. {
  101. deleted_edge_status() { }
  102. deleted_edge_status(DeletedMap map): mMap(map) { }
  103. template <typename Edge>
  104. bool operator()(const Edge& e) const
  105. { return (!get(mMap, e)); }
  106. DeletedMap mMap;
  107. };
  108. template <typename InLMap>
  109. struct inL_edge_status
  110. {
  111. inL_edge_status() { }
  112. inL_edge_status(InLMap map): mMap(map) { }
  113. template <typename Edge>
  114. bool operator()(const Edge& e) const
  115. { return get(mMap, e); }
  116. InLMap mMap;
  117. };
  118. template <
  119. typename Graph,
  120. typename Func,
  121. typename Seq,
  122. typename Map
  123. >
  124. void rec_two_graphs_common_spanning_trees
  125. (
  126. const Graph& iG,
  127. bimap<
  128. bimaps::set_of<int>,
  129. bimaps::set_of< typename graph_traits<Graph>::edge_descriptor >
  130. > iG_bimap,
  131. Map aiG_inL,
  132. Map diG,
  133. const Graph& vG,
  134. bimap<
  135. bimaps::set_of<int>,
  136. bimaps::set_of< typename graph_traits<Graph>::edge_descriptor >
  137. > vG_bimap,
  138. Map avG_inL,
  139. Map dvG,
  140. Func func,
  141. Seq inL
  142. )
  143. {
  144. typedef graph_traits<Graph> GraphTraits;
  145. typedef typename GraphTraits::vertex_descriptor vertex_descriptor;
  146. typedef typename GraphTraits::edge_descriptor edge_descriptor;
  147. typedef typename Seq::size_type seq_size_type;
  148. int edges = num_vertices(iG) - 1;
  149. //
  150. // [ Michele Caini ]
  151. //
  152. // Using the condition (edges != 0) leads to the accidental submission of
  153. // sub-graphs ((V-1+1)-fake-tree, named here fat-tree).
  154. // Remove this condition is a workaround for the problem of fat-trees.
  155. // Please do not add that condition, even if it improves performance.
  156. //
  157. // Here is proposed the previous guard (that was wrong):
  158. // for(seq_size_type i = 0; (i < inL.size()) && (edges != 0); ++i)
  159. //
  160. {
  161. for(seq_size_type i = 0; i < inL.size(); ++i)
  162. if(inL[i])
  163. --edges;
  164. if(edges < 0)
  165. return;
  166. }
  167. bool is_tree = (edges == 0);
  168. if(is_tree) {
  169. func(inL);
  170. } else {
  171. std::map<vertex_descriptor, default_color_type> vertex_color;
  172. std::map<edge_descriptor, default_color_type> edge_color;
  173. std::stack<edge_descriptor> iG_buf, vG_buf;
  174. bool found = false;
  175. seq_size_type m;
  176. for(seq_size_type j = 0; j < inL.size() && !found; ++j) {
  177. if(!inL[j]
  178. && !get(diG, iG_bimap.left.at(j))
  179. && !get(dvG, vG_bimap.left.at(j)))
  180. {
  181. put(aiG_inL, iG_bimap.left.at(j), true);
  182. put(avG_inL, vG_bimap.left.at(j), true);
  183. undirected_dfs(
  184. make_filtered_graph(iG,
  185. detail::inL_edge_status< associative_property_map<
  186. std::map<edge_descriptor, bool> > >(aiG_inL)),
  187. make_dfs_visitor(
  188. detail::cycle_finder< std::stack<edge_descriptor> > (&iG_buf)),
  189. associative_property_map<
  190. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  191. associative_property_map<
  192. std::map<edge_descriptor, default_color_type> >(edge_color)
  193. );
  194. undirected_dfs(
  195. make_filtered_graph(vG,
  196. detail::inL_edge_status< associative_property_map<
  197. std::map<edge_descriptor, bool> > >(avG_inL)),
  198. make_dfs_visitor(
  199. detail::cycle_finder< std::stack<edge_descriptor> > (&vG_buf)),
  200. associative_property_map<
  201. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  202. associative_property_map<
  203. std::map<edge_descriptor, default_color_type> >(edge_color)
  204. );
  205. if(iG_buf.empty() && vG_buf.empty()) {
  206. inL[j] = true;
  207. found = true;
  208. m = j;
  209. } else {
  210. while(!iG_buf.empty()) iG_buf.pop();
  211. while(!vG_buf.empty()) vG_buf.pop();
  212. put(aiG_inL, iG_bimap.left.at(j), false);
  213. put(avG_inL, vG_bimap.left.at(j), false);
  214. }
  215. }
  216. }
  217. if(found) {
  218. std::stack<edge_descriptor> iG_buf_copy, vG_buf_copy;
  219. for(seq_size_type j = 0; j < inL.size(); ++j) {
  220. if(!inL[j]
  221. && !get(diG, iG_bimap.left.at(j))
  222. && !get(dvG, vG_bimap.left.at(j)))
  223. {
  224. put(aiG_inL, iG_bimap.left.at(j), true);
  225. put(avG_inL, vG_bimap.left.at(j), true);
  226. undirected_dfs(
  227. make_filtered_graph(iG,
  228. detail::inL_edge_status< associative_property_map<
  229. std::map<edge_descriptor, bool> > >(aiG_inL)),
  230. make_dfs_visitor(
  231. detail::cycle_finder<
  232. std::stack<edge_descriptor> > (&iG_buf)),
  233. associative_property_map< std::map<
  234. vertex_descriptor, default_color_type> >(vertex_color),
  235. associative_property_map<
  236. std::map<edge_descriptor, default_color_type> >(edge_color)
  237. );
  238. undirected_dfs(
  239. make_filtered_graph(vG,
  240. detail::inL_edge_status< associative_property_map<
  241. std::map<edge_descriptor, bool> > >(avG_inL)),
  242. make_dfs_visitor(
  243. detail::cycle_finder<
  244. std::stack<edge_descriptor> > (&vG_buf)),
  245. associative_property_map< std::map<
  246. vertex_descriptor, default_color_type> >(vertex_color),
  247. associative_property_map<
  248. std::map<edge_descriptor, default_color_type> >(edge_color)
  249. );
  250. if(!iG_buf.empty() || !vG_buf.empty()) {
  251. while(!iG_buf.empty()) iG_buf.pop();
  252. while(!vG_buf.empty()) vG_buf.pop();
  253. put(diG, iG_bimap.left.at(j), true);
  254. put(dvG, vG_bimap.left.at(j), true);
  255. iG_buf_copy.push(iG_bimap.left.at(j));
  256. vG_buf_copy.push(vG_bimap.left.at(j));
  257. }
  258. put(aiG_inL, iG_bimap.left.at(j), false);
  259. put(avG_inL, vG_bimap.left.at(j), false);
  260. }
  261. }
  262. // REC
  263. detail::rec_two_graphs_common_spanning_trees<Graph, Func, Seq, Map>
  264. (iG, iG_bimap, aiG_inL, diG, vG, vG_bimap, aiG_inL, dvG, func, inL);
  265. while(!iG_buf_copy.empty()) {
  266. put(diG, iG_buf_copy.top(), false);
  267. put(dvG, vG_bimap.left.at(
  268. iG_bimap.right.at(iG_buf_copy.top())), false);
  269. iG_buf_copy.pop();
  270. }
  271. while(!vG_buf_copy.empty()) {
  272. put(dvG, vG_buf_copy.top(), false);
  273. put(diG, iG_bimap.left.at(
  274. vG_bimap.right.at(vG_buf_copy.top())), false);
  275. vG_buf_copy.pop();
  276. }
  277. inL[m] = false;
  278. put(aiG_inL, iG_bimap.left.at(m), false);
  279. put(avG_inL, vG_bimap.left.at(m), false);
  280. put(diG, iG_bimap.left.at(m), true);
  281. put(dvG, vG_bimap.left.at(m), true);
  282. std::map<vertex_descriptor, edge_descriptor> tree_map;
  283. std::map<vertex_descriptor, vertex_descriptor> pred_map;
  284. std::map<vertex_descriptor, int> dist_map, low_map;
  285. detail::bridges_visitor<
  286. associative_property_map<
  287. std::map<vertex_descriptor, edge_descriptor>
  288. >,
  289. associative_property_map<
  290. std::map<vertex_descriptor, vertex_descriptor>
  291. >,
  292. associative_property_map< std::map<vertex_descriptor, int> >,
  293. associative_property_map< std::map<vertex_descriptor, int> >,
  294. std::stack<edge_descriptor>
  295. >
  296. iG_vis(
  297. associative_property_map<
  298. std::map< vertex_descriptor, edge_descriptor> >(tree_map),
  299. associative_property_map<
  300. std::map< vertex_descriptor, vertex_descriptor> >(pred_map),
  301. associative_property_map<
  302. std::map< vertex_descriptor, int> >(dist_map),
  303. associative_property_map<
  304. std::map< vertex_descriptor, int> >(low_map),
  305. iG_buf
  306. ),
  307. vG_vis(
  308. associative_property_map<
  309. std::map< vertex_descriptor, edge_descriptor> >(tree_map),
  310. associative_property_map<
  311. std::map< vertex_descriptor, vertex_descriptor> >(pred_map),
  312. associative_property_map<
  313. std::map< vertex_descriptor, int> >(dist_map),
  314. associative_property_map<
  315. std::map< vertex_descriptor, int> >(low_map),
  316. vG_buf
  317. );
  318. undirected_dfs(make_filtered_graph(iG,
  319. detail::deleted_edge_status< associative_property_map<
  320. std::map<edge_descriptor, bool> > >(diG)),
  321. iG_vis,
  322. associative_property_map<
  323. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  324. associative_property_map<
  325. std::map<edge_descriptor, default_color_type> >(edge_color)
  326. );
  327. undirected_dfs(make_filtered_graph(vG,
  328. detail::deleted_edge_status< associative_property_map<
  329. std::map<edge_descriptor, bool> > >(dvG)),
  330. vG_vis,
  331. associative_property_map<
  332. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  333. associative_property_map<
  334. std::map<edge_descriptor, default_color_type> >(edge_color)
  335. );
  336. found = false;
  337. std::stack<edge_descriptor> iG_buf_tmp, vG_buf_tmp;
  338. while(!iG_buf.empty() && !found) {
  339. if(!inL[iG_bimap.right.at(iG_buf.top())]) {
  340. put(aiG_inL, iG_buf.top(), true);
  341. put(avG_inL, vG_bimap.left.at(
  342. iG_bimap.right.at(iG_buf.top())), true);
  343. undirected_dfs(
  344. make_filtered_graph(iG,
  345. detail::inL_edge_status< associative_property_map<
  346. std::map<edge_descriptor, bool> > >(aiG_inL)),
  347. make_dfs_visitor(
  348. detail::cycle_finder<
  349. std::stack<edge_descriptor> > (&iG_buf_tmp)),
  350. associative_property_map<
  351. std::map<
  352. vertex_descriptor, default_color_type> >(vertex_color),
  353. associative_property_map<
  354. std::map<edge_descriptor, default_color_type> >(edge_color)
  355. );
  356. undirected_dfs(
  357. make_filtered_graph(vG,
  358. detail::inL_edge_status< associative_property_map<
  359. std::map<edge_descriptor, bool> > >(avG_inL)),
  360. make_dfs_visitor(
  361. detail::cycle_finder<
  362. std::stack<edge_descriptor> > (&vG_buf_tmp)),
  363. associative_property_map<
  364. std::map<
  365. vertex_descriptor, default_color_type> >(vertex_color),
  366. associative_property_map<
  367. std::map<edge_descriptor, default_color_type> >(edge_color)
  368. );
  369. if(!iG_buf_tmp.empty() || !vG_buf_tmp.empty()) {
  370. found = true;
  371. } else {
  372. while(!iG_buf_tmp.empty()) iG_buf_tmp.pop();
  373. while(!vG_buf_tmp.empty()) vG_buf_tmp.pop();
  374. iG_buf_copy.push(iG_buf.top());
  375. }
  376. put(aiG_inL, iG_buf.top(), false);
  377. put(avG_inL, vG_bimap.left.at(
  378. iG_bimap.right.at(iG_buf.top())), false);
  379. }
  380. iG_buf.pop();
  381. }
  382. while(!vG_buf.empty() && !found) {
  383. if(!inL[vG_bimap.right.at(vG_buf.top())]) {
  384. put(avG_inL, vG_buf.top(), true);
  385. put(aiG_inL, iG_bimap.left.at(
  386. vG_bimap.right.at(vG_buf.top())), true);
  387. undirected_dfs(
  388. make_filtered_graph(iG,
  389. detail::inL_edge_status< associative_property_map<
  390. std::map<edge_descriptor, bool> > >(aiG_inL)),
  391. make_dfs_visitor(
  392. detail::cycle_finder<
  393. std::stack<edge_descriptor> > (&iG_buf_tmp)),
  394. associative_property_map<
  395. std::map<
  396. vertex_descriptor, default_color_type> >(vertex_color),
  397. associative_property_map<
  398. std::map<edge_descriptor, default_color_type> >(edge_color)
  399. );
  400. undirected_dfs(
  401. make_filtered_graph(vG,
  402. detail::inL_edge_status< associative_property_map<
  403. std::map<edge_descriptor, bool> > >(avG_inL)),
  404. make_dfs_visitor(
  405. detail::cycle_finder<
  406. std::stack<edge_descriptor> > (&vG_buf_tmp)),
  407. associative_property_map<
  408. std::map<
  409. vertex_descriptor, default_color_type> >(vertex_color),
  410. associative_property_map<
  411. std::map<edge_descriptor, default_color_type> >(edge_color)
  412. );
  413. if(!iG_buf_tmp.empty() || !vG_buf_tmp.empty()) {
  414. found = true;
  415. } else {
  416. while(!iG_buf_tmp.empty()) iG_buf_tmp.pop();
  417. while(!vG_buf_tmp.empty()) vG_buf_tmp.pop();
  418. vG_buf_copy.push(vG_buf.top());
  419. }
  420. put(avG_inL, vG_buf.top(), false);
  421. put(aiG_inL, iG_bimap.left.at(
  422. vG_bimap.right.at(vG_buf.top())), false);
  423. }
  424. vG_buf.pop();
  425. }
  426. if(!found) {
  427. while(!iG_buf_copy.empty()) {
  428. inL[iG_bimap.right.at(iG_buf_copy.top())] = true;
  429. put(aiG_inL, iG_buf_copy.top(), true);
  430. put(avG_inL, vG_bimap.left.at(
  431. iG_bimap.right.at(iG_buf_copy.top())), true);
  432. iG_buf.push(iG_buf_copy.top());
  433. iG_buf_copy.pop();
  434. }
  435. while(!vG_buf_copy.empty()) {
  436. inL[vG_bimap.right.at(vG_buf_copy.top())] = true;
  437. put(avG_inL, vG_buf_copy.top(), true);
  438. put(aiG_inL, iG_bimap.left.at(
  439. vG_bimap.right.at(vG_buf_copy.top())), true);
  440. vG_buf.push(vG_buf_copy.top());
  441. vG_buf_copy.pop();
  442. }
  443. // REC
  444. detail::rec_two_graphs_common_spanning_trees<
  445. Graph, Func, Seq, Map>
  446. (iG, iG_bimap, aiG_inL, diG, vG, vG_bimap, aiG_inL, dvG, func, inL);
  447. while(!iG_buf.empty()) {
  448. inL[iG_bimap.right.at(iG_buf.top())] = false;
  449. put(aiG_inL, iG_buf.top(), false);
  450. put(avG_inL, vG_bimap.left.at(
  451. iG_bimap.right.at(iG_buf.top())), false);
  452. iG_buf.pop();
  453. }
  454. while(!vG_buf.empty()) {
  455. inL[vG_bimap.right.at(vG_buf.top())] = false;
  456. put(avG_inL, vG_buf.top(), false);
  457. put(aiG_inL, iG_bimap.left.at(
  458. vG_bimap.right.at(vG_buf.top())), false);
  459. vG_buf.pop();
  460. }
  461. }
  462. put(diG, iG_bimap.left.at(m), false);
  463. put(dvG, vG_bimap.left.at(m), false);
  464. }
  465. }
  466. }
  467. } // namespace detail
  468. template <typename Coll, typename Seq>
  469. struct tree_collector
  470. {
  471. public:
  472. BOOST_CONCEPT_ASSERT((BackInsertionSequence<Coll>));
  473. BOOST_CONCEPT_ASSERT((RandomAccessContainer<Seq>));
  474. BOOST_CONCEPT_ASSERT((CopyConstructible<Seq>));
  475. typedef typename Coll::value_type coll_value_type;
  476. typedef typename Seq::value_type seq_value_type;
  477. BOOST_STATIC_ASSERT((is_same<coll_value_type, Seq>::value));
  478. BOOST_STATIC_ASSERT((is_same<seq_value_type, bool>::value));
  479. tree_collector(Coll& seqs): mSeqs(seqs) { }
  480. inline void operator()(Seq seq)
  481. { mSeqs.push_back(seq); }
  482. private:
  483. Coll& mSeqs;
  484. };
  485. template <
  486. typename Graph,
  487. typename Order,
  488. typename Func,
  489. typename Seq
  490. >
  491. BOOST_CONCEPT_REQUIRES(
  492. ((RandomAccessContainer<Order>))
  493. ((IncidenceGraphConcept<Graph>))
  494. ((UnaryFunction<Func, void, Seq>))
  495. ((Mutable_RandomAccessContainer<Seq>))
  496. ((VertexAndEdgeListGraphConcept<Graph>)),
  497. (void)
  498. )
  499. two_graphs_common_spanning_trees
  500. (
  501. const Graph& iG,
  502. Order iG_map,
  503. const Graph& vG,
  504. Order vG_map,
  505. Func func,
  506. Seq inL
  507. )
  508. {
  509. typedef graph_traits<Graph> GraphTraits;
  510. typedef typename GraphTraits::directed_category directed_category;
  511. typedef typename GraphTraits::vertex_descriptor vertex_descriptor;
  512. typedef typename GraphTraits::edge_descriptor edge_descriptor;
  513. typedef typename GraphTraits::edges_size_type edges_size_type;
  514. typedef typename GraphTraits::edge_iterator edge_iterator;
  515. typedef typename Seq::value_type seq_value_type;
  516. typedef typename Seq::size_type seq_size_type;
  517. typedef typename Order::value_type order_value_type;
  518. typedef typename Order::size_type order_size_type;
  519. BOOST_STATIC_ASSERT((is_same<order_value_type, edge_descriptor>::value));
  520. BOOST_CONCEPT_ASSERT((Convertible<order_size_type, edges_size_type>));
  521. BOOST_CONCEPT_ASSERT((Convertible<seq_size_type, edges_size_type>));
  522. BOOST_STATIC_ASSERT((is_same<seq_value_type, bool>::value));
  523. BOOST_STATIC_ASSERT((is_same<directed_category, undirected_tag>::value));
  524. if(num_vertices(iG) != num_vertices(vG))
  525. return;
  526. if(inL.size() != num_edges(iG)
  527. || inL.size() != num_edges(vG))
  528. return;
  529. if(iG_map.size() != num_edges(iG)
  530. || vG_map.size() != num_edges(vG))
  531. return;
  532. typedef bimaps::bimap<
  533. bimaps::set_of< int >,
  534. bimaps::set_of< order_value_type >
  535. > bimap_type;
  536. typedef typename bimap_type::value_type bimap_value;
  537. bimap_type iG_bimap, vG_bimap;
  538. for(order_size_type i = 0; i < iG_map.size(); ++i)
  539. iG_bimap.insert(bimap_value(i, iG_map[i]));
  540. for(order_size_type i = 0; i < vG_map.size(); ++i)
  541. vG_bimap.insert(bimap_value(i, vG_map[i]));
  542. edge_iterator current, last;
  543. boost::tuples::tie(current, last) = edges(iG);
  544. for(; current != last; ++current)
  545. if(iG_bimap.right.find(*current) == iG_bimap.right.end())
  546. return;
  547. boost::tuples::tie(current, last) = edges(vG);
  548. for(; current != last; ++current)
  549. if(vG_bimap.right.find(*current) == vG_bimap.right.end())
  550. return;
  551. std::stack<edge_descriptor> iG_buf, vG_buf;
  552. std::map<vertex_descriptor, edge_descriptor> tree_map;
  553. std::map<vertex_descriptor, vertex_descriptor> pred_map;
  554. std::map<vertex_descriptor, int> dist_map, low_map;
  555. detail::bridges_visitor<
  556. associative_property_map<
  557. std::map<vertex_descriptor, edge_descriptor>
  558. >,
  559. associative_property_map<
  560. std::map<vertex_descriptor, vertex_descriptor>
  561. >,
  562. associative_property_map< std::map<vertex_descriptor, int> >,
  563. associative_property_map< std::map<vertex_descriptor, int> >,
  564. std::stack<edge_descriptor>
  565. >
  566. iG_vis(
  567. associative_property_map<
  568. std::map< vertex_descriptor, edge_descriptor> >(tree_map),
  569. associative_property_map<
  570. std::map< vertex_descriptor, vertex_descriptor> >(pred_map),
  571. associative_property_map<std::map< vertex_descriptor, int> >(dist_map),
  572. associative_property_map<std::map< vertex_descriptor, int> >(low_map),
  573. iG_buf
  574. ),
  575. vG_vis(
  576. associative_property_map<
  577. std::map< vertex_descriptor, edge_descriptor> >(tree_map),
  578. associative_property_map<
  579. std::map< vertex_descriptor, vertex_descriptor> >(pred_map),
  580. associative_property_map<std::map< vertex_descriptor, int> >(dist_map),
  581. associative_property_map<std::map< vertex_descriptor, int> >(low_map),
  582. vG_buf
  583. );
  584. std::map<vertex_descriptor, default_color_type> vertex_color;
  585. std::map<edge_descriptor, default_color_type> edge_color;
  586. undirected_dfs(iG, iG_vis,
  587. associative_property_map<
  588. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  589. associative_property_map<
  590. std::map<edge_descriptor, default_color_type> >(edge_color)
  591. );
  592. undirected_dfs(vG, vG_vis,
  593. associative_property_map<
  594. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  595. associative_property_map<
  596. std::map<edge_descriptor, default_color_type> >(edge_color)
  597. );
  598. while(!iG_buf.empty()) {
  599. inL[iG_bimap.right.at(iG_buf.top())] = true;
  600. iG_buf.pop();
  601. }
  602. while(!vG_buf.empty()) {
  603. inL[vG_bimap.right.at(vG_buf.top())] = true;
  604. vG_buf.pop();
  605. }
  606. std::map<edge_descriptor, bool> iG_inL, vG_inL;
  607. associative_property_map< std::map<edge_descriptor, bool> >
  608. aiG_inL(iG_inL), avG_inL(vG_inL);
  609. for(seq_size_type i = 0; i < inL.size(); ++i)
  610. {
  611. if(inL[i]) {
  612. put(aiG_inL, iG_bimap.left.at(i), true);
  613. put(avG_inL, vG_bimap.left.at(i), true);
  614. } else {
  615. put(aiG_inL, iG_bimap.left.at(i), false);
  616. put(avG_inL, vG_bimap.left.at(i), false);
  617. }
  618. }
  619. undirected_dfs(
  620. make_filtered_graph(iG,
  621. detail::inL_edge_status< associative_property_map<
  622. std::map<edge_descriptor, bool> > >(aiG_inL)),
  623. make_dfs_visitor(
  624. detail::cycle_finder< std::stack<edge_descriptor> > (&iG_buf)),
  625. associative_property_map<
  626. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  627. associative_property_map<
  628. std::map<edge_descriptor, default_color_type> >(edge_color)
  629. );
  630. undirected_dfs(
  631. make_filtered_graph(vG,
  632. detail::inL_edge_status< associative_property_map<
  633. std::map<edge_descriptor, bool> > >(avG_inL)),
  634. make_dfs_visitor(
  635. detail::cycle_finder< std::stack<edge_descriptor> > (&vG_buf)),
  636. associative_property_map<
  637. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  638. associative_property_map<
  639. std::map<edge_descriptor, default_color_type> >(edge_color)
  640. );
  641. if(iG_buf.empty() && vG_buf.empty()) {
  642. std::map<edge_descriptor, bool> iG_deleted, vG_deleted;
  643. associative_property_map< std::map<edge_descriptor, bool> > diG(iG_deleted);
  644. associative_property_map< std::map<edge_descriptor, bool> > dvG(vG_deleted);
  645. boost::tuples::tie(current, last) = edges(iG);
  646. for(; current != last; ++current)
  647. put(diG, *current, false);
  648. boost::tuples::tie(current, last) = edges(vG);
  649. for(; current != last; ++current)
  650. put(dvG, *current, false);
  651. for(seq_size_type j = 0; j < inL.size(); ++j) {
  652. if(!inL[j]) {
  653. put(aiG_inL, iG_bimap.left.at(j), true);
  654. put(avG_inL, vG_bimap.left.at(j), true);
  655. undirected_dfs(
  656. make_filtered_graph(iG,
  657. detail::inL_edge_status< associative_property_map<
  658. std::map<edge_descriptor, bool> > >(aiG_inL)),
  659. make_dfs_visitor(
  660. detail::cycle_finder< std::stack<edge_descriptor> > (&iG_buf)),
  661. associative_property_map<
  662. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  663. associative_property_map<
  664. std::map<edge_descriptor, default_color_type> >(edge_color)
  665. );
  666. undirected_dfs(
  667. make_filtered_graph(vG,
  668. detail::inL_edge_status< associative_property_map<
  669. std::map<edge_descriptor, bool> > >(avG_inL)),
  670. make_dfs_visitor(
  671. detail::cycle_finder< std::stack<edge_descriptor> > (&vG_buf)),
  672. associative_property_map<
  673. std::map<vertex_descriptor, default_color_type> >(vertex_color),
  674. associative_property_map<
  675. std::map<edge_descriptor, default_color_type> >(edge_color)
  676. );
  677. if(!iG_buf.empty() || !vG_buf.empty()) {
  678. while(!iG_buf.empty()) iG_buf.pop();
  679. while(!vG_buf.empty()) vG_buf.pop();
  680. put(diG, iG_bimap.left.at(j), true);
  681. put(dvG, vG_bimap.left.at(j), true);
  682. }
  683. put(aiG_inL, iG_bimap.left.at(j), false);
  684. put(avG_inL, vG_bimap.left.at(j), false);
  685. }
  686. }
  687. int cc = 0;
  688. std::map<vertex_descriptor, int> com_map;
  689. cc += connected_components(
  690. make_filtered_graph(iG,
  691. detail::deleted_edge_status<associative_property_map<
  692. std::map<edge_descriptor, bool> > >(diG)),
  693. associative_property_map<std::map<vertex_descriptor, int> >(com_map)
  694. );
  695. cc += connected_components(
  696. make_filtered_graph(vG,
  697. detail::deleted_edge_status<associative_property_map<
  698. std::map<edge_descriptor, bool> > >(dvG)),
  699. associative_property_map< std::map<vertex_descriptor, int> >(com_map)
  700. );
  701. if(cc != 2)
  702. return;
  703. // REC
  704. detail::rec_two_graphs_common_spanning_trees<Graph, Func, Seq,
  705. associative_property_map< std::map<edge_descriptor, bool> > >
  706. (iG, iG_bimap, aiG_inL, diG, vG, vG_bimap, aiG_inL, dvG, func, inL);
  707. }
  708. }
  709. template <
  710. typename Graph,
  711. typename Func,
  712. typename Seq
  713. >
  714. BOOST_CONCEPT_REQUIRES(
  715. ((IncidenceGraphConcept<Graph>))
  716. ((EdgeListGraphConcept<Graph>)),
  717. (void)
  718. )
  719. two_graphs_common_spanning_trees
  720. (
  721. const Graph& iG,
  722. const Graph& vG,
  723. Func func,
  724. Seq inL
  725. )
  726. {
  727. typedef graph_traits<Graph> GraphTraits;
  728. typedef typename GraphTraits::edge_descriptor edge_descriptor;
  729. typedef typename GraphTraits::edge_iterator edge_iterator;
  730. std::vector<edge_descriptor> iGO, vGO;
  731. edge_iterator curr, last;
  732. boost::tuples::tie(curr, last) = edges(iG);
  733. for(; curr != last; ++curr)
  734. iGO.push_back(*curr);
  735. boost::tuples::tie(curr, last) = edges(vG);
  736. for(; curr != last; ++curr)
  737. vGO.push_back(*curr);
  738. two_graphs_common_spanning_trees(iG, iGO, vG, vGO, func, inL);
  739. }
  740. } // namespace boost
  741. #endif // BOOST_GRAPH_TWO_GRAPHS_COMMON_SPANNING_TREES_HPP