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