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. 26template < typename VertexAndEdgeListGraph > 27void 28write_graphviz(std::ostream& out, const VertexAndEdgeListGraph& g); 29 30// Graph structure with customized property output 31template < typename VertexAndEdgeListGraph, typename VertexPropertyWriter > 32void 33write_graphviz(std::ostream& out, const VertexAndEdgeListGraph& g, 34 VertexPropertyWriter vpw); 35 36template < typename VertexAndEdgeListGraph, typename VertexPropertyWriter, 37 typename EdgePropertyWriter > 38void 39write_graphviz(std::ostream& out, const VertexAndEdgeListGraph& g, 40 VertexPropertyWriter vpw, EdgePropertyWriter epw); 41 42template < typename VertexAndEdgeListGraph, typename VertexPropertyWriter, 43 typename EdgePropertyWriter, typename GraphPropertyWriter > 44void 45write_graphviz(std::ostream& out, const VertexAndEdgeListGraph& g, 46 VertexPropertyWriter vpw, EdgePropertyWriter epw, 47 GraphPropertyWriter gpw); 48 49template < typename VertexAndEdgeListGraph, typename VertexPropertyWriter, 50 typename EdgePropertyWriter, typename GraphPropertyWriter, 51 typename VertexID > 52void 53write_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 58template<typename Graph> 59void 60write_graphviz_dp(std::ostream& out, const Graph& g, 61 const dynamic_properties& dp, 62 const std::string& node_id = "node_id"); 63 64template<typename Graph, typename VertexID> 65void 66write_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> 72This is to write a BGL graph object into an output stream in graphviz dot format 73so that users can make use of <a href="http://www.graphviz.org/">graphviz</a> 74to draw a picture with nice layout. 75<p> 76The 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 78ID. If users have either interior or exterior properties for each vertex 79in the graph, the second version above provides a way to print those 80properties into the graphviz format file. For example, if each vertex 81in the graph has its label through property map object <tt>name</tt>, 82users can use the second version: 83<pre> 84 write_graph(out, g, make_label_writer(name)); 85</pre> 86The utility function <tt>make_label_writer</tt> returns a predefined 87<a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> for 88vertex labels. Similarly, the third 89version 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>, 93respectively. 94 95<p> The two overloads of <code>write_graphviz_dp</code> will emit 96all of the properties stored in the <a 97href="../../property_map/doc/dynamic_property_map.html"><code>dynamic_properties</a></code> 98object, thereby retaining the properties that have been read in 99through the dual function <a 100href="read_graphviz.html"><code>read_graphviz</code></a>. With these 101overloads, <code>node_id</code> is the name of the property map that 102stores the IDs of each node for use in the output (if it is stored in 103the <code>dynamic_properties</code> structure); alternatively, one may 104provide an arbitrary property map for <code>vertex_id</code> giving the 105vertex 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 118PropertyWriter is used in the <tt>write_graphviz</tt> function to 119print vertex, edge or graph properties. There are two types of 120PropertyWriter. One is for a vertex or edge. The other is for a graph. 121Thus, users could easily extend the <tt>write_graphviz</tt> function 122by creating their own <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> only. 123<p> 124A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> 125for 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 127pair of brackets with a series of assigments "name=value" inside. 128Each assignment should be separated either with space, with comma, or with semicolon. 129The following functor, provided by BGL, is the example of PropertyWriter for 130vertices 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> 146A function to conveniently create this writer is provided: 147<pre> 148template < class Name > 149label_writer<Name> 150make_label_writer(Name n); 151</pre> 152 153<p> 154A <a href="./write-graphviz.html#concept:PropertyWriter">PropertyWriter</a> 155for 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 157code 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> 170There exists a class <tt>default_writer</tt>, which can be used as both 171vertex/edge and graph property writer, and does nothing. It is useful when 172only edge properties must be written, but the function interface also requires a 173vertex 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 209IN: <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 215IN: <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 222IN: <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> 228Example 229</H3> 230 231This example demonstrates using the BGL-Graphviz interface to write 232a BGL graph into a Graphviz format file. 233 234<pre> 235#include <boost/graph/graphviz.hpp> 236 237enum 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 }; 241const 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 246int 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 282The output will be: 283<pre> 284digraph G { 2850 -> 4; 2860 -> 6; 2870 -> 1; 2880 [label="dax.h"]; 2891 -> 6; 2901 -> 11; 2911 [label="yow.h"]; 2922 -> 6; 2932 -> 9; 2942 -> 11; 2952 [label="boz.h"]; 2963 -> 4; 2973 [label="zow.h"]; 2984 -> 5; 2994 [label="foo.cpp"]; 3005 -> 8; 3015 [label="foo.o"]; 3026 -> 7; 3036 [label="bar.cpp"]; 3047 -> 8; 3057 [label="bar.o"]; 3068 -> 13; 3078 [label="libfoobar.a"]; 3089 -> 10; 3099 [label="zig.cpp"]; 31010 -> 13; 31110 [label="zig.o"]; 31211 -> 12; 31311 [label="zag.cpp"]; 31412 -> 13; 31512 [label="zag.o"]; 31613 -> 14; 31713 [label="libzigzag.a"]; 31814; 31914 [label="killerapp"]; 3206 -> 0; 321} 322</pre> 323 324<p>The examples directory contains an <a 325href="../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> 343