hawick_circuits.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright Louis Dionne 2013
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
  4. // at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_GRAPH_HAWICK_CIRCUITS_HPP
  6. #define BOOST_GRAPH_HAWICK_CIRCUITS_HPP
  7. #include <algorithm>
  8. #include <boost/assert.hpp>
  9. #include <boost/foreach.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/graph/one_bit_color_map.hpp>
  12. #include <boost/graph/properties.hpp>
  13. #include <boost/move/utility.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/end.hpp>
  17. #include <boost/range/iterator.hpp>
  18. #include <boost/tuple/tuple.hpp> // for boost::tie
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/utility/result_of.hpp>
  21. #include <set>
  22. #include <utility> // for std::pair
  23. #include <vector>
  24. namespace boost
  25. {
  26. namespace hawick_circuits_detail
  27. {
  28. //! @internal Functor returning all the vertices adjacent to a vertex.
  29. struct get_all_adjacent_vertices
  30. {
  31. template < typename Sig > struct result;
  32. template < typename This, typename Vertex, typename Graph >
  33. struct result< This(Vertex, Graph) >
  34. {
  35. private:
  36. typedef typename remove_reference< Graph >::type RawGraph;
  37. typedef graph_traits< RawGraph > Traits;
  38. typedef typename Traits::adjacency_iterator AdjacencyIterator;
  39. public:
  40. typedef std::pair< AdjacencyIterator, AdjacencyIterator > type;
  41. };
  42. template < typename Vertex, typename Graph >
  43. typename result< get_all_adjacent_vertices(
  44. BOOST_FWD_REF(Vertex), BOOST_FWD_REF(Graph)) >::type
  45. operator()(BOOST_FWD_REF(Vertex) v, BOOST_FWD_REF(Graph) g) const
  46. {
  47. return adjacent_vertices(
  48. boost::forward< Vertex >(v), boost::forward< Graph >(g));
  49. }
  50. };
  51. //! @internal Functor returning a set of the vertices adjacent to a vertex.
  52. struct get_unique_adjacent_vertices
  53. {
  54. template < typename Sig > struct result;
  55. template < typename This, typename Vertex, typename Graph >
  56. struct result< This(Vertex, Graph) >
  57. {
  58. typedef std::set< typename remove_reference< Vertex >::type > type;
  59. };
  60. template < typename Vertex, typename Graph >
  61. typename result< get_unique_adjacent_vertices(
  62. Vertex, Graph const&) >::type
  63. operator()(Vertex v, Graph const& g) const
  64. {
  65. typedef typename result< get_unique_adjacent_vertices(
  66. Vertex, Graph const&) >::type Set;
  67. return Set(
  68. adjacent_vertices(v, g).first, adjacent_vertices(v, g).second);
  69. }
  70. };
  71. //! @internal
  72. //! Return whether a container contains a given value.
  73. //! This is not meant as a general purpose membership testing function; it
  74. //! would have to be more clever about possible optimizations.
  75. template < typename Container, typename Value >
  76. bool contains(Container const& c, Value const& v)
  77. {
  78. return std::find(boost::begin(c), boost::end(c), v) != boost::end(c);
  79. }
  80. /*!
  81. * @internal
  82. * Algorithm finding all the cycles starting from a given vertex.
  83. *
  84. * The search is only done in the subgraph induced by the starting vertex
  85. * and the vertices with an index higher than the starting vertex.
  86. */
  87. template < typename Graph, typename Visitor, typename VertexIndexMap,
  88. typename Stack, typename ClosedMatrix, typename GetAdjacentVertices >
  89. struct hawick_circuits_from
  90. {
  91. private:
  92. typedef graph_traits< Graph > Traits;
  93. typedef typename Traits::vertex_descriptor Vertex;
  94. typedef typename Traits::edge_descriptor Edge;
  95. typedef typename Traits::vertices_size_type VerticesSize;
  96. typedef
  97. typename property_traits< VertexIndexMap >::value_type VertexIndex;
  98. typedef typename result_of< GetAdjacentVertices(
  99. Vertex, Graph const&) >::type AdjacentVertices;
  100. typedef typename range_iterator< AdjacentVertices const >::type
  101. AdjacencyIterator;
  102. // The one_bit_color_map starts all white, i.e. not blocked.
  103. // Since we make that assumption (I looked at the implementation, but
  104. // I can't find anything that documents this behavior), we're gonna
  105. // assert it in the constructor.
  106. typedef one_bit_color_map< VertexIndexMap > BlockedMap;
  107. typedef typename property_traits< BlockedMap >::value_type BlockedColor;
  108. static BlockedColor blocked_false_color()
  109. {
  110. return color_traits< BlockedColor >::white();
  111. }
  112. static BlockedColor blocked_true_color()
  113. {
  114. return color_traits< BlockedColor >::black();
  115. }
  116. // This is used by the constructor to secure the assumption
  117. // documented above.
  118. bool blocked_map_starts_all_unblocked() const
  119. {
  120. BOOST_FOREACH (Vertex v, vertices(graph_))
  121. if (is_blocked(v))
  122. return false;
  123. return true;
  124. }
  125. // This is only used in the constructor to make sure the optimization of
  126. // sharing data structures between iterations does not break the code.
  127. bool all_closed_rows_are_empty() const
  128. {
  129. BOOST_FOREACH (typename ClosedMatrix::reference row, closed_)
  130. if (!row.empty())
  131. return false;
  132. return true;
  133. }
  134. public:
  135. hawick_circuits_from(Graph const& graph, Visitor& visitor,
  136. VertexIndexMap const& vim, Stack& stack, ClosedMatrix& closed,
  137. VerticesSize n_vertices)
  138. : graph_(graph)
  139. , visitor_(visitor)
  140. , vim_(vim)
  141. , stack_(stack)
  142. , closed_(closed)
  143. , blocked_(n_vertices, vim_)
  144. {
  145. BOOST_ASSERT(blocked_map_starts_all_unblocked());
  146. // Since sharing the data structures between iterations is
  147. // just an optimization, it must always be equivalent to
  148. // constructing new ones in this constructor.
  149. BOOST_ASSERT(stack_.empty());
  150. BOOST_ASSERT(closed_.size() == n_vertices);
  151. BOOST_ASSERT(all_closed_rows_are_empty());
  152. }
  153. private:
  154. //! @internal Return the index of a given vertex.
  155. VertexIndex index_of(Vertex v) const { return get(vim_, v); }
  156. //! @internal Return whether a vertex `v` is closed to a vertex `u`.
  157. bool is_closed_to(Vertex u, Vertex v) const
  158. {
  159. typedef typename ClosedMatrix::const_reference VertexList;
  160. VertexList closed_to_u = closed_[index_of(u)];
  161. return contains(closed_to_u, v);
  162. }
  163. //! @internal Close a vertex `v` to a vertex `u`.
  164. void close_to(Vertex u, Vertex v)
  165. {
  166. BOOST_ASSERT(!is_closed_to(u, v));
  167. closed_[index_of(u)].push_back(v);
  168. }
  169. //! @internal Return whether a given vertex is blocked.
  170. bool is_blocked(Vertex v) const
  171. {
  172. return get(blocked_, v) == blocked_true_color();
  173. }
  174. //! @internal Block a given vertex.
  175. void block(Vertex v) { put(blocked_, v, blocked_true_color()); }
  176. //! @internal Unblock a given vertex.
  177. void unblock(Vertex u)
  178. {
  179. typedef typename ClosedMatrix::reference VertexList;
  180. put(blocked_, u, blocked_false_color());
  181. VertexList closed_to_u = closed_[index_of(u)];
  182. while (!closed_to_u.empty())
  183. {
  184. Vertex const w = closed_to_u.back();
  185. closed_to_u.pop_back();
  186. if (is_blocked(w))
  187. unblock(w);
  188. }
  189. BOOST_ASSERT(closed_to_u.empty());
  190. }
  191. //! @internal Main procedure as described in the paper.
  192. bool circuit(Vertex start, Vertex v)
  193. {
  194. bool found_circuit = false;
  195. stack_.push_back(v);
  196. block(v);
  197. // Cache some values that are used more than once in the function.
  198. VertexIndex const index_of_start = index_of(start);
  199. AdjacentVertices const adj_vertices
  200. = GetAdjacentVertices()(v, graph_);
  201. AdjacencyIterator const w_end = boost::end(adj_vertices);
  202. for (AdjacencyIterator w_it = boost::begin(adj_vertices);
  203. w_it != w_end; ++w_it)
  204. {
  205. Vertex const w = *w_it;
  206. // Since we're only looking in the subgraph induced by `start`
  207. // and the vertices with an index higher than `start`, we skip
  208. // any vertex that does not satisfy that.
  209. if (index_of(w) < index_of_start)
  210. continue;
  211. // If the last vertex is equal to `start`, we have a circuit.
  212. else if (w == start)
  213. {
  214. // const_cast to ensure the visitor does not modify the
  215. // stack
  216. visitor_.cycle(const_cast< Stack const& >(stack_), graph_);
  217. found_circuit = true;
  218. }
  219. // If `w` is not blocked, we continue searching further down the
  220. // same path for a cycle with `w` in it.
  221. else if (!is_blocked(w) && circuit(start, w))
  222. found_circuit = true;
  223. }
  224. if (found_circuit)
  225. unblock(v);
  226. else
  227. for (AdjacencyIterator w_it = boost::begin(adj_vertices);
  228. w_it != w_end; ++w_it)
  229. {
  230. Vertex const w = *w_it;
  231. // Like above, we skip vertices that are not in the subgraph
  232. // we're considering.
  233. if (index_of(w) < index_of_start)
  234. continue;
  235. // If `v` is not closed to `w`, we make it so.
  236. if (!is_closed_to(w, v))
  237. close_to(w, v);
  238. }
  239. BOOST_ASSERT(v == stack_.back());
  240. stack_.pop_back();
  241. return found_circuit;
  242. }
  243. public:
  244. void operator()(Vertex start) { circuit(start, start); }
  245. private:
  246. Graph const& graph_;
  247. Visitor& visitor_;
  248. VertexIndexMap const& vim_;
  249. Stack& stack_;
  250. ClosedMatrix& closed_;
  251. BlockedMap blocked_;
  252. };
  253. template < typename GetAdjacentVertices, typename Graph, typename Visitor,
  254. typename VertexIndexMap >
  255. void call_hawick_circuits(Graph const& graph,
  256. Visitor /* by value */ visitor, VertexIndexMap const& vertex_index_map)
  257. {
  258. typedef graph_traits< Graph > Traits;
  259. typedef typename Traits::vertex_descriptor Vertex;
  260. typedef typename Traits::vertices_size_type VerticesSize;
  261. typedef typename Traits::vertex_iterator VertexIterator;
  262. typedef std::vector< Vertex > Stack;
  263. typedef std::vector< std::vector< Vertex > > ClosedMatrix;
  264. typedef hawick_circuits_from< Graph, Visitor, VertexIndexMap, Stack,
  265. ClosedMatrix, GetAdjacentVertices >
  266. SubAlgorithm;
  267. VerticesSize const n_vertices = num_vertices(graph);
  268. Stack stack;
  269. stack.reserve(n_vertices);
  270. ClosedMatrix closed(n_vertices);
  271. VertexIterator start, last;
  272. for (boost::tie(start, last) = vertices(graph); start != last; ++start)
  273. {
  274. // Note1: The sub algorithm may NOT be reused once it has been
  275. // called.
  276. // Note2: We reuse the Stack and the ClosedMatrix (after clearing
  277. // them) in each iteration to avoid redundant destruction and
  278. // construction. It would be strictly equivalent to have these as
  279. // member variables of the sub algorithm.
  280. SubAlgorithm sub_algo(
  281. graph, visitor, vertex_index_map, stack, closed, n_vertices);
  282. sub_algo(*start);
  283. stack.clear();
  284. typename ClosedMatrix::iterator row, last_row = closed.end();
  285. for (row = closed.begin(); row != last_row; ++row)
  286. row->clear();
  287. }
  288. }
  289. template < typename GetAdjacentVertices, typename Graph, typename Visitor >
  290. void call_hawick_circuits(
  291. Graph const& graph, BOOST_FWD_REF(Visitor) visitor)
  292. {
  293. call_hawick_circuits< GetAdjacentVertices >(graph,
  294. boost::forward< Visitor >(visitor), get(vertex_index, graph));
  295. }
  296. } // end namespace hawick_circuits_detail
  297. //! Enumerate all the elementary circuits in a directed multigraph.
  298. template < typename Graph, typename Visitor, typename VertexIndexMap >
  299. void hawick_circuits(BOOST_FWD_REF(Graph) graph, BOOST_FWD_REF(Visitor) visitor,
  300. BOOST_FWD_REF(VertexIndexMap) vertex_index_map)
  301. {
  302. hawick_circuits_detail::call_hawick_circuits<
  303. hawick_circuits_detail::get_all_adjacent_vertices >(
  304. boost::forward< Graph >(graph), boost::forward< Visitor >(visitor),
  305. boost::forward< VertexIndexMap >(vertex_index_map));
  306. }
  307. template < typename Graph, typename Visitor >
  308. void hawick_circuits(BOOST_FWD_REF(Graph) graph, BOOST_FWD_REF(Visitor) visitor)
  309. {
  310. hawick_circuits_detail::call_hawick_circuits<
  311. hawick_circuits_detail::get_all_adjacent_vertices >(
  312. boost::forward< Graph >(graph), boost::forward< Visitor >(visitor));
  313. }
  314. /*!
  315. * Same as `boost::hawick_circuits`, but duplicate circuits caused by parallel
  316. * edges will not be considered. Each circuit will be considered only once.
  317. */
  318. template < typename Graph, typename Visitor, typename VertexIndexMap >
  319. void hawick_unique_circuits(BOOST_FWD_REF(Graph) graph,
  320. BOOST_FWD_REF(Visitor) visitor,
  321. BOOST_FWD_REF(VertexIndexMap) vertex_index_map)
  322. {
  323. hawick_circuits_detail::call_hawick_circuits<
  324. hawick_circuits_detail::get_unique_adjacent_vertices >(
  325. boost::forward< Graph >(graph), boost::forward< Visitor >(visitor),
  326. boost::forward< VertexIndexMap >(vertex_index_map));
  327. }
  328. template < typename Graph, typename Visitor >
  329. void hawick_unique_circuits(
  330. BOOST_FWD_REF(Graph) graph, BOOST_FWD_REF(Visitor) visitor)
  331. {
  332. hawick_circuits_detail::call_hawick_circuits<
  333. hawick_circuits_detail::get_unique_adjacent_vertices >(
  334. boost::forward< Graph >(graph), boost::forward< Visitor >(visitor));
  335. }
  336. } // end namespace boost
  337. #endif // !BOOST_GRAPH_HAWICK_CIRCUITS_HPP