Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

C++ Boost

Using SGB Graphs in BGL

The Boost Graph Library (BGL) header, <boost/graph/stanford_graph.hpp>, adapts a Stanford GraphBase (SGB) Graph pointer into a BGL-compatible VertexListGraph.  Note that a graph adaptor class is not used; SGB's Graph* itself becomes a model of VertexListGraph.  The VertexListGraph concept is fulfilled by defining the appropriate non-member functions for Graph*.

The Stanford GraphBase

"The Stanford GraphBase (SGB) is a collection of datasets and computer programs that generate and examine a wide variety of graphs and networks."  The SGB was developed and published by Donald E. Knuth in 1993.  The fully documented source code is available for anonymous ftp from Stanford University and in the book "The Stanford GraphBase, A Platform for Combinatorial Computing," published jointly by ACM Press and Addison-Wesley Publishing Company in 1993.  (This book contains several chapters with additional information not available in the electronic distribution.)


The source code of SGB is written in accordance with the rules of the Literate Programming paradigm, so you need to make sure that your computer supports the CWEB system.  The CWEB sources are available for anonymous ftp from Stanford University.  Bootstrapping CWEB on Unix systems is elementary and documented in the CWEB distribution; pre-compiled binary executables of the CWEB tools for Win32 systems are available from

Installing the SGB

After you have acquired the SGB sources and have installed a working CWEB system (at least the "ctangle" processor is required), you're almost set for compiling the SGB sources.  SGB is written in "old-style C," but the Boost Graph Library expects to handle "modern C" and C++.  Fortunately, the SGB distribution comes with an appropriate set of patches that convert all the sources from "KR-C" to "ANSI-C," thus allowing for smooth integration of the Stanford GraphBase in the Boost Graph Library.

Using the SGB

After you have run the installation process of the SGB, you can use the BGL graph interface with the SGB Graph*, <boost/graph/stanford_graph.hpp>, which will be described next.  All you have to do is tell the C++ compiler where to look for the SGB headerfiles (by default, /usr/local/sgb/include on Unix and the "MSVC" subdirectory of the SGB installation on Win32) and the linker where to find the SGB static library file (libgb.a on Unix and libgb.lib on Win32); consult the documentation of your particular compiler about how to do that.


The BGL Interface for the SGB

Where Defined


The main purpose of this Boost Graph Library (BGL) headerfile is to #include all global definitions for the general stuff of the Stanford GraphBase (SGB) and its various graph generator functions by reading all SGB headerfiles as in section 2 of the "test_sample" program.

On top of the SGB stuff, the BGL stanford_graph.hpp header adds and defines appropriate types and functions for using the SGB graphs in the BGL framework.  Apart from the improved interface, the SGB (static) library is used "as is" in the context of BGL.

Model Of

Vertex List Graph and Property Graph. The set of property tags that can be used with the SGB graph is described in the Vertex and Edge Properties section below.


The example program <example/miles_span.cpp> represents the first application of the generic framework of BGL to an SGB graph.  It uses Prim's algorithm to solve the "minimum spanning tree" problem.  In addition, the programs <example/girth.cpp> and <example/roget_components.cpp> have been ported from the SGB.  We intend to implement more algorithms from SGB in a generic fashion and to provide the remaining example programs of SGB for the BGL framework.  If you would like to help, feel free to submit your contributions!

Associated Types


The type for the vertex descriptors associated with the Graph*. We use the type Vertex* as the vertex descriptor.

The type for the edge descriptors associated with the Graph*. This is the type boost::sgb_edge. In addition to supporting all the required operations of a BGL edge descriptor, the boost::sgb_edge class has the following constructor.
   sgb_edge::sgb_edge(Arc* arc, Vertex* source)


The type for the iterators returned by vertices().

The type for the iterators returned by out_edges().

The type for the iterators returned by adjacent_vertices().

The type used for dealing with the number of vertices in the graph.

The type used for dealing with the number of edges in the graph.

The type used for dealing with the number of edges incident to a vertex in the graph.

Provides information about whether the graph is directed or undirected. An SGB Graph* is directed so this type is directed_tag.

An SGB Graph* provides traversal of the vertex set, out edges, and adjacent vertices. Therefore the traversal category tag is defined as follows:
struct sgb_traversal_tag :
  public virtual vertex_list_graph_tag,
  public virtual incidence_graph_tag,
  public virtual adjacency_graph_tag { };


This describes whether the graph class allows the insertion of parallel edges (edges with the same source and target).  The SGB Graph* does not prevent addition of parallel edges, so this type is allow_parallel_edge_tag.

Non-Member Functions

std::pair<vertex_iterator, vertex_iterator>
vertices(Graph* g)
Returns an iterator-range providing access to the vertex set of graph g.
std::pair<out_edge_iterator, out_edge_iterator>
out_edges(vertex_descriptor v, Graph* g)
Returns an iterator-range providing access to the out-edges of vertex v in graph g.
There is no corresponding in_edges function.
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(vertex_descriptor v, Graph* g)
Returns an iterator-range providing access to the adjacent vertices of vertex v in graph g.
source(edge_descriptor e, Graph* g)
Returns the source vertex of edge e.
target(edge_descriptor e, Graph* g)
Returns the target vertex of edge e.
out_degree(vertex_descriptor v, Graph* g)
Returns the number of edges leaving vertex v.
There is no corresponding in_degree function.
num_vertices(Graph* g)
Returns the number of vertices in the graph g.
num_edges(Graph* g)
Returns the number of edges in the graph g.
vertex(vertices_size_type n, Graph* g)
Returns the (0-based) nth vertex in the graph's vertex list.
template <class PropertyTag>
property_map<Graph*, PropertyTag>::type
get(PropertyTag, Graph*& g)

template <class PropertyTag>
property_map<Graph*, Tag>::const_type
get(PropertyTag, const Graph*& g)
Returns the property map object for the vertex property specified by PropertyTag. The PropertyTag must be one of the described below.

Vertex and Edge Properties

The SGB Vertex and Arc structures provide "utility" fields for storing extra information. We provide BGL wrappers that provide access to these fields through property maps. In addition, vertex index and edge length maps are provided. A property map object can be obtained from a SGB Graph* using the get() function described in the Property Graph concept.

The following list of property tags can be used to specify which utility field you would like a property map for.

// vertex properties
template <class T> u_property;
template <class T> v_property;
template <class T> w_property;
template <class T> x_property;
template <class T> y_property;
template <class T> z_property;

// edge properties
template <class T> a_property;
template <class T> b_property;

The template parameter T for these tags is limited to the types in the util union declared in the SGB header gb_graph.h, which are Vertex*, Arc*, Graph*, char*, and long. The property maps for the utility fields are models of Lvalue Property Map.

The property map for vertex indices can be obtained using the vertex_index_t tag, and this property map is a Readable Property Map. A property map for edge length's can be obtained using the edge_length_t tag, and this property map is a Lvalue Property Map whose value type is long.

Property map objects can be obtained via the get() function of the Property Graph concept. The type of the property map is given by the property_map traits class.

Copyright © 2001 Andreas Scherer, Aachen (
Jeremy Siek, Indiana University (
Lie-Quan Lee, Indiana University (
Andrew Lumsdaine, Indiana University (