undirected_dfs.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <string>
  9. #include <boost/graph/adjacency_list.hpp>
  10. #include <boost/graph/undirected_dfs.hpp>
  11. #include <boost/cstdlib.hpp>
  12. #include <iostream>
  13. /*
  14. Example graph from Tarjei Knapstad.
  15. H15
  16. |
  17. H8 C2
  18. \ / \
  19. H9-C0-C1 C3-O7-H14
  20. / | |
  21. H10 C6 C4
  22. / \ / \
  23. H11 C5 H13
  24. |
  25. H12
  26. */
  27. std::string name[] = { "C0", "C1", "C2", "C3", "C4", "C5", "C6", "O7",
  28. "H8", "H9", "H10", "H11", "H12", "H13", "H14", "H15"};
  29. struct detect_loops : public boost::dfs_visitor<>
  30. {
  31. template <class Edge, class Graph>
  32. void back_edge(Edge e, const Graph& g) {
  33. std::cout << name[source(e, g)]
  34. << " -- "
  35. << name[target(e, g)] << "\n";
  36. }
  37. };
  38. int main(int, char*[])
  39. {
  40. using namespace boost;
  41. typedef adjacency_list< vecS, vecS, undirectedS,
  42. no_property,
  43. property<edge_color_t, default_color_type> > graph_t;
  44. typedef graph_traits<graph_t>::vertex_descriptor vertex_t;
  45. const std::size_t N = sizeof(name)/sizeof(std::string);
  46. graph_t g(N);
  47. add_edge(0, 1, g);
  48. add_edge(0, 8, g);
  49. add_edge(0, 9, g);
  50. add_edge(0, 10, g);
  51. add_edge(1, 2, g);
  52. add_edge(1, 6, g);
  53. add_edge(2, 15, g);
  54. add_edge(2, 3, g);
  55. add_edge(3, 7, g);
  56. add_edge(3, 4, g);
  57. add_edge(4, 13, g);
  58. add_edge(4, 5, g);
  59. add_edge(5, 12, g);
  60. add_edge(5, 6, g);
  61. add_edge(6, 11, g);
  62. add_edge(7, 14, g);
  63. std::cout << "back edges:\n";
  64. detect_loops vis;
  65. undirected_dfs(g, root_vertex(vertex_t(0)).visitor(vis)
  66. .edge_color_map(get(edge_color, g)));
  67. std::cout << std::endl;
  68. return boost::exit_success;
  69. }