[29] | 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 VertexListGraph > |
---|
| 27 | void |
---|
| 28 | write_graphviz(std::ostream& out, const VertexListGraph& g); |
---|
| 29 | |
---|
| 30 | // Graph structure with customized property output |
---|
| 31 | template < typename VertexListGraph, typename VertexPropertyWriter > |
---|
| 32 | void |
---|
| 33 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
| 34 | VertexPropertyWriter vpw); |
---|
| 35 | |
---|
| 36 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
| 37 | typename EdgePropertyWriter > |
---|
| 38 | void |
---|
| 39 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
| 40 | VertexPropertyWriter vpw, EdgePropertyWriter epw); |
---|
| 41 | |
---|
| 42 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
| 43 | typename EdgePropertyWriter, typename GraphPropertyWriter > |
---|
| 44 | void |
---|
| 45 | write_graphviz(std::ostream& out, const VertexListGraph& g, |
---|
| 46 | VertexPropertyWriter vpw, EdgePropertyWriter epw, |
---|
| 47 | GraphPropertyWriter gpw); |
---|
| 48 | |
---|
| 49 | template < typename VertexListGraph, typename VertexPropertyWriter, |
---|
| 50 | typename EdgePropertyWriter, typename GraphPropertyWriter, |
---|
| 51 | typename VertexID > |
---|
| 52 | void |
---|
| 53 | write_graphviz(std::ostream& out, const VertexListGraph& 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(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(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 |
---|
| 73 | 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 |
---|
| 74 | 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 final two overloads of <code>write_graphviz</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.</p> |
---|
| 106 | |
---|
| 107 | <H3>Where Defined</H3> |
---|
| 108 | |
---|
| 109 | <P> |
---|
| 110 | <a href="../../../boost/graph/graphviz.hpp"><TT>boost/graph/graphviz.hpp</TT></a> |
---|
| 111 | |
---|
| 112 | <h3><A NAME="concept:PropertyWriter"> |
---|
| 113 | <tt>PropertyWriter</tt> |
---|
| 114 | </h3> |
---|
| 115 | |
---|
| 116 | PropertyWriter is used in the <tt>write_graphviz</tt> function to |
---|
| 117 | print properties of vertex, edge or graph. There are two types of |
---|
| 118 | PropertyWriter. One is for a vertex or edge. The other is for a graph. |
---|
| 119 | Thus, users could easily extend the <tt>write_graphviz</tt> function |
---|
| 120 | by creating their own <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> only. |
---|
| 121 | <p> |
---|
| 122 | A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> |
---|
| 123 | for vertices or edges is a functor which can be called with two parameters: |
---|
| 124 | <tt>std::ostream</tt> and either a vertex or an edge descriptor. It should output a |
---|
| 125 | pair of brackets with a series of assigments "name=value" inside. |
---|
| 126 | Each assignment should be separated either with space, with comma, or with semicolon. |
---|
| 127 | The following functor, provided by BGL, is the example of PropertyWriter for |
---|
| 128 | vertices or edges. It is used to print the label of each vertex or edge. |
---|
| 129 | <pre> |
---|
| 130 | template < class Name > |
---|
| 131 | class label_writer { |
---|
| 132 | public: |
---|
| 133 | label_writer(Name _name) : name(_name) {} |
---|
| 134 | template <class VertexOrEdge> |
---|
| 135 | void operator()(std::ostream& out, const VertexOrEdge& v) const { |
---|
| 136 | out << "[label=\"" << name[v] << "\"]"; |
---|
| 137 | } |
---|
| 138 | private: |
---|
| 139 | Name name; |
---|
| 140 | }; |
---|
| 141 | </pre> |
---|
| 142 | |
---|
| 143 | <p> |
---|
| 144 | A function to conveniently create this writer is provided: |
---|
| 145 | <pre> |
---|
| 146 | template < class Name > |
---|
| 147 | label_writer<Name> |
---|
| 148 | make_label_writer(Name n); |
---|
| 149 | </pre> |
---|
| 150 | |
---|
| 151 | <p> |
---|
| 152 | A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> |
---|
| 153 | for graphs is a functor which is called with one parameter of type |
---|
| 154 | <tt>std::ostream</tt> and should print a series of graph properties. The following |
---|
| 155 | code excerpt is an example of a PropertyWriter for a graph. |
---|
| 156 | <pre> |
---|
| 157 | struct sample_graph_writer { |
---|
| 158 | void operator()(std::ostream& out) const { |
---|
| 159 | out << "graph [bgcolor=lightgrey]" << std::endl; |
---|
| 160 | out << "node [shape=circle color=white]" << std::endl; |
---|
| 161 | out << "edge [style=dashed]" << std::endl; |
---|
| 162 | } |
---|
| 163 | }; |
---|
| 164 | } |
---|
| 165 | </pre> |
---|
| 166 | |
---|
| 167 | <p> |
---|
| 168 | There exists a class <tt>default_writer</tt>, which can be used as both |
---|
| 169 | vertex/edge and graph property writer, and does nothing. It comes handy when |
---|
| 170 | only edge properties must be written, but function interface requries to pass |
---|
| 171 | vertex property writer as well. |
---|
| 172 | |
---|
| 173 | <h3>Parameters</h3> |
---|
| 174 | OUT: <tt>std::ostream& out</tt> |
---|
| 175 | <blockquote> |
---|
| 176 | A standard <tt>std::ostream</tt> object. |
---|
| 177 | </blockquote> |
---|
| 178 | |
---|
| 179 | IN: <tt>VertexListGraph& g</tt> |
---|
| 180 | <blockquote> |
---|
| 181 | A directed or undirected graph. The graph's type must be a model of |
---|
| 182 | <a href="./VertexListGraph.html">VertexListGraph</a>. Also the graph |
---|
| 183 | must have an internal <tt>vertex_index</tt> property map. |
---|
| 184 | </blockquote> |
---|
| 185 | |
---|
| 186 | IN: <tt>VertexPropertyWriter vpw</tt> |
---|
| 187 | <blockquote> |
---|
| 188 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
| 189 | properties of a vertex.<br> |
---|
| 190 | <b>Default</b>: <tt>default_writer()</tt> |
---|
| 191 | </blockquote> |
---|
| 192 | |
---|
| 193 | IN: <tt>EdgePropertyWriter epw</tt> |
---|
| 194 | <blockquote> |
---|
| 195 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
| 196 | properties of an edge.<br> |
---|
| 197 | <b>Default</b>: <tt>default_writer()</tt> |
---|
| 198 | </blockquote> |
---|
| 199 | |
---|
| 200 | IN: <tt>GraphPropertyWriter epw</tt> |
---|
| 201 | <blockquote> |
---|
| 202 | A functor that models <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> concept to print |
---|
| 203 | properties of a graph.<br> |
---|
| 204 | <b>Default</b>: <tt>default_writer()</tt> |
---|
| 205 | </blockquote> |
---|
| 206 | |
---|
| 207 | IN: <tt>dynamic_properties& dp</tt> |
---|
| 208 | <blockquote> |
---|
| 209 | Contains all of the vertex and edge properties that should be |
---|
| 210 | emitted by the GraphViz writer. |
---|
| 211 | </blockquote> |
---|
| 212 | |
---|
| 213 | IN: <tt>const std::string& node_id</tt> |
---|
| 214 | <blockquote> |
---|
| 215 | The name of the property map that provides identifiers for the |
---|
| 216 | vertices in the graph.<br> |
---|
| 217 | <b>Default</b>: <tt>"node_id"</tt> |
---|
| 218 | </blockquote> |
---|
| 219 | |
---|
| 220 | IN: <tt>VertexID vertex_id</tt> |
---|
| 221 | <blockquote> |
---|
| 222 | 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> |
---|
| 223 | <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>. |
---|
| 224 | </blockquote> |
---|
| 225 | <H3> |
---|
| 226 | Example |
---|
| 227 | </H3> |
---|
| 228 | |
---|
| 229 | This example demonstrates using BGL-graphviz interface to write |
---|
| 230 | a BGL graph into a graphviz format file. |
---|
| 231 | |
---|
| 232 | <pre> |
---|
| 233 | #include <boost/graph/graphviz.hpp> |
---|
| 234 | |
---|
| 235 | enum files_e { dax_h, yow_h, boz_h, zow_h, foo_cpp, |
---|
| 236 | foo_o, bar_cpp, bar_o, libfoobar_a, |
---|
| 237 | zig_cpp, zig_o, zag_cpp, zag_o, |
---|
| 238 | libzigzag_a, killerapp, N }; |
---|
| 239 | const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp", |
---|
| 240 | "foo.o", "bar.cpp", "bar.o", "libfoobar.a", |
---|
| 241 | "zig.cpp", "zig.o", "zag.cpp", "zag.o", |
---|
| 242 | "libzigzag.a", "killerapp" }; |
---|
| 243 | |
---|
| 244 | int main(int,char*[]) |
---|
| 245 | { |
---|
| 246 | |
---|
| 247 | typedef pair<int,int> Edge; |
---|
| 248 | Edge used_by[] = { |
---|
| 249 | Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h), |
---|
| 250 | Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp), |
---|
| 251 | Edge(boz_h, bar_cpp), Edge(boz_h, zig_cpp), Edge(boz_h, zag_cpp), |
---|
| 252 | Edge(zow_h, foo_cpp), |
---|
| 253 | Edge(foo_cpp, foo_o), |
---|
| 254 | Edge(foo_o, libfoobar_a), |
---|
| 255 | Edge(bar_cpp, bar_o), |
---|
| 256 | Edge(bar_o, libfoobar_a), |
---|
| 257 | Edge(libfoobar_a, libzigzag_a), |
---|
| 258 | Edge(zig_cpp, zig_o), |
---|
| 259 | Edge(zig_o, libzigzag_a), |
---|
| 260 | Edge(zag_cpp, zag_o), |
---|
| 261 | Edge(zag_o, libzigzag_a), |
---|
| 262 | Edge(libzigzag_a, killerapp) |
---|
| 263 | }; |
---|
| 264 | const int nedges = sizeof(used_by)/sizeof(Edge); |
---|
| 265 | int weights[nedges]; |
---|
| 266 | std::fill(weights, weights + nedges, 1); |
---|
| 267 | |
---|
| 268 | using namespace boost; |
---|
| 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(used_by, used_by + nedges, weights, N); |
---|
| 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> |
---|