C++ Boost

Boost Graph Library: Python Bindings (Experimental)

The Boost Graph Library offers a wealth of graph algorithms and data types for C++. These algorithms are flexible and efficient, but the mechanisms employed to achieve this goal can result in long compile times that slow the development cycle.

The Python bindings are build using the Boost.Python library. The bindings are meant to strike a balance of usability, flexibility, and efficiency, making it possible to rapidly develop useful systems using the BGL in Python.

Caveats and Warnings

The Python bindings for the Graph library are experimental at this time. The bindings may not work (but they probably do) and we will not attempt to provide backward compatibility if the interfaces change. If you have any ideas

Building the Python Bindings

To build the Python bindings, change to the libs/graph/build/python subdirectory of Boost and then follow the Boost Build Instructions to build the Python extension module. The end result will be a dynamic library (DLL, .so, etc.) that can be loaded into Python via "import bgl". Note: For this build to succeed, the graph library must work flawlessly on the compile, including the new GraphViz parser, which tends to break many popular compilers. For instance, recent versions of GCC are okay but Visual C++ 7.1 will crash. We're working to resolve the situation.

A First Example

Our first example illustrates the use of BGL graphs, graph algorithms, and property maps from Python. The initial step is to load the BGL-Python bindings:

from bgl import *
    

Next, we load a GraphViz file containing the description of a sample graph. The Graph type is an undirected graph; file_kind is an enumerator with only two potential values: adjlist, for an adjacency-list representation, or graphviz, for a GraphViz file.

g = Graph("biconnected_components.dot", file_kind.graphviz)
    

Assuming no exceptions have been thrown, we will have an undirected graph loaded. We can immediately call biconnected_components to compute the biconnected components within the graph:

art_points = biconnected_components(g, g.get_edge_int_map("label"))
    

There are several interesting parts of the preceeding line. We call the biconnected_components algorithm with two parameters. The first is just the graph itself. The second parameter is a property map from edges to an integer value, which will be stored in the graph under the name "label". Finally, the return value of biconnected_components is a Python list containing the articulation points of the graph.

Other kinds of property maps are available. In this case, we create two property maps attaching strings to the vertices of the graph. We then iterate over all vertices of the graph to set default values for each vertex property.

v_color = g.get_vertex_string_map("fillcolor") 
v_style = g.get_vertex_string_map("style") 
for v in g.vertices: 
  v_color[v] = "white" 
  v_style[v] = "filled"
    

Lastly, we mark all of the articulation points red and write out the result in GraphViz format.

for v in art_points:
  v_color[v] = "red"

g.write_graphviz("biconnected_components_out.dot")
    

Documentation style


Copyright © 2005 Doug Gregor, Indiana University ()
Andrew Lumsdaine, Indiana University ()