The Boost Graphic Library (BGL) gives experienced C++ developers high quality implementations of a wide range of graph data structures and algorithms -- helping them save time that would otherwise have been spent on developing and debugging. Now, the BGL's creators offer a complete tutorial and reference designed to help developers get results with the BGL quickly. They also offer practical, hard-to-find guidance on generic programming that can help developers build their own software development libraries. For practicing programmers, the book introduces high quality implementations of graph data structures and algorithms that deliver outstanding efficiency and performance, and presents the BGL's flexible interface, which enables programmers to apply graph algorithms in settings where a graph may exist only implicitly. For all intermediate-to-advanced C++ programmers.
Click below for Sample Chapter related to this title:
Download the sample pages (includes Chapter 3 and Index)
I User Guide.
Some Graph Terminology.
Vertex and Edge Descriptors.
Graph Construction and Modification
Graph Classes and Adaptors.
Generic Graph Algorithms.
The Topological Sort Generic Algorithm.
The Depth-First Search Generic Algorithm.
Polymorphism in Object-Oriented Programming.
Polymorphism in Generic Programming.
Comparison of GP and OOP.
Generic Programming and the STL.
Concepts and Models.
Sets of Requirements.
Associated Types and Traits Classes.
Associated Types Needed in Function Template.
Typedefs Nested in Classes.
Definition of a Traits Class.
The Boost Namespace.
Named Function Parameters.
Topological Sort via DFS.
Marking Vertices Using External Properties.
Accessing Adjacent Vertices.
Traversing All the Vertices.
Toward a Generic DFS: Visitors.
Graph Setup: Internal Properties.
A Generic Topological Sort and DFS.
Parallel Compilation Time.
Six Degrees of Kevin Bacon.
Finding Loops in Program-Control-Flow Graphs.
Bellman-Ford and Distance Vector Routing.
Dijkstra and Link-State Routing.
Telephone Network Planning.
Connected Components and Internet Connectivity.
Strongly Connected Components and Web Page Links.
Knight's Jumps as a Graph.
Backtracking Graph Search.
Using BGL Topological Sort with a LEDA Graph.
Using BGL Topological Sort with a SGB Graph.
Implementing Graph Adaptors.
Graph Class Comparisons.
The Results and Discussion.
II Reference Manual.
Graph Traversal Concepts.
Graph Modification Concepts.
Static Connected Components.
Incremental Connected Components.
Property Map Concepts.
Property Map Classes.
Creating Your Own Property Maps.
Property Maps for Stanford GraphBase.
A Property Map Implemented with std::map.
The graph abstraction is a powerful problem-solving tool used to describe relationships between discrete objects. Many practical problems can be modeled in their essential form by graphs. Such problems appear in many domains: Internet packet routing, telephone network design, software build systems, Web search engines, molecular biology, automated road-trip planning, scientific computing, and so on. The power of the graph abstraction arises from the fact that the solution to a graph-theoretic problem can be used to solve problems in a wide variety of domains. For example, the problem of solving a maze and the problem of finding groups of Web pages that are mutually reachable can both be solved using depth-first search, an important concept from graph theory. By concentrating on the essence of these problems—the graph model describing discrete objects and the relationships between them—graph theoreticians have created solutions to not just a handful of particular problems, but to entire families of problems.
Now a question arises. If graph theory is generally and broadly applicable to arbitrary problem domains, should not the software that implements graph algorithms be just as broadly applicable? Graph theory would seem to be an ideal area for software reuse. However, up until now the potential for reuse has been far from realized. Graph problems do not typically occur in a pure graph-theoretic form, but rather are embedded in larger domain-specific problems. As a result, the data to be modeled as a graph are often not explicitly represented as a graph but are instead encoded in some application-specific data structure. Even in the case where the application data are explicitly represented as a graph, the particular graph representation chosen by the programmer might not match the representation expected by a library that the programmer wants to use. Moreover, different applications may place different time and space requirements on the graph data structure.
This implies a serious problem for the graph library writer who wants to provide reusable software, for it is impossible to anticipate every possible data structure that might be needed and to write a different version of the graph algorithm specifically for each one. The current state of affairs is that graph algorithms are written in terms of whatever data structure is most convenient for the algorithm and users must convert their data structures to that format in order to use the algorithm. This is an inefficient undertaking, consuming programmer time and computational resources. Often, the cost is perceived not to be worthwhile, and the programmer instead chooses to rewrite the algorithm in terms of his or her own data structure. This approach is also time consuming and error prone, and will tend to lead to sub-optimal solutions since the application programmer may not be a graph algorithms expert.
Generic programming lends itself well to solving the reusability problem for graph libraries. With generic programming, graph algorithms can be made much more flexible, allowing them to be easily used in a wide variety applications. Each graph algorithm is written not in terms of a specific data structure, but instead to a graph abstraction that can be easily implemented by many different data structures. Writing generic graph algorithms has the additional advantage of being more natural; the abstraction inherent in the pseudo-code description of an algorithm is retained in the generic function. The Boost Graph Library (BGL) is the first C++ graph library to apply the notions of generic programming to the construction of graph algorithms.
Soon after the Standard Template Library was released, work began at the LSC to apply generic programming to scientific computing. The Matrix Template Library (MTL) was one of the first projects. Many of the lessons learned during construction of the MTL were applied to the design and implementation of the GGCL. The LSC has since evolved into the Open Systems Laboratory (OSL) http://www.osl.iu.edu.Although the name and location have changed, the research agenda remains the same.
An important class of linear algebra computations in scientific computing is that of sparse matrix computations, an area where graph algorithms play an important role. As the LSC was developing the sparse matrix capabilities of the MTL, the need for high-performance reusable (and generic) graph algorithms became apparent. However, none of the graph libraries available at the time (LEDA, GTL, Stanford GraphBase) were written using the generic programming style of the MTL and the STL, and hence did not fulfill the flexibility and high-performance requirements of the LSC. Other researchers were also expressing interest in a generic C++ graph library. During a meeting with Bjarne Stroustrup, we were introduced to several individuals at AT&T who needed such a library. Other early work in the area of generic graph algorithms included some codes written by Alexander Stepanov, as well as Dietmar Kühl’s master’s thesis.
With this in mind, and motivated by homework assignments in his algorithms class ,Jeremy Siek began prototyping an interface and some graph classes in the spring of1998. Lie-Quan Lee then developed the first version of the GGCL, which became his master’s thesis project. During the following year, the authors began collaborating with Alexander Stepanov and Matthew Austern. During this time, Stepanov’s disjoint-sets-based connected components implementation was added to the GGCL, and work began on providing concept documentation for the GGCL, similar to Austern’s STL documentation.
During this year the authors also became aware of Boost and were excited to find an organization interested in creating high-quality, open source C++ libraries. Boost included several people interested in generic graph algorithms, most notably Dietmar Kühl. Some discussions about generic interfaces for graph structures resulted in a revision of the GGCL that closely resembles the current Boost Graph Library interface. On September 4, 2000, the GGCL passed the Boost formal review (managed by David Abrahams) and became the Boost Graph Library. The first release of the BGL was September 27, 2000. The BGL is not a “frozen” library. It continues to grow as new algorithms are contributed, and it continues to evolve to meet users’ needs. We encourage readers to participate in the Boost group and help with extensions to the BGL.
The zip archive of the Boost library collection can be unzipped by using WinZip or other similar tools. The UNIX “tar ball” can be expanded using the following command:
gunzip _cd boost all.tar.gz j tar xvf _
Extracting the archive creates a directory whose name consists of the word boost and a version number. For example, extracting the Boost release 1.25.1 creates a directory boost 1 25 1. Under this top directory, are two principal subdirectories: boost and libs. The subdirectory boost contains the header files for all the libraries in the collection. The subdirectory libs contains a separate subdirectory for each library in the collection. These subdirectories contain library-specific source and documentation files. You can point your Web browser to boost 1 251/index.htm and navigate the whole Boost library collection.
All of the BGL header files are in the directory boost/graph/. However, other Boost header files are needed since BGL uses other Boost components. The HTML documentation is in libs/graph/doc/ and the source code for the examples is inlibs/graph/example/. Regression tests for BGL are in libs/graph/test/. The source files in libs/graph/src/ implement the Graphviz file parsers and printers.
Except as described next, there are no compilation and build steps necessary to use BGL. All that is required is that the Boost header file directory be added to your compiler’s include path. For example, using Windows 2000, if you have unzipped release 1.25.1 from boost all.zip into the top level directory of your C drive, for Borland, GCC, and Metrowerks compilers add -Ic:/boost 1 25 1 to the compiler command line, and for the Microsoft Visual C++ compiler add /I "c:/boost 1 25 1". For IDEs, add c:/boost 1 25 1 (or whatever you have renamed it to) to the include search paths using the appropriate dialog. Before using the BGL interface to LEDA or Stanford GraphBase, LEDA or GraphBase must be installed according to their installation instructions. To use the read graphviz() functions (for reading AT&T Graphviz files), you must build and link to an additional library under boost 1 251/libs/graph/src.
The Boost Graph Library is written in ISO/IEC Standard C++ and compiles with most C++ compilers. For an up-to-date summary of the compatibility with a particular compiler, see the “Compiler Status” page at the Boost Web site http://www.boost.org/status/- compiler status.html.
The third partner to the user guide and reference manual is the BGL code itself. The BGL code is not simply academic and instructional. It is intended to be used.
For students learning about graph algorithms and data structures, BGL provides a comprehensive graph algorithm framework. The student can concentrate on learning the important theory behind graph algorithms without becoming bogged down and distracted in too many implementation details. For practicing programmers, BGL provides high-quality implementations of graph data structures and algorithms. Programmers will realize significant time saving from this reliability. Time that would have otherwise been spent developing (and debugging) complicated graph data structures and algorithms can now be spent in more productive pursuits. Moreover, the flexible interface to the BGL will allow programmers to apply graph algorithms in settings where a graph may only exist implicitly.
For the graph theoretician, this book makes a persuasive case for the use of generic programming for implementing graph-theoretic algorithms. Algorithms written using the BGL interface will have broad applicability and will be able to be reused innumerous settings.
We assume that the reader has a good grasp of C++. Since there are many sources where the reader can learn about C++, we do not try to teach it here (see the references at the end of the book—The C++ Programming Language, Special ed., by Stroustrup and C++ Primer, 3rd ed., by Josee Lajoie and Stanley B. Lippman are our recommendations). We also assume some familiarity with the STL (see STL Tutorial and Reference Guide by David R. Musser, Gillmer J. Derge, and Atul Sainiand Generic Programming and the STL by Matthew Austern ). We do, however, present some of the more advanced C++ features used to implement generic libraries in general and the BGL in particular. Some necessary graph theory concepts are introduced here, but not in great detail. For a detailed discussion of elementary graph theory see Introduction to Algorithms by T. H. Cormen, C. E. Leiserson, and R.L. Rivest .
Dave Abrahams, Jens Maurer, Dietmar Kühl, Beman Dawes, Gary Powell, Greg Colvin and the rest of the group at Boost provided valuable input to the BGL interface, numerous suggestions for improvement, and proofreads of this book. We also thank the following BGL users whose questions helped to motivate and improve BGL (as well as this book): Gordon Woodhull, Dave Longhorn, Joel Phillips, Edward Luke, and Stephen North.
Thanks to a number of individuals who reviewed the book during its development: Jan Christiaan van Winkel, David Musser, Beman Dawes, and Jeffrey Squyres. A great thanks to our editor Deborah Lafferty; Kim Arney Mulcahy, Cheryl Ferguson, and Marcy Barnes, the production coordinator; and the rest of the team at Addison-Wesley. It was a pleasure to work with them.
Our original work on the BGL was supported in part by NSF grant ACI-9982205. Parts of the BGL were completed while the third author was on sabbatical at Lawrence Berkeley National Laboratory (where the first two authors were occasional guests).All of the graph drawings in this book were produced using the dot program from the Graphviz package.
The BGL may be used freely for both commercial and noncommercial use. The main restriction on BGL is that modified source code can only be redistributed if it is clearly marked as a nonstandard version of BGL. The preferred method for the distribution of BGL, and for submitting changes, is through the Boost Web site.
, (comma), 40
. (period), 40
; (semicolon), 73
Aabstract data types (ADTs), 19
accumulate function, 26-27
basic description of, 13-14
add_edge function, 9, 17, 43, 84, 121, 152-153, 226
EdgeMutablePropertyGraph concept and,157
performance guidelines and, 128
undirected graphs and, 141
AdditiveAbelianGroup class, 20-21
add_vertex function, 9, 43, 120, 152, 157, 128, 225
AdjacencyGraph concept, 46-47, 114, 115, 146-149
adjacency_graph_tag function, 124
adjacency_iterator function, 47, 146
adjacency_list class, 11-12, 13
Bacon numbers and, 63, 65
basic description of, 43
boost namespace and, 37-39
compilation order and, 37, 46
implicit graphs and, 114
interfacing with other graph libraries and,119
internal properties and, 52-53
maximum flow and, 107
minimum-spanning-tree problem and, 92
performance guidelines and, 127, 128, 130, 132
shortest-path problems and, 84
template parameters, 52
using topological sort with, 17-18
adjacency_list.hpp, 17, 216, 246
adjacency_matrix class, 11-12, 43
associated types, 238-239
basic description of, 235-242
member functions, 239-240
nonmember functions, 240-242
template parameters, 238
type requirements, 238
adjacency_vertices function, 46-47, 146
implicit graphs and, 114, 115
maximum flow and, 109
adjacent iterators, 7
ADTs (abstract data types), 19
advance_dispatch function, 33
advance function, 33
Algorithms. See also Algorithms (listed by name)
basic description of, 13-18, 61-74
Koenig lookup and, 39
Algorithms (listed by name) See also Algorithms
bellman_ford_shortest_paths algorithm, 40, 76-82, 162, 182-186
breadth_first_search algorithm, 11, 39, 61-67, 158-159, 165-169
depth_first_search algorithm, 13, 18, 44-46, 57, 67-75, 98, 160-161, 170-175
Dijkstra’s shortest-path algorithm, 76, 81-88, 161, 179-181, 277
Edmunds-Karp algorithm, 105, 109
Ford-Fulkerson algorithm, 105
Kruskal’s algorithm, 90-93, 95, 189-192
Prim’s algorithm, 89, 90, 94-96
push-relabel algorithm, 105
ANSI C, 262
archetype class, 36
array_traits class, 31-33
array traits, for pointer types, 32-33
Array type, 30
Assignable concept, 37, 28-29, 143
Associated types, 28-34, 143, 205
adjacency_list class, 216-217
adjacency_matrix class, 238-239
edge_list class, 251-252
filtered _raph class, 259-260
graph_property_iter_range class, 298
iterator_property_map class, 284
LEDA Graph class, 268-269
property_map class, 249
reverse_graph class, 253-255
associative_property_map adaptor, 103
Austern, Matthew, 28
Bback_edge function, 50, 67, 160
back_edge_recorder class, 70
back_edges vector, 71
backward edge, 106
Bacon, Kevin, 62-67
bacon_number array, 66
basic description of, 61-67
graph setup and, 63-65
input files and, 63-65
classes, 20, 21
basic block, 69
BCCL (Boost Concept Checking Library), 35, 36
bellman_ford_shortest_paths algorithm, 40, 162
basic description of, 76-82, 182-186
named parameters, 184
time complexity and, 185
BellmanFordVisitor concept, 161-162
BFS (breadth-first search), 11, 39. See also breadth_first_search algorithm
Bacon numbers and, 65-67
basic description of, 61-67
visitor concepts and, 158-159
bfs_name_printer class, 11
BFSVisitor interface, 66, 158-159
bgl_named_params class, 40
BidirectionalGraph concept, 69, 72, 124, 145-146
bidirectional_graph_tag class, 124
Binary method problem, 23-24
Boost Concept Checking Library (BCCL), 35, 36
adjacency_list class and, 37-38
basic description of, 37-39
boost:: prefix, 37-38
Boost Property Map Library, 55-56, 79, 80
Boost Tokenizer Library, 63
breadth-first search (BFS), 11, 39. See also breadth_first_search algorithm
Bacon numbers and, 65-67
basic description of, 61-67
visitor concepts and, 158-159
breadth_first_search algorithm, 11, 39. See also breadth-first search (BFS)
Bacon numbers and, 65-67
basic description of, 61-67, 165-169
named parameters, 167
visitor concepts and, 158-159
Breadth-first tree, 61
CC++ (high-level language)
associated types and, 28
binary method problem and, 23-24
code bloat and, 23
concept checking and, 34-37
generic programming in, 19-59
GNU, 127, 128
Koenig lookup and, 38-39
Monoid concept and, 291
named parameters and, 39-40
object-oriented programming and, 22-25
Standard, 22, 28, 55, 125
valid expressions and, 28
capacity_map parameter, 206
cap object, 108
Chessboard, Knight’s tour problem for, 113-118
Class(es). See also Classes (listed by name)
auxiliary, 242-251, 289-298
basic description of, 13-14, 213-275
base, 20, 21
typedefs nested in, 30-31
Classes (listed by name). See also adjacency_list class
AdditiveAbelianGroup class, 20-21
adjacency_matrix class, 11-12, 43, 234-242
adjacency_vertices class, 109, 114, 115