Skip to main content

What is graph-tool?

Graph-tool is an efficient Python module for manipulation and statistical analysis of graphs (a.k.a. networks). Contrary to most other Python modules with similar functionality, the core data structures and algorithms are implemented in C++, making extensive use of template metaprogramming, based heavily on the Boost Graph Library. This confers it a level of performance that is comparable (both in memory usage and computation time) to that of a pure C/C++ library.


Graph-tool is mature software that has been on the cutting edge of network science for more than 20 years. It is fully documented, and is released as Free Software under the LGPLv3 license.

It is fast!

The core algorithms and data structures are written in C++, so they often run orders of magnitude faster than Python-only alternatives, making graph-tool especially suited for large networks. See a performance comparison.

Parallel

Many algorithms, as well as graph construction and random graph sampling, run in parallel with OpenMP on multi-core machines. The C++ code also releases the GIL, so Python threads can run it concurrently.

Fully documented

All over 600 functions are documented, with examples, in the online documentation and its cookbooks.

Powerful visualization

Static and interactive drawing with its own layout algorithms. See the drawing tutorial.

Tip

After installation, you should start with the quick start guide!

You should then take a look at the various cookbooks, and explore all the examples in the various submodules.

For commonly asked questions, read the FAQ.

Unique features

These are some of the functionalities of graph-tool which are currently not available in most other comparable packages:

  1. Comprehensive framework for inferential community detection, built upon statistically principled approaches that avoid overfitting and are interpretable. (See here as well as  [1] for why you should avoid off-the-shelf methods available in other software packages.)

  2. Support for network reconstruction from dynamics.

  3. Support for uncertainty quantification in network data.

  4. Support for OpenMP shared memory parallelism throughout the library: not only in many algorithms, but also when building, copying and filtering graphs, and when sampling random graphs from stochastic block models.

  5. High-quality network visualization, both static and interactive, with 29 layout algorithms, and supporting animations and matplotlib integration.

  6. Filtered graphs, i.e. graphs where nodes and edges are temporarily masked. These are first class citizens in the library, and are accepted by every function. Due to the use of C++ template metaprogramming, this functionality comes at no performance cost when filtering is not being used.

  7. Efficient and fully documented binary format for network files.

  8. Integration with the Netzschleuder network data repository, enabling easy loading of network data.

  9. Support for writing custom C++ extensions.

Quick installation:

conda create --name gt -c conda-forge graph-tool
conda activate gt
mkdir my-project
cd my-project
pixi init
pixi add graph-tool
port install py-graph-tool
brew install graph-tool
pacman -S python-graph-tool
apt-get install python3-graph-tool

(see the instructions for installing the bleeding edge version from the upstream repository.)

emerge graph-tool
dnf install python3-graph-tool
docker pull tiagopeixoto/graph-tool
Note

See here for more detailed installation instructions.

Tip

For a quick test run without installing on your machine, you can run graph-tool under colab.

Quick demos:

import graph_tool.all as gt
import matplotlib.cm

gt.seed_rng(47)

g = gt.collection.ns["foodweb_ulanowicz/baywet"]

sargs = dict(rec=[g.ep.weight],
             rec_types=["real-exponential"])
state = \
    gt.minimize_nested_blockmodel_dl(g,
                                     base_state=gt.WeightedBlockState,
                                     base_state_args=sargs)

state.draw(edge_color=gt.prop_to_size(g.ep.weight,
                                      power=1,
                                      log=True),
           ecmap=(matplotlib.cm.inferno, .6),
           eorder=g.ep.weight,
           edge_pen_width=gt.prop_to_size(g.ep.weight,
                                          1, 4,
                                          power=1,
                                          log=True),
           edge_gradient=[]);

import graph_tool.all as gt
import matplotlib.cm

g = gt.collection.data["polblogs"]

g = gt.extract_largest_component(g)
pr = gt.pagerank(g)

gt.graph_draw(g, pos=g.vp["pos"],
              vertex_fill_color=pr,
              vertex_size=gt.prop_to_size(pr,
                                          mi=5,
                                          ma=15),
              vorder=pr,
              vcmap=matplotlib.cm.gist_heat,
              bg_color=[1,1,1,1]);

import graph_tool.all as gt

g = gt.collection.data["polblogs"]
g = gt.GraphView(g, directed=False)
gt.remove_parallel_edges(g)
g = gt.extract_largest_component(g)

s = g.vp.value.t(lambda x: 2 * x - 1)

state = gt.IsingGlauberState(g, s=s, beta=.1)

state.iterate_async(niter=1000 * g.num_vertices())

gt.graph_draw(g, g.vp.pos,
              vertex_fill_color=state.s,
              bg_color=[1,1,1,1]);

import graph_tool.all as gt
from numpy.random import random
from scipy.linalg import norm

points = random((500, 2)) * 4

g, pos = gt.triangulation(points, type="delaunay")
weight = g.new_edge_property("double")

for e in g.edges():
    weight[e] = norm(pos[e.source()].a -
                     pos[e.target()].a)

b = gt.betweenness(g, weight=weight)

gt.graph_draw(g, pos=pos, vertex_fill_color=b[0],
              edge_pen_width=b[1].t(lambda x: x*120),
              bg_color=[1,1,1,1]);

import graph_tool.all as gt

g = gt.collection.data["polblogs"]

g = gt.extract_largest_component(g, prune=True)

state = gt.minimize_blockmodel_dl(g)

u = gt.generate_sbm(state.b.a,
                    gt.adjacency(state.get_bg(),
                                 state.get_ers()).T,
                    g.degree_property_map("out").a,
                    g.degree_property_map("in").a,
                    directed=True)

gt.graph_draw(u, g.vp.pos, bg_color=[1,1,1,1]);

Features at a glance

NoteNew in version 4.0

Version 4.0 is a major release that adds hundreds of new functions: the new structure module (which replaces topology) with algorithms for cliques, coloring, matchings and covers, chordal graphs, d-separation, graph polynomials and isomorphism; dozens of new random graph models; new centralities, connectivity and flow algorithms; the map equation, hierarchical random graphs and semi-supervised vertex classification for inference; magnetic and directed Laplacians; 22 new layout algorithms; new file formats; and parallelism in many more places. See the changelog for details.

More than 600 documented functions and classes, covering:

References

[1]
T. P. Peixoto, Descriptive Vs. Inferential Community Detection in Networks: Pitfalls, Myths and Half-Truths, Elements in the Structure and Dynamics of Complex Networks (2023).
lglpv3
by Tiago P. Peixoto