transitive_closure.hpp 14 KB

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