transitive_closure.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 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. int 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. typename std::vector< cg_vertex >::iterator di
  97. = std::unique(adj.begin(), adj.end());
  98. if (di != adj.end())
  99. adj.erase(di, adj.end());
  100. for (typename std::vector< cg_vertex >::const_iterator i = adj.begin();
  101. i != adj.end(); ++i)
  102. {
  103. add_edge(s, *i, CG);
  104. }
  105. }
  106. std::vector< cg_vertex > topo_order;
  107. std::vector< cg_vertex > topo_number(num_vertices(CG));
  108. topological_sort(CG, std::back_inserter(topo_order),
  109. vertex_index_map(identity_property_map()));
  110. std::reverse(topo_order.begin(), topo_order.end());
  111. size_type n = 0;
  112. for (typename std::vector< cg_vertex >::iterator iter = topo_order.begin();
  113. iter != topo_order.end(); ++iter)
  114. topo_number[*iter] = n++;
  115. std::vector< std::vector< cg_vertex > > CG_vec(num_vertices(CG));
  116. for (size_type i = 0; i < num_vertices(CG); ++i)
  117. {
  118. typedef typename boost::graph_traits< CG_t >::adjacency_iterator
  119. 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. {
  133. cg_vertex v = *i;
  134. if (!in_a_chain[v])
  135. {
  136. chains.resize(chains.size() + 1);
  137. std::vector< cg_vertex >& chain = chains.back();
  138. for (;;)
  139. {
  140. chain.push_back(v);
  141. in_a_chain[v] = true;
  142. typename std::vector< cg_vertex >::const_iterator next
  143. = std::find_if(CG_vec[v].begin(), CG_vec[v].end(),
  144. std::not1(detail::subscript(in_a_chain)));
  145. if (next != CG_vec[v].end())
  146. v = *next;
  147. else
  148. break; // end of chain, dead-end
  149. }
  150. }
  151. }
  152. }
  153. std::vector< size_type > chain_number(CG_vec.size());
  154. std::vector< size_type > pos_in_chain(CG_vec.size());
  155. for (size_type i = 0; i < chains.size(); ++i)
  156. for (size_type j = 0; j < chains[i].size(); ++j)
  157. {
  158. cg_vertex v = chains[i][j];
  159. chain_number[v] = i;
  160. pos_in_chain[v] = j;
  161. }
  162. cg_vertex inf = (std::numeric_limits< cg_vertex >::max)();
  163. std::vector< std::vector< cg_vertex > > successors(
  164. CG_vec.size(), std::vector< cg_vertex >(chains.size(), inf));
  165. for (typename std::vector< cg_vertex >::reverse_iterator i
  166. = topo_order.rbegin();
  167. i != topo_order.rend(); ++i)
  168. {
  169. cg_vertex u = *i;
  170. typename std::vector< cg_vertex >::const_iterator adj, adj_last;
  171. for (adj = CG_vec[u].begin(), adj_last = CG_vec[u].end();
  172. adj != adj_last; ++adj)
  173. {
  174. cg_vertex v = *adj;
  175. if (topo_number[v] < successors[u][chain_number[v]])
  176. {
  177. // Succ(u) = Succ(u) U Succ(v)
  178. detail::union_successor_sets(
  179. successors[u], successors[v], successors[u]);
  180. // Succ(u) = Succ(u) U {v}
  181. successors[u][chain_number[v]] = topo_number[v];
  182. }
  183. }
  184. }
  185. for (size_type i = 0; i < CG_vec.size(); ++i)
  186. CG_vec[i].clear();
  187. for (size_type i = 0; i < CG_vec.size(); ++i)
  188. for (size_type j = 0; j < chains.size(); ++j)
  189. {
  190. size_type topo_num = successors[i][j];
  191. if (topo_num < inf)
  192. {
  193. cg_vertex v = topo_order[topo_num];
  194. for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k)
  195. CG_vec[i].push_back(chains[j][k]);
  196. }
  197. }
  198. // Add vertices to the transitive closure graph
  199. {
  200. vertex_iterator i, i_end;
  201. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  202. g_to_tc_map[*i] = add_vertex(tc);
  203. }
  204. // Add edges between all the vertices in two adjacent SCCs
  205. typename std::vector< std::vector< cg_vertex > >::const_iterator si, si_end;
  206. for (si = CG_vec.begin(), si_end = CG_vec.end(); si != si_end; ++si)
  207. {
  208. cg_vertex s = si - CG_vec.begin();
  209. typename std::vector< cg_vertex >::const_iterator i, i_end;
  210. for (i = CG_vec[s].begin(), i_end = CG_vec[s].end(); i != i_end; ++i)
  211. {
  212. cg_vertex t = *i;
  213. for (size_type k = 0; k < components[s].size(); ++k)
  214. for (size_type l = 0; l < components[t].size(); ++l)
  215. add_edge(g_to_tc_map[components[s][k]],
  216. g_to_tc_map[components[t][l]], tc);
  217. }
  218. }
  219. // Add edges connecting all vertices in a SCC
  220. for (size_type i = 0; i < components.size(); ++i)
  221. if (components[i].size() > 1)
  222. for (size_type k = 0; k < components[i].size(); ++k)
  223. for (size_type l = 0; l < components[i].size(); ++l)
  224. {
  225. vertex u = components[i][k], v = components[i][l];
  226. add_edge(g_to_tc_map[u], g_to_tc_map[v], tc);
  227. }
  228. // Find loopbacks in the original graph.
  229. // Need to add it to transitive closure.
  230. {
  231. vertex_iterator i, i_end;
  232. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  233. {
  234. adjacency_iterator ab, ae;
  235. for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab)
  236. {
  237. if (*ab == *i)
  238. if (components[component_number[*i]].size() == 1)
  239. add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc);
  240. }
  241. }
  242. }
  243. }
  244. template < typename Graph, typename GraphTC >
  245. void transitive_closure(const Graph& g, GraphTC& tc)
  246. {
  247. if (num_vertices(g) == 0)
  248. return;
  249. typedef typename property_map< Graph, vertex_index_t >::const_type
  250. VertexIndexMap;
  251. VertexIndexMap index_map = get(vertex_index, g);
  252. typedef typename graph_traits< GraphTC >::vertex_descriptor tc_vertex;
  253. std::vector< tc_vertex > to_tc_vec(num_vertices(g));
  254. iterator_property_map< tc_vertex*, VertexIndexMap, tc_vertex, tc_vertex& >
  255. g_to_tc_map(&to_tc_vec[0], index_map);
  256. transitive_closure(g, tc, g_to_tc_map, index_map);
  257. }
  258. namespace detail
  259. {
  260. template < typename Graph, typename GraphTC, typename G_to_TC_VertexMap,
  261. typename VertexIndexMap >
  262. void transitive_closure_dispatch(const Graph& g, GraphTC& tc,
  263. G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map)
  264. {
  265. typedef typename graph_traits< GraphTC >::vertex_descriptor tc_vertex;
  266. typename std::vector< tc_vertex >::size_type n
  267. = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
  268. std::vector< tc_vertex > to_tc_vec(n);
  269. transitive_closure(g, tc,
  270. choose_param(g_to_tc_map,
  271. make_iterator_property_map(
  272. to_tc_vec.begin(), index_map, to_tc_vec[0])),
  273. index_map);
  274. }
  275. } // namespace detail
  276. template < typename Graph, typename GraphTC, typename P, typename T,
  277. typename R >
  278. void transitive_closure(
  279. const Graph& g, GraphTC& tc, const bgl_named_params< P, T, R >& params)
  280. {
  281. if (num_vertices(g) == 0)
  282. return;
  283. detail::transitive_closure_dispatch(g, tc,
  284. get_param(params, orig_to_copy_t()),
  285. choose_const_pmap(get_param(params, vertex_index), g, vertex_index));
  286. }
  287. template < typename G > void warshall_transitive_closure(G& g)
  288. {
  289. typedef typename graph_traits< G >::vertex_iterator vertex_iterator;
  290. BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< G >));
  291. BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept< G >));
  292. // Matrix form:
  293. // for k
  294. // for i
  295. // if A[i,k]
  296. // for j
  297. // A[i,j] = A[i,j] | A[k,j]
  298. vertex_iterator ki, ke, ii, ie, ji, je;
  299. for (boost::tie(ki, ke) = vertices(g); ki != ke; ++ki)
  300. for (boost::tie(ii, ie) = vertices(g); ii != ie; ++ii)
  301. if (edge(*ii, *ki, g).second)
  302. for (boost::tie(ji, je) = vertices(g); ji != je; ++ji)
  303. if (!edge(*ii, *ji, g).second && edge(*ki, *ji, g).second)
  304. {
  305. add_edge(*ii, *ji, g);
  306. }
  307. }
  308. template < typename G > void warren_transitive_closure(G& g)
  309. {
  310. using namespace boost;
  311. typedef typename graph_traits< G >::vertex_iterator vertex_iterator;
  312. BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< G >));
  313. BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept< G >));
  314. // Make sure second loop will work
  315. if (num_vertices(g) == 0)
  316. return;
  317. // for i = 2 to n
  318. // for k = 1 to i - 1
  319. // if A[i,k]
  320. // for j = 1 to n
  321. // A[i,j] = A[i,j] | A[k,j]
  322. vertex_iterator ic, ie, jc, je, kc, ke;
  323. for (boost::tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic)
  324. for (boost::tie(kc, ke) = vertices(g); *kc != *ic; ++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. {
  329. add_edge(*ic, *jc, g);
  330. }
  331. // for i = 1 to n - 1
  332. // for k = i + 1 to n
  333. // if A[i,k]
  334. // for j = 1 to n
  335. // A[i,j] = A[i,j] | A[k,j]
  336. for (boost::tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic)
  337. for (kc = ic, ke = ie, ++kc; kc != ke; ++kc)
  338. if (edge(*ic, *kc, g).second)
  339. for (boost::tie(jc, je) = vertices(g); jc != je; ++jc)
  340. if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second)
  341. {
  342. add_edge(*ic, *jc, g);
  343. }
  344. }
  345. } // namespace boost
  346. #endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP