The graph-tool documentation#

graph-tool is an efficient Python module for manipulation and statistical analysis of graphs (a.k.a. networks).

The graph_tool module provides a Graph class and several algorithms that operate on it. The internals of this class, and of most algorithms, are written in C++ for performance, using template metaprogramming for code specialization, and the Boost Graph Library.

graph-tool can be orders of magnitude faster than Python-only alternatives, and therefore it is especially suited for large-scale network analysis.

In addition to its performance, graph-tool contains the following features, which are currently not available in most other comparable packages:

Feature

Description

Inferential community detection

A framework built upon statistically principled approaches that avoid overfitting and are interpretable (see here and [1] for the reasons to avoid off-the-shelf methods available in other software packages).

Network reconstruction

Inference of networks from the observed dynamics on their vertices.

Uncertainty quantification

Inference of networks from noisy or incomplete measurements of their edges.

Parallel algorithms

Support for OpenMP shared memory parallelism throughout the library: in many algorithms, and also when building, copying, and filtering graphs, and when sampling random graphs.

Network visualization

Static and interactive, supporting animations and matplotlib integration.

Filtered graphs

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.

Binary format

An efficient and fully documented format for network files.

Netzschleuder

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

C++ extensions

Support for writing custom extensions in C++.

Overview of the functionality#

The library contains several hundred documented functions and classes, organized in the submodules summarized in the following table, where each submodule page lists all of its functions.

Submodule

Functionality

graph_tool

The Graph class, directed and undirected, with vertex, edge, and graph property maps of any type, graph views and filtering, fast array-based iteration, input and output in the gt binary format and over 20 other file formats (see load_graph()), and more.

graph_tool.structure

Shortest paths and distances (also with contraction hierarchies for fast queries), connected components, k-core and truss decompositions, spanning trees and arborescences, topological sorting and dominator trees of directed acyclic graphs, cliques, matchings, independent sets and vertex covers, coloring, chordal graphs and treewidth, planarity, Eulerian paths and traveling salesman tours, isomorphism, subgraph isomorphism and edit distance, automorphism groups, Tutte and chromatic polynomials, and more.

graph_tool.centrality

PageRank, betweenness, closeness, eigenvector, Katz, HITS, current-flow, group, load, and distinctiveness centralities, communicability, structural holes, trophic levels, and more.

graph_tool.clustering

Local and global clustering coefficients (also for subsets of the vertices), extended clustering, motif counting and significance, triad and dyad census, bipartite measures such as the clustering of Latapy et al. and node redundancy, and more.

graph_tool.flow

Maximum flows, minimum cuts, minimum-cost flows, Gomory–Hu trees, vertex and edge connectivity, disjoint paths, k-components, and more.

graph_tool.stats

Degree and property histograms, distance histograms, and more.

graph_tool.correlations

Assortativity, degree correlations, rich-club coefficients, and more.

graph_tool.spectral

Adjacency, Laplacian, incidence, transition, non-backtracking, magnetic, and directed Laplacian matrices, as sparse matrices or matrix-free operators, spectral embedding and clustering, and more.

graph_tool.generation

Stochastic block models, configuration models, growing models such as preferential attachment, geometric graphs, random trees and bipartite graphs, graph transformations such as line graphs and products, set operations such as unions, named deterministic graphs, and more.

graph_tool.inference

Variants of the stochastic block model for community detection and network reconstruction, partition comparison and consensus, and more.

graph_tool.dynamics

Epidemic models (SI, SIS, SIR, and SIRS), Ising, Potts, voter, Kuramoto, and Lotka–Volterra models, belief propagation, and more.

graph_tool.draw

Static and interactive visualization with graph_draw(), dozens of layout algorithms such as SFDP and the hierarchical force-directed layout, output to PNG, PDF, SVG, and PostScript, animations, matplotlib integration, and more.

graph_tool.search

Breadth-first, depth-first, Dijkstra, Bellman–Ford, and A* searches, with visitors that can modify their behavior, and more.

graph_tool.collection

Example networks and the Netzschleuder repository.

graph_tool.util

Utility functions to find vertices and edges.

Installing graph-tool#

Detailed installation instructions for various platforms are available here.

The simplest way to install it is to use conda:

conda create --name gt -c conda-forge graph-tool
conda activate gt

On HPC systems, graph_tool can also be used with Apptainer/Singularity.

Getting started#

We recommend first reading the quick start guide, then the cookbook guides, and the examples in the submodules. Commonly asked questions are answered in the FAQ.

Asking questions and reporting bugs#

Questions about graph-tool can be asked in the discussion forum.

Problems can be reported as issues in the git repository.

Before asking, please check whether the question has been asked before, or whether a similar issue is open. When asking questions or reporting problems, it is important to include 1. the exact graph-tool version; 2. the operating system; and 3. a minimal working example that shows the problem.

The third item is essential, since with only the part of the code that is believed to cause the problem it is not possible to understand the context that may have contributed to it.

How to use the documentation#

Documentation is available in two forms: docstrings provided with the code, and the full documentation available on the graph-tool homepage.

We recommend exploring the docstrings using IPython, an advanced Python shell with TAB-completion and introspection capabilities.

The docstring examples assume that graph_tool.all has been imported as gt:

>>> import graph_tool.all as gt

Code snippets are indicated by three greater-than signs:

>>> x = x + 1

Use the built-in help function to view a function’s docstring:

>>> help(gt.Graph)

Contents#

References#

[1]

Tiago P. Peixoto. Descriptive vs. Inferential Community Detection in Networks: Pitfalls, Myths and Half-Truths. Elements in the Structure and Dynamics of Complex Networks, July 2023. arXiv:2112.00183, doi:10.1017/9781009118897, [sci-hub].