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