# 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 15 years. It is fully documented, and is released as Free Software under the LGPLv3 license.

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

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.

See the changelog.

**Quick installation:**

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

`brew install graph-tool`

`pikaur -S python-graph-tool`

`apt-get install python3-graph-tool`

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

`emerge graph-tool`

`dnf install python3-graph-tool`

`docker pull tiagopeixoto/graph-tool`

See here for more detailed installation instructions.

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

under colab.

### Unique features

These are some of the functionalities of `graph-tool`

which are currently not available in most other comparable packages:

Comprehensive framework for inferential community detection, build 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.)

Support for network reconstruction from dynamics.

Support for uncertainty quantification in network data.

Support for OpenMP shared memory parallelism for several algorithms.

High-quality network visualization, both static and interactive, supporting animations and matplotlib integration.

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 C++ template metaprogramming, this functionality comes at no performance cost when filtering is not being used.

Efficient and fully documented binary format for network files.

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

Support for writing custom C++ extensions.

**Quick demos:**

```
import graph_tool.all as gt
import matplotlib.cm
47)
gt.seed_rng(
= gt.collection.ns["foodweb_baywet"]
g
= dict(recs=[g.ep.weight],
sargs =["real-exponential"])
rec_types= \
state
gt.minimize_nested_blockmodel_dl(g,=sargs)
state_args
=gt.prop_to_size(g.ep.weight,
state.draw(edge_color=1,
power=True),
log=(matplotlib.cm.inferno, .6),
ecmap=g.ep.weight,
eorder=gt.prop_to_size(g.ep.weight,
edge_pen_width1, 4,
=1,
power=True),
log=[]); edge_gradient
```

```
= gt.collection.data["polblogs"]
g
= gt.extract_largest_component(g)
g = gt.pagerank(g)
pr
=g.vp["pos"],
gt.graph_draw(g, pos=pr,
vertex_fill_color=gt.prop_to_size(pr,
vertex_size=5,
mi=15),
ma=pr,
vorder=matplotlib.cm.gist_heat); vcmap
```

```
import graph_tool.all as gt
= gt.collection.data["polblogs"]
g = gt.GraphView(g, directed=False)
g
gt.remove_parallel_edges(g)= gt.extract_largest_component(g)
g
= g.vp.value.t(lambda x: 2 * x - 1)
s
= gt.IsingGlauberState(g, s=s, beta=.1)
state
=1000 * g.num_vertices())
state.iterate_async(niter
gt.graph_draw(g, g.vp.pos,=state.s); vertex_fill_color
```

```
import graph_tool.all as gt
from numpy.random import random
from scipy.linalg import norm
= random((500, 2)) * 4
points
= gt.triangulation(points, type="delaunay")
g, pos = g.new_edge_property("double")
weight
for e in g.edges():
= norm(pos[e.source()].a -
weight[e]
pos[e.target()].a)
= gt.betweenness(g, weight=weight)
b
=pos, vertex_fill_color=b[0],
gt.graph_draw(g, pos=b[1].t(lambda x: x*120)); edge_pen_width
```

```
import graph_tool.all as gt
= gt.collection.data["polblogs"]
g
= gt.extract_largest_component(g, prune=True)
g
= gt.minimize_blockmodel_dl(g)
state
= gt.generate_sbm(state.b.a,
u
gt.adjacency(state.get_bg(),
state.get_ers()).T,"out").a,
g.degree_property_map("in").a,
g.degree_property_map(=True)
directed
; gt.graph_draw(u, g.vp.pos)
```

### It is *fast!*

Despite its nice, soft outer appearance of a regular Python module, the core algorithms and data structures of graph-tool are written in C++, with performance in mind. Most of the time, you can expect the algorithms to run just as fast as if graph-tool were a pure C/C++ library. See a performance comparison.

### OpenMP support

Many algorithms are implemented in parallel using OpenMP, which provides excellent performance on multi-core architectures, without degrading it on single-core machines.

### Comprehensive functionality

An extensive array of features is included, such as support for arbitrary vertex, edge or graph properties, efficient “on the fly” filtering of vertices and edges, powerful graph I/O using the GraphML, GML and dot file formats, graph pickling, graph statistics (degree/property histogram, vertex correlations, average shortest distance, etc.), centrality measures, standard structural algorithms (isomorphism, minimum spanning tree, connected components, dominator tree, maximum flow, etc.), generation of random graphs with arbitrary degrees and correlations, detection of modules and communities via statistical inference, and much more.

### Powerful visualization

Conveniently draw your graphs, using a variety of algorithms and output formats (including to the screen). Graph-tool has its own layout algorithms and versatile, interactive drawing routines based on cairo and GTK+, but it can also work as a very comfortable interface to the excellent graphviz package.

### Fully documented

Every single function in the module is documented in the docstrings and in the online documentation, which is full of examples.

## References

*Descriptive Vs. Inferential Community Detection in Networks: Pitfalls, Myths and Half-Truths*, Elements in the Structure and Dynamics of Complex Networks (2023).