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
13 Boost.Grpah. Someone needs to update it, as it does NOT compile.
14 */
15
16 #include <boost/config.hpp>
17 #include <iostream>
18 #include <fstream>
19 #include <boost/graph/adjacency_list.hpp>
20 #include <boost/graph/depth_first_search.hpp>
21 #include <boost/graph/graphviz.hpp>
22 #include <boost/graph/copy.hpp>
23
main(int argc,char * argv[])24 int main(int argc, char* argv[])
25 {
26 if (argc < 3)
27 {
28 std::cerr << "usage: reachable-loop-head.exe <in-file> <out-file>"
29 << std::endl;
30 return -1;
31 }
32 using namespace boost;
33 GraphvizDigraph g;
34 read_graphviz(argv[1], g);
35 graph_traits< GraphvizDigraph >::vertex_descriptor loop_head = 1;
36 typedef color_traits< default_color_type > Color;
37
38 std::vector< default_color_type > reachable_from_head(
39 num_vertices(g), Color::white());
40 default_color_type c;
41 depth_first_visit(g, loop_head, default_dfs_visitor(),
42 make_iterator_property_map(
43 reachable_from_head.begin(), get(vertex_index, g), c));
44
45 property_map< GraphvizDigraph, vertex_attribute_t >::type vattr_map
46 = get(vertex_attribute, g);
47
48 graph_traits< GraphvizDigraph >::vertex_iterator i, i_end;
49 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
50 if (reachable_from_head[*i] != Color::white())
51 {
52 vattr_map[*i]["color"] = "gray";
53 vattr_map[*i]["style"] = "filled";
54 }
55
56 std::ofstream loops_out(argv[2]);
57 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
58 // VC++ has trouble with the get_property() functions
59 loops_out << "digraph G {\n"
60 << "size=\"3,3\"\n"
61 << "ratio=\"fill\"\n"
62 << "shape=\"box\"\n";
63 graph_traits< GraphvizDigraph >::vertex_iterator vi, vi_end;
64 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
65 {
66 loops_out << *vi << "[";
67 for (std::map< std::string, std::string >::iterator ai
68 = vattr_map[*vi].begin();
69 ai != vattr_map[*vi].end(); ++ai)
70 {
71 loops_out << ai->first << "=" << ai->second;
72 if (next(ai) != vattr_map[*vi].end())
73 loops_out << ", ";
74 }
75 loops_out << "]";
76 }
77 property_map< GraphvizDigraph, edge_attribute_t >::type eattr_map
78 = get(edge_attribute, g);
79 graph_traits< GraphvizDigraph >::edge_iterator ei, ei_end;
80 for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
81 {
82 loops_out << source(*ei, g) << " -> " << target(*ei, g) << "[";
83 std::map< std::string, std::string >& attr_map = eattr_map[*ei];
84 for (std::map< std::string, std::string >::iterator eai
85 = attr_map.begin();
86 eai != attr_map.end(); ++eai)
87 {
88 loops_out << eai->first << "=" << eai->second;
89 if (next(eai) != attr_map.end())
90 loops_out << ", ";
91 }
92 loops_out << "]";
93 }
94 loops_out << "}\n";
95 #else
96 get_property(g, graph_graph_attribute)["size"] = "3,3";
97 get_property(g, graph_graph_attribute)["ratio"] = "fill";
98 get_property(g, graph_vertex_attribute)["shape"] = "box";
99
100 write_graphviz(loops_out, g, make_vertex_attributes_writer(g),
101 make_edge_attributes_writer(g), make_graph_attributes_writer(g));
102 #endif
103
104 return EXIT_SUCCESS;
105 }
106