index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <HTML>
  4. <!--
  5. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  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. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
  12. <Title>The Boost Graph Library</Title>
  13. <BODY bgcolor="#ffffff" link="#0000ee" text="#000000" vlink="#551a8b"
  14. alink="#ff0000">
  15. <IMG src="../../../boost.png"
  16. alt="C++ Boost" width="277" height="86">
  17. <h1>The Boost Graph Library (BGL)
  18. <a href="http://www.awprofessional.com/title/0201729148">
  19. <img src="bgl-cover.jpg" alt="BGL Book" align="RIGHT"></a>
  20. </h1>
  21. <P>
  22. Graphs are mathematical abstractions that are useful for solving many
  23. types of problems in computer science. Consequently, these
  24. abstractions must also be represented in computer programs. A
  25. standardized generic interface for traversing graphs is of utmost
  26. importance to encourage reuse of graph algorithms and data structures.
  27. Part of the Boost Graph Library is a generic interface that allows
  28. access to a graph's structure, but hides the details of the
  29. implementation. This is an &ldquo;open&rdquo; interface in the sense that any
  30. graph library that implements this interface will be interoperable
  31. with the BGL generic algorithms and with other algorithms that also
  32. use this interface. The BGL provides some general purpose graph classes
  33. that conform to this interface, but they are not meant to be the
  34. &ldquo;only&rdquo; graph classes; there certainly will be other graph classes
  35. that are better for certain situations. We believe that the main
  36. contribution of the The BGL is the formulation of this interface.
  37. <P>
  38. The BGL graph interface and graph components are <I>generic</I>, in the
  39. same sense as the Standard Template Library (STL)&nbsp;[<A
  40. HREF="bibliography.html#austern99:_gener_progr_stl">2</A>].
  41. In the following sections, we review the role that generic programming
  42. plays in the STL and compare that to how we applied generic
  43. programming in the context of graphs.
  44. <P>
  45. Of course, if you are already familiar with generic programming,
  46. please dive right in! Here's the <a
  47. href="./table_of_contents.html">Table of Contents</a>. For distributed-memory
  48. parallelism, you can also look at the <a
  49. href="../../graph_parallel/doc/html/index.html">Parallel BGL</a>.
  50. <P>
  51. The source for the BGL is available as part of the Boost distribution,
  52. which you can <a href="http://sourceforge.net/project/showfiles.php?group_id=7586">
  53. download from here</a>.
  54. <H2>How to Build the BGL</H2>
  55. <p><b>DON'T!</b> The Boost Graph Library is a header-only library and
  56. does not need to be built to be used. The only exceptions are the <a
  57. href="read_graphviz.html">GraphViz input parser</a> and the <a
  58. href="read_graphml.html">GraphML parser</a>.</p>
  59. <p>When compiling programs that use the BGL, <b>be sure to compile
  60. with optimization</b>. For instance, select &ldquo;Release&rdquo; mode with
  61. Microsoft Visual C++ or supply the flag <tt>-O2</tt> or <tt>-O3</tt>
  62. to GCC. </p>
  63. <H2>Genericity in STL</H2>
  64. <P>
  65. There are three ways in which the STL is generic.
  66. <P>
  67. <H3>Algorithm/Data-Structure Interoperability</H3>
  68. <P>
  69. First, each algorithm is written in a data-structure neutral way,
  70. allowing a single template function to operate on many different
  71. classes of containers. The concept of an iterator is the key
  72. ingredient in this decoupling of algorithms and data-structures. The
  73. impact of this technique is a reduction in the STL's code size from
  74. <i>O(M*N)</i> to <i>O(M+N)</i>, where <i>M</i> is the number of
  75. algorithms and <i>N</i> is the number of containers. Considering a
  76. situation of 20 algorithms and 5 data-structures, this would be the
  77. difference between writing 100 functions versus only 25 functions! And
  78. the differences continues to grow faster and faster as the number of
  79. algorithms and data-structures increase.
  80. <P>
  81. <H3>Extension through Function Objects</H3>
  82. <P>
  83. The second way that STL is generic is that its algorithms and containers
  84. are extensible. The user can adapt and customize the STL through the
  85. use of function objects. This flexibility is what makes STL such a
  86. great tool for solving real-world problems. Each programming problem
  87. brings its own set of entities and interactions that must be
  88. modeled. Function objects provide a mechanism for extending the STL to
  89. handle the specifics of each problem domain.
  90. <P>
  91. <H3>Element Type Parameterization</H3>
  92. <P>
  93. The third way that STL is generic is that its containers are
  94. parameterized on the element type. Though hugely important, this is
  95. perhaps the least &ldquo;interesting&rdquo; way in which STL is generic.
  96. Generic programming is often summarized by a brief description of
  97. parameterized lists such as <TT>std::list&lt;T&gt;</TT>. This hardly scratches
  98. the surface!
  99. <P>
  100. <H2>Genericity in the Boost Graph Library
  101. </H2>
  102. <P>
  103. Like the STL, there are three ways in which the BGL is generic.
  104. <P>
  105. <H3>Algorithm/Data-Structure Interoperability</H3>
  106. <P>
  107. First, the graph algorithms of the BGL are written to an interface that
  108. abstracts away the details of the particular graph data-structure.
  109. Like the STL, the BGL uses iterators to define the interface for
  110. data-structure traversal. There are three distinct graph traversal
  111. patterns: traversal of all vertices in the graph, through all of the
  112. edges, and along the adjacency structure of the graph (from a vertex
  113. to each of its neighbors). There are separate iterators for each
  114. pattern of traversal.
  115. <P>
  116. This generic interface allows template functions such as <a
  117. href="./breadth_first_search.html"><TT>breadth_first_search()</TT></a>
  118. to work on a large variety of graph data-structures, from graphs
  119. implemented with pointer-linked nodes to graphs encoded in
  120. arrays. This flexibility is especially important in the domain of
  121. graphs. Graph data-structures are often custom-made for a particular
  122. application. Traditionally, if programmers want to reuse an
  123. algorithm implementation they must convert/copy their graph data into
  124. the graph library's prescribed graph structure. This is the case with
  125. libraries such as LEDA, GTL, Stanford GraphBase; it is especially true
  126. of graph algorithms written in Fortran. This severely limits the reuse
  127. of their graph algorithms.
  128. <P>
  129. In contrast, custom-made (or even legacy) graph structures can be used
  130. as-is with the generic graph algorithms of the BGL, using <I>external
  131. adaptation</I> (see Section <A HREF="./leda_conversion.html">How to
  132. Convert Existing Graphs to the BGL</A>). External adaptation wraps a new
  133. interface around a data-structure without copying and without placing
  134. the data inside adaptor objects. The BGL interface was carefully
  135. designed to make this adaptation easy. To demonstrate this, we have
  136. built interfacing code for using a variety of graph structures (LEDA
  137. graphs, Stanford GraphBase graphs, and even Fortran-style arrays) in
  138. BGL graph algorithms.
  139. <P>
  140. <H3>Extension through Visitors</H3>
  141. <P>
  142. Second, the graph algorithms of the BGL are extensible. The BGL introduces the
  143. notion of a <I>visitor</I>, which is just a function object with
  144. multiple methods. In graph algorithms, there are often several key
  145. &ldquo;event points&rdquo; at which it is useful to insert user-defined
  146. operations. The visitor object has a different method that is invoked
  147. at each event point. The particular event points and corresponding
  148. visitor methods depend on the particular algorithm. They often
  149. include methods like <TT>start_vertex()</TT>,
  150. <TT>discover_vertex()</TT>, <TT>examine_edge()</TT>,
  151. <tt>tree_edge()</tt>, and <TT>finish_vertex()</TT>.
  152. <P>
  153. <H3>Vertex and Edge Property Multi-Parameterization</H3>
  154. <P>
  155. The third way that the BGL is generic is analogous to the parameterization
  156. of the element-type in STL containers, though again the story is a bit
  157. more complicated for graphs. We need to associate values (called
  158. &ldquo;properties&rdquo;) with both the vertices and the edges of the graph.
  159. In addition, it will often be necessary to associate
  160. multiple properties with each vertex and edge; this is what we mean
  161. by multi-parameterization.
  162. The STL <tt>std::list&lt;T&gt;</tt> class has a parameter <tt>T</tt>
  163. for its element type. Similarly, BGL graph classes have template
  164. parameters for vertex and edge &ldquo;properties&rdquo;. A
  165. property specifies the parameterized type of the property and also assigns
  166. an identifying tag to the property. This tag is used to distinguish
  167. between the multiple properties which an edge or vertex may have. A
  168. property value that is attached to a
  169. particular vertex or edge can be obtained via a <I>property
  170. map</I>. There is a separate property map for each property.
  171. <P>
  172. Traditional graph libraries and graph structures fall down when it
  173. comes to the parameterization of graph properties. This is one of the
  174. primary reasons that graph data-structures must be custom-built for
  175. applications. The parameterization of properties in the BGL graph
  176. classes makes them well suited for re-use.
  177. <p>
  178. <H2>Algorithms</H2>
  179. <P>
  180. The BGL algorithms consist of a core set of algorithm <I>patterns</I>
  181. (implemented as generic algorithms) and a larger set of graph
  182. algorithms. The core algorithm patterns are
  183. <P>
  184. <UL>
  185. <LI>Breadth First Search
  186. </LI>
  187. <LI>Depth First Search
  188. </LI>
  189. <LI>Uniform Cost Search
  190. </LI>
  191. </UL>
  192. <P>
  193. By themselves, the algorithm patterns do not compute any meaningful
  194. quantities over graphs; they are merely building blocks for
  195. constructing graph algorithms. The graph algorithms in the BGL currently
  196. include
  197. <P>
  198. <UL>
  199. <LI>Dijkstra's Shortest Paths</LI>
  200. <LI>Bellman-Ford Shortest Paths</LI>
  201. <LI>Johnson's All-Pairs Shortest Paths</LI>
  202. <LI>Kruskal's Minimum Spanning Tree</LI>
  203. <LI>Prim's Minimum Spanning Tree</LI>
  204. <LI>Connected Components</LI>
  205. <LI>Strongly Connected Components</LI>
  206. <LI>Dynamic Connected Components (using Disjoint Sets)</LI>
  207. <LI>Topological Sort</LI>
  208. <LI>Transpose</LI>
  209. <LI>Reverse Cuthill Mckee Ordering</LI>
  210. <LI>Smallest Last Vertex Ordering</LI>
  211. <LI>Sequential Vertex Coloring</LI>
  212. </UL>
  213. <P>
  214. <H2>Data Structures</H2>
  215. <P>
  216. The BGL currently provides two graph classes and an edge list adaptor:
  217. <P>
  218. <UL>
  219. <LI><a href="adjacency_list.html"><TT>adjacency_list</TT></a></LI>
  220. <LI><a href="adjacency_matrix.html"><TT>adjacency_matrix</TT></a></LI>
  221. <LI><a href="edge_list.html"><TT>edge_list</TT></a></LI>
  222. </UL>
  223. <P>
  224. The <TT>adjacency_list</TT> class is the general purpose &ldquo;swiss army
  225. knife&rdquo; of graph classes. It is highly parameterized so that it can be
  226. optimized for different situations: the graph is directed or
  227. undirected, allow or disallow parallel edges, efficient access to just
  228. the out-edges or also to the in-edges, fast vertex insertion and
  229. removal at the cost of extra space overhead, etc.
  230. <P>
  231. The <tt>adjacency_matrix</tt> class stores edges in a <i>|V| x |V|</i>
  232. matrix (where <i>|V|</i> is the number of vertices). The elements of
  233. this matrix represent edges in the graph. Adjacency matrix
  234. representations are especially suitable for very dense graphs, i.e.,
  235. those where the number of edges approaches <i>|V|<sup>2</sup></i>.
  236. <P>
  237. The <TT>edge_list</TT> class is an adaptor that takes any kind of edge
  238. iterator and implements an <a href="./EdgeListGraph.html">Edge List Graph</a>.
  239. <br>
  240. <HR>
  241. <TABLE>
  242. <TR valign=top>
  243. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  244. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  245. Indiana University (<A
  246. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  247. <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>
  248. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  249. Indiana University (<A
  250. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  251. </TD></TR></TABLE>
  252. </BODY>
  253. </HTML>