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