stanford_graph.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <HTML>
  2. <!--
  3. Copyright (C) 2001, Andreas Scherer, Jeremy Siek, Lie-Quan Lee,
  4. and Andrew Lumsdaine
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. -->
  9. <Head>
  10. <Title>Boost Graph Library: Stanford Graph Interface</Title>
  11. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  12. ALINK="#ff0000">
  13. <IMG SRC="../../../boost.png"
  14. ALT="C++ Boost" width="277" height="86">
  15. <BR Clear>
  16. <H1>
  17. Using SGB Graphs in BGL
  18. </H1>
  19. The Boost Graph Library (BGL) header, <a
  20. href="../../../boost/graph/stanford_graph.hpp"
  21. ><tt>&lt;boost/graph/stanford_graph.hpp&gt;</tt></a>, adapts a
  22. Stanford GraphBase (SGB) <tt>Graph</tt> pointer into a BGL-compatible
  23. <a href="./VertexListGraph.html">VertexListGraph</a>.&nbsp; Note that
  24. a graph adaptor <b>class</b> is <i>not</i> used; SGB's <tt>Graph*</tt>
  25. itself becomes a model of VertexListGraph.&nbsp; The VertexListGraph
  26. concept is fulfilled by defining the appropriate non-member functions
  27. for <tt>Graph*</tt>.
  28. <H2><a name="sec:SGB"></a>
  29. The Stanford GraphBase
  30. </H2>
  31. <P>
  32. "The <a href="http://www-cs-staff.stanford.edu/~knuth/sgb.html">Stanford
  33. GraphBase</a> (SGB) is a collection of datasets and computer programs that
  34. generate and examine a wide variety of graphs and networks."&nbsp; The SGB was
  35. developed and published by
  36. <a href="http://www-cs-staff.stanford.edu/~knuth">Donald E. Knuth</a>
  37. in 1993.&nbsp; The fully documented source code is available for anonymous ftp
  38. from <a href="ftp://labrea.stanford.edu/pub/sgb/sgb.tar.gz">Stanford
  39. University</a> and in the book "The Stanford GraphBase, A Platform for
  40. Combinatorial Computing," published jointly by ACM Press and Addison-Wesley
  41. Publishing Company in 1993.&nbsp; (This book contains several chapters with
  42. additional information not available in the electronic distribution.)
  43. <H3><a name="sec:CWEB"></a>
  44. Prerequisites
  45. </H3>
  46. The source code of SGB is written in accordance with the rules of the
  47. <a href="http://www-cs-staff.stanford.edu/~knuth/lp.html">Literate
  48. Programming</a> paradigm, so you need to make sure that your computer supports
  49. the <a href="http://www-cs-staff.stanford.edu/~knuth/cweb.html">CWEB</a>
  50. system.&nbsp; The CWEB sources are available for anonymous ftp from
  51. <a href="ftp://labrea.stanford.edu/pub/cweb/cweb.tar.gz">Stanford
  52. University</a>.&nbsp; Bootstrapping CWEB on Unix systems is elementary and
  53. documented in the CWEB distribution; pre-compiled binary executables of the
  54. CWEB tools for Win32 systems are available from
  55. <a href="http://www.literateprogramming.com">www.literateprogramming.com</a>.
  56. <H3><a name="sec:SGB:Installation"></a>
  57. Installing the SGB
  58. </H3>
  59. After you have acquired the <a href="#sec:SGB">SGB sources</a> and have
  60. installed a working <a href="#sec:CWEB">CWEB system</a> (at least the
  61. "ctangle" processor is required), you're almost set for compiling the SGB
  62. sources.&nbsp; SGB is written in "old-style C," but the Boost Graph Library
  63. expects to handle "modern C" and C++.&nbsp; Fortunately, the SGB distribution
  64. comes with an appropriate set of patches that convert all the sources from
  65. "KR-C" to "ANSI-C," thus allowing for smooth integration of the Stanford
  66. GraphBase in the Boost Graph Library.
  67. <ul>
  68. <li>
  69. <b>Unix</b>: After extracting the SGB archive, but prior to invoking
  70. "<tt>make tests</tt>" and "<tt>make install</tt>," you should say
  71. "<tt>ln -s PROTOTYPES/*.ch .</tt>" in the root directory where you extracted
  72. the SGB files (or you can simply copy the change files next to the proper
  73. source files).&nbsp; The Unix <tt>Makefile</tt> coming with SGB conveniently
  74. looks for "change files" matching the SGB source files and automatically
  75. applies them with the "ctangle" processor.&nbsp; The resulting C files will
  76. smoothly run through the compiler.
  77. </li>
  78. <li>
  79. <b>Win32</b>: The "MSVC" subdirectory of the SGB distribution contains a
  80. complete set of "Developer Studio Projects" (and a single "Workspace"),
  81. applicable with Microsoft Developer Studio 6.&nbsp; The installation process
  82. is documented in the accompanying file <tt>README.MSVC</tt>.&nbsp; The "MSVC"
  83. contribution has been updated to make use of the "PROTOTYPES" as well, so you
  84. don't need to worry about that.
  85. </li>
  86. </ul>
  87. <H3><a name="sec:UsingSGB"></a>
  88. Using the SGB
  89. </H3>
  90. After you have run <a href="#sec:SGB:Installation">the installation
  91. process</a> of the SGB, you can use the BGL graph interface with the
  92. SGB <tt>Graph*</tt>, <a href="../../../boost/graph/stanford_graph.hpp"
  93. ><tt>&lt;boost/graph/stanford_graph.hpp&gt;</tt></a>, which will be
  94. described <a href="#sec:BGL:Interface">next</a>.&nbsp; All you have to
  95. do is tell the C++ compiler where to look for the SGB headerfiles (by
  96. default, <tt>/usr/local/sgb/include</tt> on Unix and the "MSVC"
  97. subdirectory of the SGB installation on Win32) and the linker where to
  98. find the SGB static library file (<tt>libgb.a</tt> on Unix and
  99. <tt>libgb.lib</tt> on Win32); consult the documentation of your
  100. particular compiler about how to do that.
  101. <H3><a name="sec:SGB:Problems"></a>
  102. Technicalities
  103. </H3>
  104. <ul>
  105. <li>
  106. <b>Headerfile selection</b>: The two SGB modules <tt>gb_graph</tt> and
  107. <tt>gb_io</tt> use the preprocessor switch <tt>SYSV</tt> to select either the
  108. headerfile <tt>&lt;string.h&gt;</tt> (if <tt>SYSV</tt> is <tt>#define</tt>d)
  109. or the headerfile <tt>&lt;strings.h&gt;</tt> (if <tt>SYSV</tt> is <i>not</i>
  110. <tt>#define</tt>d).&nbsp; Some compilers, like <tt>gcc</tt>/<tt>g++</tt>,
  111. don't care much (<tt>gcc</tt> "knows" about the "string" functions without
  112. referring to <tt>&lt;string.h&gt;</tt>), but others, like MSVC on Win32, do (so
  113. all "Developer Studio Projects" in the "MSVC" subdirectory of the
  114. <a href="#sec:SGB">SGB distribution</a> appropriately define <tt>SYSV</tt>).
  115. You should be careful to set (or not) <tt>SYSV</tt> according to the needs of
  116. your compiler.
  117. </li>
  118. <li>
  119. <b>Missing include guards</b>: None of the SGB headerfiles uses "internal
  120. include guards" to protect itself from multiple inclusion.&nbsp; To avoid
  121. trouble, you must <i>not</i> <tt>#include</tt> any of the SGB headerfiles
  122. before or after <a href="#sec:Wrapper">the BGL wrapper</a> in a compilation
  123. unit; it will fully suffice to use the BGL interface.
  124. </li>
  125. <li>
  126. <b>Preprocessor macros</b>: The SGB headerfiles make liberal use of the
  127. preprocessor <i>without</i> sticking to a particular convention (like
  128. all-uppercase names or a particular prefix).&nbsp; At the time of writing,
  129. already three of these preprocessor macros collide with the conventions of
  130. either C++, g++, or BGL, and are fixed in <a href="#sec:Wrapper">the BGL
  131. wrapper</a>.&nbsp; We can not guarantee that no other preprocessor-induced
  132. problems may arise (but we are willing to learn about any such collisions).
  133. </li>
  134. </ul>
  135. <H2><a name="sec:BGL:Interface"></a>
  136. The BGL Interface for the SGB
  137. </H2>
  138. <H3><a name="sec:Wrapper"></a>
  139. Where Defined
  140. </H3>
  141. <a href="../../../boost/graph/stanford_graph.hpp"
  142. ><tt>&lt;boost/graph/stanford_graph.hpp&gt;</tt></a>
  143. <p> The main purpose of this Boost Graph Library (BGL) headerfile is to
  144. <tt>#include</tt> all global definitions for the general stuff of the
  145. <a href="#sec:SGB">Stanford GraphBase</a> (SGB) and its various graph generator
  146. functions by reading all <a href="#sec:SGB:Problems">SGB headerfiles</a> as in
  147. section 2 of the "<tt>test_sample</tt>" program.
  148. <p> On top of the SGB stuff, the BGL <tt>stanford_graph.hpp</tt>
  149. header adds and defines appropriate types and functions for using the
  150. SGB graphs in the BGL framework.&nbsp; Apart from the improved
  151. interface, the <a href="#sec:UsingSGB">SGB (static) library</a> is
  152. used "as is" in the context of BGL.
  153. <H3>
  154. Model Of
  155. </H3>
  156. <a href="./VertexListGraph.html">Vertex List Graph</a> and <a
  157. href="./PropertyGraph.html">Property Graph</a>. The set of property
  158. tags that can be used with the SGB graph is described in the <a
  159. href="#properties">Vertex and Edge Properties</a> section below.
  160. <H3><a name="sec:Example"></a>
  161. Example
  162. </H3>
  163. The example program <a href="../example/miles_span.cpp">
  164. <tt>&lt;example/miles_span.cpp&gt;</tt></a> represents the first
  165. application of the generic framework of BGL to an SGB graph.&nbsp; It
  166. uses Prim's algorithm to solve the "minimum spanning tree"
  167. problem.&nbsp; In addition, the programs <a
  168. href="../../../libs/graph/example/girth.cpp">
  169. <tt>&lt;example/girth.cpp&gt;</tt></a> and <a
  170. href="../example/roget_components.cpp">
  171. <tt>&lt;example/roget_components.cpp&gt;</tt></a> have been ported
  172. from the SGB.&nbsp; We intend to implement more algorithms from SGB in
  173. a generic fashion and to provide the remaining example programs of SGB
  174. for the BGL framework.&nbsp; If you would like to help, feel free to
  175. submit your contributions!
  176. <H3>
  177. Associated Types
  178. </H3>
  179. <hr>
  180. <tt>graph_traits&lt;Graph*&gt;::vertex_descriptor</tt><br><br>
  181. The type for the vertex descriptors associated with the <tt>Graph*</tt>.
  182. We use the type <tt>Vertex*</tt> as the vertex descriptor.
  183. <hr>
  184. <tt>graph_traits&lt;Graph*&gt;::edge_descriptor</tt><br><br> The type
  185. for the edge descriptors associated with the <tt>Graph*</tt>. This is
  186. the type <tt>boost::sgb_edge</tt>. In addition to supporting all the
  187. required operations of a BGL edge descriptor, the
  188. <tt>boost::sgb_edge</tt> class has the following constructor.
  189. <pre>
  190. sgb_edge::sgb_edge(Arc* arc, Vertex* source)
  191. </pre>
  192. <hr>
  193. <tt>graph_traits&lt;Graph*&gt;::vertex_iterator</tt><br><br>
  194. The type for the iterators returned by <tt>vertices()</tt>.
  195. <hr>
  196. <tt>graph_traits&lt;Graph*&gt;::out_edge_iterator</tt><br><br>
  197. The type for the iterators returned by <tt>out_edges()</tt>.
  198. <hr>
  199. <tt>graph_traits&lt;Graph*&gt;::adjacency_iterator</tt><br><br>
  200. The type for the iterators returned by <tt>adjacent_vertices()</tt>.
  201. <hr>
  202. <tt>graph_traits&lt;Graph*&gt;::vertices_size_type</tt><br><br>
  203. The type used for dealing with the number of vertices in the graph.
  204. <hr>
  205. <tt>graph_traits&lt;Graph*&gt;::edge_size_type</tt><br><br>
  206. The type used for dealing with the number of edges in the graph.
  207. <hr>
  208. <tt>graph_traits&lt;Graph*&gt;::degree_size_type</tt><br><br>
  209. The type used for dealing with the number of edges incident to a vertex
  210. in the graph.
  211. <hr>
  212. <tt>graph_traits&lt;Graph*&gt;::directed_category</tt><br><br>
  213. Provides information about whether the graph is directed or
  214. undirected. An SGB <tt>Graph*</tt> is directed so this type is
  215. <tt>directed_tag</tt>.
  216. <hr>
  217. <tt>graph_traits&lt;Graph*&gt;::traversal_category</tt><br><br>
  218. An SGB <tt>Graph*</tt> provides traversal of the vertex set,
  219. out edges, and adjacent vertices. Therefore the traversal category
  220. tag is defined as follows:
  221. <pre>
  222. struct sgb_traversal_tag :
  223. public virtual vertex_list_graph_tag,
  224. public virtual incidence_graph_tag,
  225. public virtual adjacency_graph_tag { };
  226. </pre>
  227. <hr>
  228. <tt>graph_traits&lt;Graph*&gt;::edge_parallel_category</tt><br><br>
  229. This describes whether the graph class allows the insertion of parallel edges
  230. (edges with the same source and target).&nbsp; The SGB <tt>Graph*</tt>
  231. does not prevent addition of parallel edges, so this type
  232. is <tt>allow_parallel_edge_tag</tt>.
  233. <hr>
  234. <H3>
  235. Non-Member Functions
  236. </H3>
  237. <hr>
  238. <pre>
  239. std::pair&lt;vertex_iterator,&nbsp;vertex_iterator&gt;
  240. vertices(Graph*&nbsp;g)
  241. </pre>
  242. Returns an iterator-range providing access to the vertex set of graph
  243. <tt>g</tt>.
  244. <hr>
  245. <pre>
  246. std::pair&lt;out_edge_iterator,&nbsp;out_edge_iterator&gt;
  247. out_edges(vertex_descriptor&nbsp;v, Graph*&nbsp;g)
  248. </pre>
  249. Returns an iterator-range providing access to the out-edges of vertex
  250. <tt>v</tt> in graph <tt>g</tt>.<br>
  251. There is no corresponding <tt>in_edges</tt> function.
  252. <hr>
  253. <pre>
  254. std::pair&lt;adjacency_iterator,&nbsp;adjacency_iterator&gt;
  255. adjacent_vertices(vertex_descriptor&nbsp;v, Graph*&nbsp;g)
  256. </pre>
  257. Returns an iterator-range providing access to the adjacent vertices of vertex
  258. <tt>v</tt> in graph <tt>g</tt>.
  259. <hr>
  260. <pre>
  261. vertex_descriptor
  262. source(edge_descriptor&nbsp;e, Graph*&nbsp;g)
  263. </pre>
  264. Returns the source vertex of edge <tt>e</tt>.
  265. <hr>
  266. <pre>
  267. vertex_descriptor
  268. target(edge_descriptor&nbsp;e, Graph*&nbsp;g)
  269. </pre>
  270. Returns the target vertex of edge <tt>e</tt>.
  271. <hr>
  272. <pre>
  273. degree_size_type
  274. out_degree(vertex_descriptor&nbsp;v, Graph*&nbsp;g)
  275. </pre>
  276. Returns the number of edges leaving vertex <tt>v</tt>.<br>
  277. There is no corresponding <tt>in_degree</tt> function.
  278. <hr>
  279. <pre>
  280. vertices_size_type
  281. num_vertices(Graph*&nbsp;g)
  282. </pre>
  283. Returns the number of vertices in the graph <tt>g</tt>.
  284. <hr>
  285. <pre>
  286. edge_size_type
  287. num_edges(Graph*&nbsp;g)
  288. </pre>
  289. Returns the number of edges in the graph <tt>g</tt>.
  290. <hr>
  291. <pre>
  292. vertex_descriptor
  293. vertex(vertices_size_type&nbsp;n, Graph*&nbsp;g)
  294. </pre>
  295. Returns the (0-based) nth vertex in the graph's vertex list.
  296. <hr>
  297. <pre>
  298. template &lt;class <a href="./PropertyTag.html">PropertyTag</a>&gt;
  299. property_map&lt;Graph*, PropertyTag&gt;::type
  300. get(PropertyTag, Graph*&amp; g)
  301. template &lt;class <a href="./PropertyTag.html">PropertyTag</a>&gt;
  302. property_map&lt;Graph*, Tag&gt;::const_type
  303. get(PropertyTag, const Graph*&amp; g)
  304. </pre>
  305. Returns the property map object for the vertex property specified by
  306. <TT>PropertyTag</TT>. The <TT>PropertyTag</TT> must be one of
  307. the described below.
  308. <hr>
  309. <h3><a name="properties">Vertex and Edge Properties</a></h3>
  310. The SGB <tt>Vertex</tt> and <tt>Arc</tt> structures provide
  311. &quot;utility&quot; fields for storing extra information. We provide
  312. BGL wrappers that provide access to these fields through <a
  313. href="../../property_map/doc/property_map.html">property maps</a>. In
  314. addition, vertex index and edge length maps are provided. A property
  315. map object can be obtained from a SGB <tt>Graph*</tt> using the
  316. <tt>get()</tt> function described in the <a
  317. href="./PropertyGraph.html">Property Graph</a> concept.
  318. <p>
  319. The following list of property tags can be used to specify which
  320. utility field you would like a property map for.
  321. </p>
  322. <pre>
  323. <i>// vertex properties</i>
  324. template &lt;class T&gt; u_property;
  325. template &lt;class T&gt; v_property;
  326. template &lt;class T&gt; w_property;
  327. template &lt;class T&gt; x_property;
  328. template &lt;class T&gt; y_property;
  329. template &lt;class T&gt; z_property;
  330. <i>// edge properties</i>
  331. template &lt;class T&gt; a_property;
  332. template &lt;class T&gt; b_property;
  333. </pre>
  334. <p>
  335. The template parameter <tt>T</tt> for these tags is limited to the
  336. types in the <tt>util</tt> union declared in the SGB header
  337. <tt>gb_graph.h</tt>, which are <tt>Vertex*</tt>, <tt>Arc*</tt>,
  338. <tt>Graph*</tt>, <tt>char*</tt>, and <tt>long</tt>. The property maps
  339. for the utility fields are models of <a
  340. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property
  341. Map</a>.
  342. </p>
  343. <p>
  344. The property map for vertex indices can be obtained using the
  345. <tt>vertex_index_t</tt> tag, and this property map is a <a
  346. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  347. Map</a>. A property map for edge length's can be obtained using the
  348. <tt>edge_length_t</tt> tag, and this property map is a <a
  349. href="../../property_map/doc/LvaluePropertyMap.html">Lvalue Property
  350. Map</a> whose value type is <tt>long</tt>.
  351. </p>
  352. <p>
  353. Property map objects can be obtained via the <tt>get()</tt> function
  354. of the Property Graph concept. The type of the property map is given
  355. by the <a href="./property_map.html"><tt>property_map</tt></a> traits
  356. class.</p>
  357. <HR>
  358. <TABLE>
  359. <TR valign=top>
  360. <TD nowrap>Copyright &copy; 2001</TD><TD>
  361. <A HREF="http://people.freenet.de/andreas.scherer">Andreas Scherer</A>,
  362. Aachen (<A
  363. HREF="mailto:andreas_freenet@freenet.de">andreas_freenet@freenet.de</A>)<br>
  364. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  365. Indiana University (<A
  366. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  367. <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>,
  368. Indiana University (<A
  369. HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
  370. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  371. Indiana University (<A
  372. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  373. </TD></TR></TABLE>
  374. </BODY>
  375. </HTML>