loops_dfs.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //=======================================================================
  2. // Copyright 2001 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. IMPORTANT!!!
  10. ~~~~~~~~~~~~
  11. This example uses interfaces that have been deprecated and removed from Boost.Grpah.
  12. Someone needs to update it, as it does NOT compile.
  13. */
  14. #include <boost/config.hpp>
  15. #include <boost/concept/assert.hpp>
  16. #include <iostream>
  17. #include <fstream>
  18. #include <stack>
  19. #include <map>
  20. #include <boost/lexical_cast.hpp>
  21. #include <boost/graph/adjacency_list.hpp>
  22. #include <boost/graph/depth_first_search.hpp>
  23. #include <boost/graph/graphviz.hpp>
  24. #include <boost/graph/copy.hpp>
  25. #include <boost/graph/reverse_graph.hpp>
  26. using namespace boost;
  27. template < typename OutputIterator >
  28. class back_edge_recorder : public default_dfs_visitor
  29. {
  30. public:
  31. back_edge_recorder(OutputIterator out):m_out(out) { }
  32. template < typename Edge, typename Graph >
  33. void back_edge(Edge e, const Graph &)
  34. {
  35. *m_out++ = e;
  36. }
  37. private:
  38. OutputIterator m_out;
  39. };
  40. // object generator function
  41. template < typename OutputIterator >
  42. back_edge_recorder < OutputIterator >
  43. make_back_edge_recorder(OutputIterator out)
  44. {
  45. return back_edge_recorder < OutputIterator > (out);
  46. }
  47. template < typename Graph, typename Loops > void
  48. find_loops(typename graph_traits < Graph >::vertex_descriptor entry,
  49. const Graph & g,
  50. Loops & loops) // A container of sets of vertices
  51. {
  52. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph> ));
  53. typedef typename graph_traits < Graph >::edge_descriptor Edge;
  54. std::vector < Edge > back_edges;
  55. std::vector < default_color_type > color_map(num_vertices(g));
  56. depth_first_visit(g, entry,
  57. make_back_edge_recorder(std::back_inserter(back_edges)),
  58. make_iterator_property_map(color_map.begin(),
  59. get(vertex_index, g), color_map[0]));
  60. for (typename std::vector < Edge >::size_type i = 0; i < back_edges.size(); ++i) {
  61. typename Loops::value_type x;
  62. loops.push_back(x);
  63. compute_loop_extent(back_edges[i], g, loops.back());
  64. }
  65. }
  66. template < typename Graph, typename Set > void
  67. compute_loop_extent(typename graph_traits <
  68. Graph >::edge_descriptor back_edge, const Graph & g,
  69. Set & loop_set)
  70. {
  71. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph> ));
  72. typedef typename graph_traits < Graph >::vertex_descriptor Vertex;
  73. typedef color_traits < default_color_type > Color;
  74. Vertex loop_head, loop_tail;
  75. loop_tail = source(back_edge, g);
  76. loop_head = target(back_edge, g);
  77. std::vector < default_color_type >
  78. reachable_from_head(num_vertices(g), Color::white());
  79. default_color_type c;
  80. depth_first_visit(g, loop_head, default_dfs_visitor(),
  81. make_iterator_property_map(reachable_from_head.begin(),
  82. get(vertex_index, g), c));
  83. std::vector < default_color_type > reachable_to_tail(num_vertices(g));
  84. reverse_graph < Graph > reverse_g(g);
  85. depth_first_visit(reverse_g, loop_tail, default_dfs_visitor(),
  86. make_iterator_property_map(reachable_to_tail.begin(),
  87. get(vertex_index, g), c));
  88. typename graph_traits < Graph >::vertex_iterator vi, vi_end;
  89. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  90. if (reachable_from_head[*vi] != Color::white()
  91. && reachable_to_tail[*vi] != Color::white())
  92. loop_set.insert(*vi);
  93. }
  94. int
  95. main(int argc, char *argv[])
  96. {
  97. if (argc < 3) {
  98. std::cerr << "usage: loops_dfs <in-file> <out-file>" << std::endl;
  99. return -1;
  100. }
  101. GraphvizDigraph g_in;
  102. read_graphviz(argv[1], g_in);
  103. typedef adjacency_list < vecS, vecS, bidirectionalS,
  104. GraphvizVertexProperty,
  105. GraphvizEdgeProperty, GraphvizGraphProperty > Graph;
  106. typedef graph_traits < Graph >::vertex_descriptor Vertex;
  107. Graph g;
  108. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  109. // VC++ has trouble with the get_property() function
  110. get_property(g, graph_name) = "loops";
  111. #endif
  112. copy_graph(g_in, g);
  113. typedef std::set < Vertex > set_t;
  114. typedef std::list < set_t > list_of_sets_t;
  115. list_of_sets_t loops;
  116. Vertex entry = *vertices(g).first;
  117. find_loops(entry, g, loops);
  118. property_map<Graph, vertex_attribute_t>::type vattr_map = get(vertex_attribute, g);
  119. property_map<Graph, edge_attribute_t>::type eattr_map = get(edge_attribute, g);
  120. graph_traits < Graph >::edge_iterator ei, ei_end;
  121. for (list_of_sets_t::iterator i = loops.begin(); i != loops.end(); ++i) {
  122. std::vector < bool > in_loop(num_vertices(g), false);
  123. for (set_t::iterator j = (*i).begin(); j != (*i).end(); ++j) {
  124. vattr_map[*j]["color"] = "gray";
  125. in_loop[*j] = true;
  126. }
  127. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  128. if (in_loop[source(*ei, g)] && in_loop[target(*ei, g)])
  129. eattr_map[*ei]["color"] = "gray";
  130. }
  131. std::ofstream loops_out(argv[2]);
  132. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  133. // VC++ has trouble with the get_property() functions
  134. loops_out << "digraph loops {\n"
  135. << "size=\"3,3\"\n"
  136. << "ratio=\"fill\"\n"
  137. << "shape=\"box\"\n";
  138. graph_traits<Graph>::vertex_iterator vi, vi_end;
  139. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  140. loops_out << *vi << "[";
  141. for (std::map<std::string,std::string>::iterator ai = vattr_map[*vi].begin();
  142. ai != vattr_map[*vi].end(); ++ai) {
  143. loops_out << ai->first << "=" << ai->second;
  144. if (next(ai) != vattr_map[*vi].end())
  145. loops_out << ", ";
  146. }
  147. loops_out<< "]";
  148. }
  149. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
  150. loops_out << source(*ei, g) << " -> " << target(*ei, g) << "[";
  151. std::map<std::string,std::string>& attr_map = eattr_map[*ei];
  152. for (std::map<std::string,std::string>::iterator eai = attr_map.begin();
  153. eai != attr_map.end(); ++eai) {
  154. loops_out << eai->first << "=" << eai->second;
  155. if (next(eai) != attr_map.end())
  156. loops_out << ", ";
  157. }
  158. loops_out<< "]";
  159. }
  160. loops_out << "}\n";
  161. #else
  162. get_property(g, graph_graph_attribute)["size"] = "3,3";
  163. get_property(g, graph_graph_attribute)["ratio"] = "fill";
  164. get_property(g, graph_vertex_attribute)["shape"] = "box";
  165. write_graphviz(loops_out, g,
  166. make_vertex_attributes_writer(g),
  167. make_edge_attributes_writer(g),
  168. make_graph_attributes_writer(g));
  169. #endif
  170. return 0;
  171. }