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