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

http://hadesmem.googlecode.com/ · C++ Header · 376 lines · 241 code · 44 blank · 91 comment · 25 complexity · 134ededc992d7be4ec31190379d15f32 MD5 · raw file

  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_CYCLE_HPP
  7. #define BOOST_GRAPH_CYCLE_HPP
  8. #include <vector>
  9. #include <boost/config.hpp>
  10. #include <boost/graph/graph_concepts.hpp>
  11. #include <boost/graph/graph_traits.hpp>
  12. #include <boost/graph/properties.hpp>
  13. #include <boost/concept/detail/concept_def.hpp>
  14. namespace boost {
  15. namespace concepts {
  16. BOOST_concept(CycleVisitor,(Visitor)(Path)(Graph))
  17. {
  18. BOOST_CONCEPT_USAGE(CycleVisitor)
  19. {
  20. vis.cycle(p, g);
  21. }
  22. private:
  23. Visitor vis;
  24. Graph g;
  25. Path p;
  26. };
  27. } /* namespace concepts */
  28. using concepts::CycleVisitorConcept;
  29. } /* namespace boost */
  30. #include <boost/concept/detail/concept_undef.hpp>
  31. namespace boost
  32. {
  33. // The implementation of this algorithm is a reproduction of the Teirnan
  34. // approach for directed graphs: bibtex follows
  35. //
  36. // @article{362819,
  37. // author = {James C. Tiernan},
  38. // title = {An efficient search algorithm to find the elementary circuits of a graph},
  39. // journal = {Commun. ACM},
  40. // volume = {13},
  41. // number = {12},
  42. // year = {1970},
  43. // issn = {0001-0782},
  44. // pages = {722--726},
  45. // doi = {http://doi.acm.org/10.1145/362814.362819},
  46. // publisher = {ACM Press},
  47. // address = {New York, NY, USA},
  48. // }
  49. //
  50. // It should be pointed out that the author does not provide a complete analysis for
  51. // either time or space. This is in part, due to the fact that it's a fairly input
  52. // sensitive problem related to the density and construction of the graph, not just
  53. // its size.
  54. //
  55. // I've also taken some liberties with the interpretation of the algorithm - I've
  56. // basically modernized it to use real data structures (no more arrays and matrices).
  57. // Oh... and there's explicit control structures - not just gotos.
  58. //
  59. // The problem is definitely NP-complete, an an unbounded implementation of this
  60. // will probably run for quite a while on a large graph. The conclusions
  61. // of this paper also reference a Paton algorithm for undirected graphs as being
  62. // much more efficient (apparently based on spanning trees). Although not implemented,
  63. // it can be found here:
  64. //
  65. // @article{363232,
  66. // author = {Keith Paton},
  67. // title = {An algorithm for finding a fundamental set of cycles of a graph},
  68. // journal = {Commun. ACM},
  69. // volume = {12},
  70. // number = {9},
  71. // year = {1969},
  72. // issn = {0001-0782},
  73. // pages = {514--518},
  74. // doi = {http://doi.acm.org/10.1145/363219.363232},
  75. // publisher = {ACM Press},
  76. // address = {New York, NY, USA},
  77. // }
  78. /**
  79. * The default cycle visitor providse an empty visit function for cycle
  80. * visitors.
  81. */
  82. struct cycle_visitor
  83. {
  84. template <typename Path, typename Graph>
  85. inline void cycle(const Path& p, const Graph& g)
  86. { }
  87. };
  88. /**
  89. * The min_max_cycle_visitor simultaneously records the minimum and maximum
  90. * cycles in a graph.
  91. */
  92. struct min_max_cycle_visitor
  93. {
  94. min_max_cycle_visitor(std::size_t& min_, std::size_t& max_)
  95. : minimum(min_), maximum(max_)
  96. { }
  97. template <typename Path, typename Graph>
  98. inline void cycle(const Path& p, const Graph& g)
  99. {
  100. BOOST_USING_STD_MIN();
  101. BOOST_USING_STD_MAX();
  102. std::size_t len = p.size();
  103. minimum = min BOOST_PREVENT_MACRO_SUBSTITUTION (minimum, len);
  104. maximum = max BOOST_PREVENT_MACRO_SUBSTITUTION (maximum, len);
  105. }
  106. std::size_t& minimum;
  107. std::size_t& maximum;
  108. };
  109. inline min_max_cycle_visitor
  110. find_min_max_cycle(std::size_t& min_, std::size_t& max_)
  111. { return min_max_cycle_visitor(min_, max_); }
  112. namespace detail
  113. {
  114. template <typename Graph, typename Path>
  115. inline bool
  116. is_vertex_in_path(const Graph&,
  117. typename graph_traits<Graph>::vertex_descriptor v,
  118. const Path& p)
  119. {
  120. return (std::find(p.begin(), p.end(), v) != p.end());
  121. }
  122. template <typename Graph, typename ClosedMatrix>
  123. inline bool
  124. is_path_closed(const Graph& g,
  125. typename graph_traits<Graph>::vertex_descriptor u,
  126. typename graph_traits<Graph>::vertex_descriptor v,
  127. const ClosedMatrix& closed)
  128. {
  129. // the path from u to v is closed if v can be found in the list
  130. // of closed vertices associated with u.
  131. typedef typename ClosedMatrix::const_reference Row;
  132. Row r = closed[get(vertex_index, g, u)];
  133. if(find(r.begin(), r.end(), v) != r.end()) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. template <typename Graph, typename Path, typename ClosedMatrix>
  139. inline bool
  140. can_extend_path(const Graph& g,
  141. typename graph_traits<Graph>::edge_descriptor e,
  142. const Path& p,
  143. const ClosedMatrix& m)
  144. {
  145. function_requires< IncidenceGraphConcept<Graph> >();
  146. function_requires< VertexIndexGraphConcept<Graph> >();
  147. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  148. // get the vertices in question
  149. Vertex
  150. u = source(e, g),
  151. v = target(e, g);
  152. // conditions for allowing a traversal along this edge are:
  153. // 1. the index of v must be greater than that at which the
  154. // the path is rooted (p.front()).
  155. // 2. the vertex v cannot already be in the path
  156. // 3. the vertex v cannot be closed to the vertex u
  157. bool indices = get(vertex_index, g, p.front()) < get(vertex_index, g, v);
  158. bool path = !is_vertex_in_path(g, v, p);
  159. bool closed = !is_path_closed(g, u, v, m);
  160. return indices && path && closed;
  161. }
  162. template <typename Graph, typename Path>
  163. inline bool
  164. can_wrap_path(const Graph& g, const Path& p)
  165. {
  166. function_requires< IncidenceGraphConcept<Graph> >();
  167. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  168. typedef typename graph_traits<Graph>::out_edge_iterator OutIterator;
  169. // iterate over the out-edges of the back, looking for the
  170. // front of the path. also, we can't travel along the same
  171. // edge that we did on the way here, but we don't quite have the
  172. // stringent requirements that we do in can_extend_path().
  173. Vertex
  174. u = p.back(),
  175. v = p.front();
  176. OutIterator i, end;
  177. for(tie(i, end) = out_edges(u, g); i != end; ++i) {
  178. if((target(*i, g) == v)) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. template <typename Graph,
  185. typename Path,
  186. typename ClosedMatrix>
  187. inline typename graph_traits<Graph>::vertex_descriptor
  188. extend_path(const Graph& g,
  189. Path& p,
  190. ClosedMatrix& closed)
  191. {
  192. function_requires< IncidenceGraphConcept<Graph> >();
  193. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  194. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  195. typedef typename graph_traits<Graph>::out_edge_iterator OutIterator;
  196. // get the current vertex
  197. Vertex u = p.back();
  198. Vertex ret = graph_traits<Graph>::null_vertex();
  199. // AdjacencyIterator i, end;
  200. OutIterator i, end;
  201. for(tie(i, end) = out_edges(u, g); i != end; ++i) {
  202. Vertex v = target(*i, g);
  203. // if we can actually extend along this edge,
  204. // then that's what we want to do
  205. if(can_extend_path(g, *i, p, closed)) {
  206. p.push_back(v); // add the vertex to the path
  207. ret = v;
  208. break;
  209. }
  210. }
  211. return ret;
  212. }
  213. template <typename Graph, typename Path, typename ClosedMatrix>
  214. inline bool
  215. exhaust_paths(const Graph& g, Path& p, ClosedMatrix& closed)
  216. {
  217. function_requires< GraphConcept<Graph> >();
  218. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  219. // if there's more than one vertex in the path, this closes
  220. // of some possible routes and returns true. otherwise, if there's
  221. // only one vertex left, the vertex has been used up
  222. if(p.size() > 1) {
  223. // get the last and second to last vertices, popping the last
  224. // vertex off the path
  225. Vertex last, prev;
  226. last = p.back();
  227. p.pop_back();
  228. prev = p.back();
  229. // reset the closure for the last vertex of the path and
  230. // indicate that the last vertex in p is now closed to
  231. // the next-to-last vertex in p
  232. closed[get(vertex_index, g, last)].clear();
  233. closed[get(vertex_index, g, prev)].push_back(last);
  234. return true;
  235. }
  236. else {
  237. return false;
  238. }
  239. }
  240. template <typename Graph, typename Visitor>
  241. inline void
  242. all_cycles_from_vertex(const Graph& g,
  243. typename graph_traits<Graph>::vertex_descriptor v,
  244. Visitor vis,
  245. std::size_t minlen,
  246. std::size_t maxlen)
  247. {
  248. function_requires< VertexListGraphConcept<Graph> >();
  249. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  250. typedef std::vector<Vertex> Path;
  251. function_requires< CycleVisitorConcept<Visitor,Path,Graph> >();
  252. typedef std::vector<Vertex> VertexList;
  253. typedef std::vector<VertexList> ClosedMatrix;
  254. Path p;
  255. ClosedMatrix closed(num_vertices(g), VertexList());
  256. Vertex null = graph_traits<Graph>::null_vertex();
  257. // each path investigation starts at the ith vertex
  258. p.push_back(v);
  259. while(1) {
  260. // extend the path until we've reached the end or the
  261. // maxlen-sized cycle
  262. Vertex j = null;
  263. while(((j = detail::extend_path(g, p, closed)) != null)
  264. && (p.size() < maxlen))
  265. ; // empty loop
  266. // if we're done extending the path and there's an edge
  267. // connecting the back to the front, then we should have
  268. // a cycle.
  269. if(detail::can_wrap_path(g, p) && p.size() >= minlen) {
  270. vis.cycle(p, g);
  271. }
  272. if(!detail::exhaust_paths(g, p, closed)) {
  273. break;
  274. }
  275. }
  276. }
  277. // Select the minimum allowable length of a cycle based on the directedness
  278. // of the graph - 2 for directed, 3 for undirected.
  279. template <typename D> struct min_cycles { enum { value = 2 }; };
  280. template <> struct min_cycles<undirected_tag> { enum { value = 3 }; };
  281. } /* namespace detail */
  282. template <typename Graph, typename Visitor>
  283. inline void
  284. tiernan_all_cycles(const Graph& g,
  285. Visitor vis,
  286. std::size_t minlen,
  287. std::size_t maxlen)
  288. {
  289. function_requires< VertexListGraphConcept<Graph> >();
  290. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  291. VertexIterator i, end;
  292. for(tie(i, end) = vertices(g); i != end; ++i) {
  293. detail::all_cycles_from_vertex(g, *i, vis, minlen, maxlen);
  294. }
  295. }
  296. template <typename Graph, typename Visitor>
  297. inline void
  298. tiernan_all_cycles(const Graph& g, Visitor vis, std::size_t maxlen)
  299. {
  300. typedef typename graph_traits<Graph>::directed_category Dir;
  301. tiernan_all_cycles(g, vis, detail::min_cycles<Dir>::value, maxlen);
  302. }
  303. template <typename Graph, typename Visitor>
  304. inline void
  305. tiernan_all_cycles(const Graph& g, Visitor vis)
  306. {
  307. typedef typename graph_traits<Graph>::directed_category Dir;
  308. tiernan_all_cycles(g, vis, detail::min_cycles<Dir>::value,
  309. (std::numeric_limits<std::size_t>::max)());
  310. }
  311. template <typename Graph>
  312. inline std::pair<std::size_t, std::size_t>
  313. tiernan_girth_and_circumference(const Graph& g)
  314. {
  315. std::size_t
  316. min_ = (std::numeric_limits<std::size_t>::max)(),
  317. max_ = 0;
  318. tiernan_all_cycles(g, find_min_max_cycle(min_, max_));
  319. // if this is the case, the graph is acyclic...
  320. if(max_ == 0) max_ = min_;
  321. return std::make_pair(min_, max_);
  322. }
  323. template <typename Graph>
  324. inline std::size_t
  325. tiernan_girth(const Graph& g)
  326. { return tiernan_girth_and_circumference(g).first; }
  327. template <typename Graph>
  328. inline std::size_t
  329. tiernan_circumference(const Graph& g)
  330. { return tiernan_girth_and_circumference(g).second; }
  331. } /* namespace boost */
  332. #endif