transitive_closure.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright (C) 2001 Vladimir Prus <ghost@cs.msu.su>
  2. // Copyright (C) 2001 Jeremy Siek <jsiek@cs.indiana.edu>
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // NOTE: this final is generated by libs/graph/doc/transitive_closure.w
  7. #ifndef BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
  8. #define BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
  9. #include <vector>
  10. #include <algorithm> // for std::min and std::max
  11. #include <functional>
  12. #include <boost/config.hpp>
  13. #include <boost/bind.hpp>
  14. #include <boost/graph/strong_components.hpp>
  15. #include <boost/graph/topological_sort.hpp>
  16. #include <boost/graph/graph_concepts.hpp>
  17. #include <boost/graph/named_function_params.hpp>
  18. #include <boost/graph/adjacency_list.hpp>
  19. #include <boost/concept/assert.hpp>
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. inline void
  25. union_successor_sets(const std::vector < std::size_t > &s1,
  26. const std::vector < std::size_t > &s2,
  27. std::vector < std::size_t > &s3)
  28. {
  29. BOOST_USING_STD_MIN();
  30. for (std::size_t k = 0; k < s1.size(); ++k)
  31. s3[k] = min BOOST_PREVENT_MACRO_SUBSTITUTION(s1[k], s2[k]);
  32. }
  33. } // namespace detail
  34. namespace detail
  35. {
  36. template < typename TheContainer, typename ST = std::size_t,
  37. typename VT = typename TheContainer::value_type >
  38. struct subscript_t
  39. {
  40. typedef ST argument_type;
  41. typedef VT& result_type;
  42. subscript_t(TheContainer & c):container(&c)
  43. {
  44. }
  45. VT & operator() (const ST & i) const
  46. {
  47. return (*container)[i];
  48. }
  49. protected:
  50. TheContainer * container;
  51. };
  52. template < typename TheContainer >
  53. subscript_t < TheContainer > subscript(TheContainer & c) {
  54. return subscript_t < TheContainer > (c);
  55. }
  56. } // namespace detail
  57. template < typename Graph, typename GraphTC,
  58. typename G_to_TC_VertexMap,
  59. typename VertexIndexMap >
  60. void transitive_closure(const Graph & g, GraphTC & tc,
  61. G_to_TC_VertexMap g_to_tc_map,
  62. VertexIndexMap index_map)
  63. {
  64. if (num_vertices(g) == 0)
  65. return;
  66. typedef typename graph_traits < Graph >::vertex_descriptor vertex;
  67. typedef typename graph_traits < Graph >::vertex_iterator vertex_iterator;
  68. typedef typename property_traits < VertexIndexMap >::value_type size_type;
  69. typedef typename graph_traits <
  70. Graph >::adjacency_iterator adjacency_iterator;
  71. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept < Graph > ));
  72. BOOST_CONCEPT_ASSERT(( AdjacencyGraphConcept < Graph > ));
  73. BOOST_CONCEPT_ASSERT(( VertexMutableGraphConcept < GraphTC > ));
  74. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < GraphTC > ));
  75. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept < VertexIndexMap,
  76. vertex > ));
  77. typedef size_type cg_vertex;
  78. std::vector < cg_vertex > component_number_vec(num_vertices(g));
  79. iterator_property_map < cg_vertex *, VertexIndexMap, cg_vertex, cg_vertex& >
  80. component_number(&component_number_vec[0], index_map);
  81. int num_scc = strong_components(g, component_number,
  82. vertex_index_map(index_map));
  83. std::vector < std::vector < vertex > >components;
  84. build_component_lists(g, num_scc, component_number, components);
  85. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> CG_t;
  86. CG_t CG(num_scc);
  87. for (cg_vertex s = 0; s < components.size(); ++s) {
  88. std::vector < cg_vertex > adj;
  89. for (size_type i = 0; i < components[s].size(); ++i) {
  90. vertex u = components[s][i];
  91. adjacency_iterator v, v_end;
  92. for (boost::tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
  93. cg_vertex t = component_number[*v];
  94. if (s != t) // Avoid loops in the condensation graph
  95. adj.push_back(t);
  96. }
  97. }
  98. std::sort(adj.begin(), adj.end());
  99. typename std::vector<cg_vertex>::iterator di =
  100. std::unique(adj.begin(), adj.end());
  101. if (di != adj.end())
  102. adj.erase(di, adj.end());
  103. for (typename std::vector<cg_vertex>::const_iterator i = adj.begin();
  104. i != adj.end(); ++i) {
  105. add_edge(s, *i, CG);
  106. }
  107. }
  108. std::vector<cg_vertex> topo_order;
  109. std::vector<cg_vertex> topo_number(num_vertices(CG));
  110. topological_sort(CG, std::back_inserter(topo_order),
  111. vertex_index_map(identity_property_map()));
  112. std::reverse(topo_order.begin(), topo_order.end());
  113. size_type n = 0;
  114. for (typename std::vector<cg_vertex>::iterator iter = topo_order.begin();
  115. iter != topo_order.end(); ++iter)
  116. topo_number[*iter] = n++;
  117. std::vector<std::vector<cg_vertex> > CG_vec(num_vertices(CG));
  118. for (size_type i = 0; i < num_vertices(CG); ++i) {
  119. typedef typename boost::graph_traits<CG_t>::adjacency_iterator cg_adj_iter;
  120. std::pair<cg_adj_iter, cg_adj_iter> pr = adjacent_vertices(i, CG);
  121. CG_vec[i].assign(pr.first, pr.second);
  122. std::sort(CG_vec[i].begin(), CG_vec[i].end(),
  123. boost::bind(std::less<cg_vertex>(),
  124. boost::bind(detail::subscript(topo_number), _1),
  125. boost::bind(detail::subscript(topo_number), _2)));
  126. }
  127. std::vector<std::vector<cg_vertex> > chains;
  128. {
  129. std::vector<cg_vertex> in_a_chain(CG_vec.size());
  130. for (typename std::vector<cg_vertex>::iterator i = topo_order.begin();
  131. i != topo_order.end(); ++i) {
  132. cg_vertex v = *i;
  133. if (!in_a_chain[v]) {
  134. chains.resize(chains.size() + 1);
  135. std::vector<cg_vertex>& chain = chains.back();
  136. for (;;) {
  137. chain.push_back(v);
  138. in_a_chain[v] = true;
  139. typename std::vector<cg_vertex>::const_iterator next
  140. = std::find_if(CG_vec[v].begin(), CG_vec[v].end(),
  141. std::not1(detail::subscript(in_a_chain)));
  142. if (next != CG_vec[v].end())
  143. v = *next;
  144. else
  145. break; // end of chain, dead-end
  146. }
  147. }
  148. }
  149. }
  150. std::vector<size_type> chain_number(CG_vec.size());
  151. std::vector<size_type> pos_in_chain(CG_vec.size());
  152. for (size_type i = 0; i < chains.size(); ++i)
  153. for (size_type j = 0; j < chains[i].size(); ++j) {
  154. cg_vertex v = chains[i][j];
  155. chain_number[v] = i;
  156. pos_in_chain[v] = j;
  157. }
  158. cg_vertex inf = (std::numeric_limits< cg_vertex >::max)();
  159. std::vector<std::vector<cg_vertex> > successors(CG_vec.size(),
  160. std::vector<cg_vertex>
  161. (chains.size(), inf));
  162. for (typename std::vector<cg_vertex>::reverse_iterator
  163. i = topo_order.rbegin(); i != topo_order.rend(); ++i) {
  164. cg_vertex u = *i;
  165. typename std::vector<cg_vertex>::const_iterator adj, adj_last;
  166. for (adj = CG_vec[u].begin(), adj_last = CG_vec[u].end();
  167. adj != adj_last; ++adj) {
  168. cg_vertex v = *adj;
  169. if (topo_number[v] < successors[u][chain_number[v]]) {
  170. // Succ(u) = Succ(u) U Succ(v)
  171. detail::union_successor_sets(successors[u], successors[v],
  172. successors[u]);
  173. // Succ(u) = Succ(u) U {v}
  174. successors[u][chain_number[v]] = topo_number[v];
  175. }
  176. }
  177. }
  178. for (size_type i = 0; i < CG_vec.size(); ++i)
  179. CG_vec[i].clear();
  180. for (size_type i = 0; i < CG_vec.size(); ++i)
  181. for (size_type j = 0; j < chains.size(); ++j) {
  182. size_type topo_num = successors[i][j];
  183. if (topo_num < inf) {
  184. cg_vertex v = topo_order[topo_num];
  185. for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k)
  186. CG_vec[i].push_back(chains[j][k]);
  187. }
  188. }
  189. // Add vertices to the transitive closure graph
  190. {
  191. vertex_iterator i, i_end;
  192. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  193. g_to_tc_map[*i] = add_vertex(tc);
  194. }
  195. // Add edges between all the vertices in two adjacent SCCs
  196. typename std::vector<std::vector<cg_vertex> >::const_iterator si, si_end;
  197. for (si = CG_vec.begin(), si_end = CG_vec.end(); si != si_end; ++si) {
  198. cg_vertex s = si - CG_vec.begin();
  199. typename std::vector<cg_vertex>::const_iterator i, i_end;
  200. for (i = CG_vec[s].begin(), i_end = CG_vec[s].end(); i != i_end; ++i) {
  201. cg_vertex t = *i;
  202. for (size_type k = 0; k < components[s].size(); ++k)
  203. for (size_type l = 0; l < components[t].size(); ++l)
  204. add_edge(g_to_tc_map[components[s][k]],
  205. g_to_tc_map[components[t][l]], tc);
  206. }
  207. }
  208. // Add edges connecting all vertices in a SCC
  209. for (size_type i = 0; i < components.size(); ++i)
  210. if (components[i].size() > 1)
  211. for (size_type k = 0; k < components[i].size(); ++k)
  212. for (size_type l = 0; l < components[i].size(); ++l) {
  213. vertex u = components[i][k], v = components[i][l];
  214. add_edge(g_to_tc_map[u], g_to_tc_map[v], tc);
  215. }
  216. // Find loopbacks in the original graph.
  217. // Need to add it to transitive closure.
  218. {
  219. vertex_iterator i, i_end;
  220. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  221. {
  222. adjacency_iterator ab, ae;
  223. for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab)
  224. {
  225. if (*ab == *i)
  226. if (components[component_number[*i]].size() == 1)
  227. add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc);
  228. }
  229. }
  230. }
  231. }
  232. template <typename Graph, typename GraphTC>
  233. void transitive_closure(const Graph & g, GraphTC & tc)
  234. {
  235. if (num_vertices(g) == 0)
  236. return;
  237. typedef typename property_map<Graph, vertex_index_t>::const_type
  238. VertexIndexMap;
  239. VertexIndexMap index_map = get(vertex_index, g);
  240. typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
  241. std::vector<tc_vertex> to_tc_vec(num_vertices(g));
  242. iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&>
  243. g_to_tc_map(&to_tc_vec[0], index_map);
  244. transitive_closure(g, tc, g_to_tc_map, index_map);
  245. }
  246. namespace detail
  247. {
  248. template < typename Graph, typename GraphTC, typename G_to_TC_VertexMap,
  249. typename VertexIndexMap>
  250. void transitive_closure_dispatch
  251. (const Graph & g, GraphTC & tc,
  252. G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map)
  253. {
  254. typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex;
  255. typename std::vector < tc_vertex >::size_type
  256. n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
  257. std::vector < tc_vertex > to_tc_vec(n);
  258. transitive_closure
  259. (g, tc,
  260. choose_param(g_to_tc_map, make_iterator_property_map
  261. (to_tc_vec.begin(), index_map, to_tc_vec[0])),
  262. index_map);
  263. }
  264. } // namespace detail
  265. template < typename Graph, typename GraphTC,
  266. typename P, typename T, typename R >
  267. void transitive_closure(const Graph & g, GraphTC & tc,
  268. const bgl_named_params < P, T, R > &params)
  269. {
  270. if (num_vertices(g) == 0)
  271. return;
  272. detail::transitive_closure_dispatch
  273. (g, tc, get_param(params, orig_to_copy_t()),
  274. choose_const_pmap(get_param(params, vertex_index), g, vertex_index) );
  275. }
  276. template < typename G > void warshall_transitive_closure(G & g)
  277. {
  278. typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
  279. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept < G > ));
  280. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < G > ));
  281. // Matrix form:
  282. // for k
  283. // for i
  284. // if A[i,k]
  285. // for j
  286. // A[i,j] = A[i,j] | A[k,j]
  287. vertex_iterator ki, ke, ii, ie, ji, je;
  288. for (boost::tie(ki, ke) = vertices(g); ki != ke; ++ki)
  289. for (boost::tie(ii, ie) = vertices(g); ii != ie; ++ii)
  290. if (edge(*ii, *ki, g).second)
  291. for (boost::tie(ji, je) = vertices(g); ji != je; ++ji)
  292. if (!edge(*ii, *ji, g).second && edge(*ki, *ji, g).second) {
  293. add_edge(*ii, *ji, g);
  294. }
  295. }
  296. template < typename G > void warren_transitive_closure(G & g)
  297. {
  298. using namespace boost;
  299. typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
  300. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept < G > ));
  301. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < G > ));
  302. // Make sure second loop will work
  303. if (num_vertices(g) == 0)
  304. return;
  305. // for i = 2 to n
  306. // for k = 1 to i - 1
  307. // if A[i,k]
  308. // for j = 1 to n
  309. // A[i,j] = A[i,j] | A[k,j]
  310. vertex_iterator ic, ie, jc, je, kc, ke;
  311. for (boost::tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic)
  312. for (boost::tie(kc, ke) = vertices(g); *kc != *ic; ++kc)
  313. if (edge(*ic, *kc, g).second)
  314. for (boost::tie(jc, je) = vertices(g); jc != je; ++jc)
  315. if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
  316. add_edge(*ic, *jc, g);
  317. }
  318. // for i = 1 to n - 1
  319. // for k = i + 1 to n
  320. // if A[i,k]
  321. // for j = 1 to n
  322. // A[i,j] = A[i,j] | A[k,j]
  323. for (boost::tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic)
  324. for (kc = ic, ke = ie, ++kc; kc != ke; ++kc)
  325. if (edge(*ic, *kc, g).second)
  326. for (boost::tie(jc, je) = vertices(g); jc != je; ++jc)
  327. if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
  328. add_edge(*ic, *jc, g);
  329. }
  330. }
  331. } // namespace boost
  332. #endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP