1 //=======================================================================
2 // Copyright 2002 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
8 
9 #include <string>
10 #include <boost/graph/adjacency_list.hpp>
11 #include <boost/graph/undirected_dfs.hpp>
12 #include <boost/cstdlib.hpp>
13 #include <iostream>
14 
15 /*
16   Example graph from Tarjei Knapstad.
17 
18                    H15
19                    |
20           H8       C2
21             \     /  \
22           H9-C0-C1    C3-O7-H14
23             /   |     |
24           H10   C6    C4
25                /  \  /  \
26               H11  C5    H13
27                    |
28                    H12
29 */
30 
31 std::string name[] = { "C0", "C1", "C2", "C3", "C4", "C5", "C6", "O7",
32                        "H8", "H9", "H10", "H11", "H12", "H13", "H14", "H15"};
33 
34 
35 struct detect_loops : public boost::dfs_visitor<>
36 {
37   template <class Edge, class Graph>
back_edgedetect_loops38   void back_edge(Edge e, const Graph& g) {
39     std::cout << name[source(e, g)]
40               << " -- "
41               << name[target(e, g)] << "\n";
42   }
43 };
44 
main(int,char * [])45 int main(int, char*[])
46 {
47   using namespace boost;
48   typedef adjacency_list< vecS, vecS, undirectedS,
49     no_property,
50     property<edge_color_t, default_color_type> > graph_t;
51   typedef graph_traits<graph_t>::vertex_descriptor vertex_t;
52 
53   const std::size_t N = sizeof(name)/sizeof(std::string);
54   graph_t g(N);
55 
56   add_edge(0, 1, g);
57   add_edge(0, 8, g);
58   add_edge(0, 9, g);
59   add_edge(0, 10, g);
60   add_edge(1, 2, g);
61   add_edge(1, 6, g);
62   add_edge(2, 15, g);
63   add_edge(2, 3, g);
64   add_edge(3, 7, g);
65   add_edge(3, 4, g);
66   add_edge(4, 13, g);
67   add_edge(4, 5, g);
68   add_edge(5, 12, g);
69   add_edge(5, 6, g);
70   add_edge(6, 11, g);
71   add_edge(7, 14, g);
72 
73   std::cout << "back edges:\n";
74   detect_loops vis;
75   undirected_dfs(g, root_vertex(vertex_t(0)).visitor(vis)
76                  .edge_color_map(get(edge_color, g)));
77   std::cout << std::endl;
78 
79   return boost::exit_success;
80 }
81