write-graphviz.html 12 KB

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