two_graphs_common_spanning_trees.hpp 34 KB

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