1 | <HTML> |
---|
2 | |
---|
3 | <!-- |
---|
4 | -- Copyright (c) Lie-Quan Lee and Jeremy Siek 2000, 2001 |
---|
5 | -- |
---|
6 | -- Permission to use, copy, modify, distribute and sell this software |
---|
7 | -- and its documentation for any purpose is hereby granted without fee, |
---|
8 | -- provided that the above copyright notice appears in all copies and |
---|
9 | -- that both that copyright notice and this permission notice appear |
---|
10 | -- in supporting documentation. Silicon Graphics makes no |
---|
11 | -- representations about the suitability of this software for any |
---|
12 | -- purpose. It is provided "as is" without express or implied warranty. |
---|
13 | --> |
---|
14 | <Head> |
---|
15 | <Title>Boost Graph Library: write graphviz</Title> |
---|
16 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
---|
17 | ALINK="#ff0000"> |
---|
18 | <IMG SRC="../../../boost.png" |
---|
19 | ALT="C++ Boost" width="277" height="86"> |
---|
20 | |
---|
21 | <BR Clear> |
---|
22 | |
---|
23 | <H1><A NAME="sec:write-graphviz"> |
---|
24 | <TT>write_graphviz</TT> |
---|
25 | </H1> |
---|
26 | |
---|
27 | |
---|
28 | <pre> |
---|
29 | // Output graph structure without properties. |
---|
30 | template < typename VertexListGraph > |
---|
31 | void |
---|
32 | write_graphviz(std::ostream& out, const VertexListGraph& g); |
---|
33 | |
---|
34 | // Graph structure with customized property output |
---|
35 | template < typename VertexListGraph, typename VertexPropertyWriter > |
---|
36 | void |
---|
37 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
38 | VertexPropertyWriter vpw); |
---|
39 | |
---|
40 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
41 | typename EdgePropertyWriter > |
---|
42 | void |
---|
43 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
44 | VertexPropertyWriter vpw, EdgePropertyWriter epw); |
---|
45 | |
---|
46 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
47 | typename EdgePropertyWriter, typename GraphPropertyWriter > |
---|
48 | void |
---|
49 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
50 | VertexPropertyWriter vpw, EdgePropertyWriter epw, |
---|
51 | GraphPropertyWriter gpw); |
---|
52 | |
---|
53 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
54 | typename EdgePropertyWriter, typename GraphPropertyWriter, |
---|
55 | typename VertexID > |
---|
56 | void |
---|
57 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
58 | VertexPropertyWriter vpw, EdgePropertyWriter epw, |
---|
59 | GraphPropertyWriter gpw, VertexID vertex_id); |
---|
60 | |
---|
61 | // Graph structure with dynamic property output |
---|
62 | template<typename Graph> |
---|
63 | void |
---|
64 | write_graphviz(std::ostream& out, const Graph& g, |
---|
65 | const dynamic_properties& dp, |
---|
66 | const std::string& node_id = "node_id"); |
---|
67 | |
---|
68 | template<typename Graph, typename VertexID> |
---|
69 | void |
---|
70 | write_graphviz(std::ostream& out, const Graph& g, |
---|
71 | const dynamic_properties& dp, const std::string& node_id, |
---|
72 | VertexID vertex_id); |
---|
73 | </pre> |
---|
74 | |
---|
75 | <p> |
---|
76 | This is to write a BGL graph object into an output stream in graphviz |
---|
77 | dot format so that users can make use of <a href="http://www.research.att.com/sw/tools/graphviz/">AT&T graphviz</a> to draw a |
---|
78 | picture with nice layout. |
---|
79 | <p> |
---|
80 | The first version with two parameters will write the graph into a |
---|
81 | <tt>std::ostream</tt> where each vertex is represented by its numerical vertex |
---|
82 | ID. If users have either interior or exterior properties for each vertex |
---|
83 | in the graph, the second version above provides a way to print those |
---|
84 | properties into the graphviz format file. For example, if each vertex |
---|
85 | in the graph has its label through property map object <tt>name</tt>, |
---|
86 | users can use the second version: |
---|
87 | <pre> |
---|
88 | write_graph(out, g, make_label_writer(name)); |
---|
89 | </pre> |
---|
90 | The utility function <tt>make_label_writer</tt> returns a predefined |
---|
91 | <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> for |
---|
92 | vertex labels. Similarly, the third |
---|
93 | version and fourth version require vertex |
---|
94 | <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a>, edge |
---|
95 | <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a>, and graph |
---|
96 | <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a>, |
---|
97 | respectively. |
---|
98 | |
---|
99 | <p> The final two overloads of <code>write_graphviz</code> will emit |
---|
100 | all of the properties stored in the <a |
---|
101 | href="../../property_map/doc/dynamic_property_map.html"><code>dynamic_properties</a></code> |
---|
102 | object, thereby retaining the properties that have been read in |
---|
103 | through the dual function <a |
---|
104 | href="read_graphviz.html"><code>read_graphviz</code></a>. With these |
---|
105 | overloads, <code>node_id</code> is the name of the property map that |
---|
106 | stores the IDs of each node for use in the output (if it is stored in |
---|
107 | the <code>dynamic_properties</code> structure); alternatively, one may |
---|
108 | provide an arbitrary property map for <code>vertex_id</code> giving the |
---|
109 | vertex identifiers.</p> |
---|
110 | |
---|
111 | <H3>Where Defined</H3> |
---|
112 | |
---|
113 | <P> |
---|
114 | <a href="../../../boost/graph/graphviz.hpp"><TT>boost/graph/graphviz.hpp</TT></a> |
---|
115 | |
---|
116 | <h3><A NAME="concept:PropertyWriter"> |
---|
117 | <tt>PropertyWriter</tt> |
---|
118 | </h3> |
---|
119 | |
---|
120 | PropertyWriter is used in the <tt>write_graphviz</tt> function to |
---|
121 | print properties of vertex, edge or graph. There are two types of |
---|
122 | PropertyWriter. One is for a vertex or edge. The other is for a graph. |
---|
123 | Thus, users could easily extend the <tt>write_graphviz</tt> function |
---|
124 | by creating their own <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> only. |
---|
125 | <p> |
---|
126 | A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> |
---|
127 | for vertices or edges is a functor which can be called with two parameters: |
---|
128 | <tt>std::ostream</tt> and either a vertex or an edge descriptor. It should output a |
---|
129 | pair of brackets with a series of assigments "name=value" inside. |
---|
130 | Each assignment should be separated either with space, with comma, or with semicolon. |
---|
131 | The following functor, provided by BGL, is the example of PropertyWriter for |
---|
132 | vertices or edges. It is used to print the label of each vertex or edge. |
---|
133 | <pre> |
---|
134 | template < class Name > |
---|
135 | class label_writer { |
---|
136 | public: |
---|
137 | label_writer(Name _name) : name(_name) {} |
---|
138 | template <class VertexOrEdge> |
---|
139 | void operator()(std::ostream& out, const VertexOrEdge& v) const { |
---|
140 | out << "[label=\"" << name[v] << "\"]"; |
---|
141 | } |
---|
142 | private: |
---|
143 | Name name; |
---|
144 | }; |
---|
145 | </pre> |
---|
146 | |
---|
147 | <p> |
---|
148 | A function to conveniently create this writer is provided: |
---|
149 | <pre> |
---|
150 | template < class Name > |
---|
151 | label_writer<Name> |
---|
152 | make_label_writer(Name n); |
---|
153 | </pre> |
---|
154 | |
---|
155 | <p> |
---|
156 | A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> |
---|
157 | for graphs is a functor which is called with one parameter of type |
---|
158 | <tt>std::ostream</tt> and should print a series of graph properties. The following |
---|
159 | code excerpt is an example of a PropertyWriter for a graph. |
---|
160 | <pre> |
---|
161 | struct sample_graph_writer { |
---|
162 | void operator()(std::ostream& out) const { |
---|
163 | out << "graph [bgcolor=lightgrey]" << std::endl; |
---|
164 | out << "node [shape=circle color=white]" << std::endl; |
---|
165 | out << "edge [style=dashed]" << std::endl; |
---|
166 | } |
---|
167 | }; |
---|
168 | } |
---|
169 | </pre> |
---|
170 | |
---|
171 | <p> |
---|
172 | There exists a class <tt>default_writer</tt>, which can be used as both |
---|
173 | vertex/edge and graph property writer, and does nothing. It comes handy when |
---|
174 | only edge properties must be written, but function interface requries to pass |
---|
175 | vertex property writer as well. |
---|
176 | |
---|
177 | <h3>Parameters</h3> |
---|
178 | OUT: <tt>std::ostream& out</tt> |
---|
179 | <blockquote> |
---|
180 | A standard <tt>std::ostream</tt> object. |
---|
181 | </blockquote> |
---|
182 | |
---|
183 | IN: <tt>VertexListGraph& g</tt> |
---|
184 | <blockquote> |
---|
185 | A directed or undirected graph. The graph's type must be a model of |
---|
186 | <a href="./VertexListGraph.html">VertexListGraph</a>. Also the graph |
---|
187 | must have an internal <tt>vertex_index</tt> property map. |
---|
188 | </blockquote> |
---|
189 | |
---|
190 | IN: <tt>VertexPropertyWriter vpw</tt> |
---|
191 | <blockquote> |
---|
192 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
193 | properties of a vertex.<br> |
---|
194 | <b>Default</b>: <tt>default_writer()</tt> |
---|
195 | </blockquote> |
---|
196 | |
---|
197 | IN: <tt>EdgePropertyWriter epw</tt> |
---|
198 | <blockquote> |
---|
199 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
200 | properties of an edge.<br> |
---|
201 | <b>Default</b>: <tt>default_writer()</tt> |
---|
202 | </blockquote> |
---|
203 | |
---|
204 | IN: <tt>GraphPropertyWriter epw</tt> |
---|
205 | <blockquote> |
---|
206 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
207 | properties of a graph.<br> |
---|
208 | <b>Default</b>: <tt>default_writer()</tt> |
---|
209 | </blockquote> |
---|
210 | |
---|
211 | IN: <tt>dynamic_properties& dp</tt> |
---|
212 | <blockquote> |
---|
213 | Contains all of the vertex and edge properties that should be |
---|
214 | emitted by the GraphViz writer. |
---|
215 | </blockquote> |
---|
216 | |
---|
217 | IN: <tt>const std::string& node_id</tt> |
---|
218 | <blockquote> |
---|
219 | The name of the property map that provides identifiers for the |
---|
220 | vertices in the graph.<br> |
---|
221 | <b>Default</b>: <tt>"node_id"</tt> |
---|
222 | </blockquote> |
---|
223 | |
---|
224 | IN: <tt>VertexID vertex_id</tt> |
---|
225 | <blockquote> |
---|
226 | A property map that models <a href="../../property_map/ReadablePropertyMap.html">Readable Property Map</a> whose key type is the vertex descriptor of the graph and whose value type can be written to a stream. The value should be a unique descriptor that can be used to name a node in a Graphviz file (so it should not, for instance, have any spaces in it).<br> |
---|
227 | <b>Default</b>: If no <code>dynamic_properties</code> object is provided, <tt>get(vertex_index, g)</tt>. Otherwise, a dynamic property map that accesses the property map named <code>node_id</code>. |
---|
228 | </blockquote> |
---|
229 | <H3> |
---|
230 | Example |
---|
231 | </H3> |
---|
232 | |
---|
233 | This example demonstrates using BGL-graphviz interface to write |
---|
234 | a BGL graph into a graphviz format file. |
---|
235 | |
---|
236 | <pre> |
---|
237 | enum files_e { dax_h, yow_h, boz_h, zow_h, foo_cpp, |
---|
238 | foo_o, bar_cpp, bar_o, libfoobar_a, |
---|
239 | zig_cpp, zig_o, zag_cpp, zag_o, |
---|
240 | libzigzag_a, killerapp, N }; |
---|
241 | const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp", |
---|
242 | "foo.o", "bar.cpp", "bar.o", "libfoobar.a", |
---|
243 | "zig.cpp", "zig.o", "zag.cpp", "zag.o", |
---|
244 | "libzigzag.a", "killerapp" }; |
---|
245 | |
---|
246 | int main(int,char*[]) |
---|
247 | { |
---|
248 | |
---|
249 | typedef pair<int,int> Edge; |
---|
250 | Edge used_by[] = { |
---|
251 | Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h), |
---|
252 | Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp), |
---|
253 | Edge(boz_h, bar_cpp), Edge(boz_h, zig_cpp), Edge(boz_h, zag_cpp), |
---|
254 | Edge(zow_h, foo_cpp), |
---|
255 | Edge(foo_cpp, foo_o), |
---|
256 | Edge(foo_o, libfoobar_a), |
---|
257 | Edge(bar_cpp, bar_o), |
---|
258 | Edge(bar_o, libfoobar_a), |
---|
259 | Edge(libfoobar_a, libzigzag_a), |
---|
260 | Edge(zig_cpp, zig_o), |
---|
261 | Edge(zig_o, libzigzag_a), |
---|
262 | Edge(zag_cpp, zag_o), |
---|
263 | Edge(zag_o, libzigzag_a), |
---|
264 | Edge(libzigzag_a, killerapp) |
---|
265 | }; |
---|
266 | const int nedges = sizeof(used_by)/sizeof(Edge); |
---|
267 | int weights[nedges]; |
---|
268 | fill(weights, weights + nedges, 1); |
---|
269 | |
---|
270 | typedef adjacency_list< vecS, vecS, directedS, |
---|
271 | property< vertex_color_t, default_color_type >, |
---|
272 | property< edge_weight_t, int > |
---|
273 | > Graph; |
---|
274 | Graph g(N, used_by, used_by + nedges, weights); |
---|
275 | |
---|
276 | write_graphviz(std::cout, g, make_label_writer(name)); |
---|
277 | } |
---|
278 | </pre> |
---|
279 | |
---|
280 | The output will be: |
---|
281 | <pre> |
---|
282 | digraph G { |
---|
283 | 0 -> 4; |
---|
284 | 0 -> 6; |
---|
285 | 0 -> 1; |
---|
286 | 0 [label="dax.h"]; |
---|
287 | 1 -> 6; |
---|
288 | 1 -> 11; |
---|
289 | 1 [label="yow.h"]; |
---|
290 | 2 -> 6; |
---|
291 | 2 -> 9; |
---|
292 | 2 -> 11; |
---|
293 | 2 [label="boz.h"]; |
---|
294 | 3 -> 4; |
---|
295 | 3 [label="zow.h"]; |
---|
296 | 4 -> 5; |
---|
297 | 4 [label="foo.cpp"]; |
---|
298 | 5 -> 8; |
---|
299 | 5 [label="foo.o"]; |
---|
300 | 6 -> 7; |
---|
301 | 6 [label="bar.cpp"]; |
---|
302 | 7 -> 8; |
---|
303 | 7 [label="bar.o"]; |
---|
304 | 8 -> 13; |
---|
305 | 8 [label="libfoobar.a"]; |
---|
306 | 9 -> 10; |
---|
307 | 9 [label="zig.cpp"]; |
---|
308 | 10 -> 13; |
---|
309 | 10 [label="zig.o"]; |
---|
310 | 11 -> 12; |
---|
311 | 11 [label="zag.cpp"]; |
---|
312 | 12 -> 13; |
---|
313 | 12 [label="zag.o"]; |
---|
314 | 13 -> 14; |
---|
315 | 13 [label="libzigzag.a"]; |
---|
316 | 14; |
---|
317 | 14 [label="killerapp"]; |
---|
318 | edge[style="dotted"]; |
---|
319 | 6 -> 0; |
---|
320 | } |
---|
321 | </pre> |
---|
322 | |
---|
323 | <p>The examples directory contains an <a |
---|
324 | href="../example/graphviz.cpp">example using |
---|
325 | <code>dynamic_properties</code></a>.</p> |
---|
326 | |
---|
327 | <h3>See Also</h3> |
---|
328 | |
---|
329 | <a href="./read_graphviz.html"><tt>read_graphviz</tt></a> |
---|
330 | |
---|
331 | <h3>Notes</h3> |
---|
332 | Note that you can use Graphviz dot file write facilities |
---|
333 | without the library <tt>libbglviz.a</tt>. |
---|
334 | |
---|
335 | <br> |
---|
336 | <HR> |
---|
337 | <TABLE> |
---|
338 | <TR valign=top> |
---|
339 | <TD nowrap>Copyright © 2000-2001</TD><TD> |
---|
340 | <A HREF="../../../people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br> |
---|
341 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>) |
---|
342 | </TD></TR></TABLE> |
---|
343 | |
---|
344 | </BODY> |
---|
345 | </HTML> |
---|