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