/Src/Dependencies/Boost/boost/graph/transitive_closure.hpp

http://hadesmem.googlecode.com/ · C++ Header · 370 lines · 298 code · 42 blank · 30 comment · 85 complexity · ca2194903a64459ef79cb0639e340eb3 MD5 · raw file

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