Saturday, January 12, 2013

graph-tool. Python module for graph (networks) manipulation and display

graph-tool is a Python module for the creation, manipulation, analysis and display of graphs (the computer science data structure, not computer graphics, though it can display graphs as graphics).

I took a look at some of the code examples of graph-tool. It is fairly easy to understand and use for simple cases, e.g. there are simple library calls to create a new graph, add vertices to it, add edges between vertices, etc. Quick excerpt:

from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1, v2)
And to draw the above graph:
graph_draw(g, vertex_text=g.vertex_index, vertex_font_size=18,
output_size=(200, 200), output="two-nodes.pdf")
The above code creates this image:

It supports both directed and undirected graphs.

Though exposed as a Python module, graph-tool is internally written in C++ using the Boost Graph Library. It also makes extensive use of template metaprogramming for performance.

The graphics drawing is based on the Cairo Graphics library, which I blogged about recently.

The creator of graph-tool is Tiago de Paula Peixoto, currently a postdoc at the University of Bremen, Germany.

Vasudev Ram

No comments: