C++CodePythonPython GUIWindows

What Is The Best Python Graph Tool? graph-tool vs NetworkX

00 blogbanner graph tool python

What is graph-tool?

graph-tool is a powerful Python script module for graph manipulation and statistical analysis (a.k.a. networks). In contrast to most other Python modules with similar functionality, the core data structures and algorithms are written in C++, with extensive use of template metaprogramming and a heavy reliance on the Boost Graph Library. This gives it performance comparable to a pure C/C++ library (in terms of memory usage and computation time).

If you are looking for a decent introduction to basic knowledge in graph and network visualization, check out my article for Embarcadero Technologies here.

What Is The Best Python Graph Tool graph tool vs NetworkX the graph tool logo
graph tool logo Source Reference 1

Why graph-tool?

The following are the reasons why graph-tool might be the best network and graph visualization tools out there:

1. Fast

Most of the time, you can expect the algorithms to run just as fast as if graph-tool were a pure C/C++ library.

The following table shows brief performance comparisons between graph-tool and two other popular graph libraries with Python bindings, igraph and NetworkX [2]:

Algorithmgraph-tool (16 threads)graph-tool (1 thread)igraphNetworkX
Single-source shortest path0.0023 s0.0022 s0.0092 s0.25 s
Global clustering0.011 s0.025 s0.027 s7.94 s
PageRank0.0052 s0.022 s0.072 s1.54 s
K-core0.0033 s0.0036 s0.0098 s0.72 s
Minimum spanning tree0.0073 s0.0072 s0.026 s0.64 s
Betweenness102 s (~1.7 mins)331 s (~5.5 mins)198 s (vertex)+ 439 s (edge)(~ 10.6 mins)10297 s (vertex)13913 s (edge)(~6.7 hours)

Note: The test was performed by calling the functions several times, and calculating the average time taken per function call. The hardware used for this test is Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz with 8 cores and 16 threads. The results for graph-tool with OpenMP enabled using 16 threads are shown in the second column, and the results using only one thread are shown in the third column.

When OpenMP is disabled, both graph-tool and igraph appear to deliver comparable overall performance. Even in this case, graph-tool appears to be faster systematically.

2. OpenMP (Open Multi-Processing) support

Provides excellent performance on multi-core architectures, without degrading it on single-core machines.

3. Extensive features

Including an extensive array of features, 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 topological 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.

4. Powerful visualization

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.

5. Well documented

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

How to install graph-tool on Windows?

We cannot directly install graph-tool on Windows. For now, graph-tool is not supported in Windows, even installation with conda on Windows would also fail. On other OS, you can successfully install graph-tool via conda, by adding the ostrokach-forge channel [4].

In this article, we will demonstrate two ways of installation: Installing graph-tool using Docker Desktop and using Anaconda on WSL (Windows Subsystem for Linux). 

How to install graph-tool on Windows using Docker Desktop?

The best solution and OS-agnostic way to install graph-tool is using Docker. In this section, we will demonstrate the step-by-step in installing graph-tool using Docker, to assist you; in case you have little or no familiarity with using Docker. The following are the step-by-step graph-tool installation using Docker Desktop, for Windows users:

If you already install Docker on your Windows correctly, run the following command on your cmd to pull the graph-tool image:

What Is The Best Python Graph Tool graph tool vs NetworkX a terminal window showing graph tool being installed into Docker desktop

Open Docker Desktop, go to the Images tab, and run the graph-tool image:

What Is The Best Python Graph Tool graph tool vs NetworkX a GUI showing graph tool being installed

Go to Optional settings, to give your container a representative name, for example, graph-tool-python:

What Is The Best Python Graph Tool graph tool vs NetworkX the settings choice for running a Docker container
What Is The Best Python Graph Tool graph tool vs NetworkX checking the container name for our Docker container

Open the container, by going to the Containers tab, and run the graph-tool-python container:

What Is The Best Python Graph Tool graph tool vs NetworkX confirming Docker is running properly

And graph-tool would be installed and run successfully.

How to install graph-tool on Windows using Anaconda on WSL?

This section assumes you are already familiar with Linux on Windows using WSL (Windows Subsystem for Linux)  [5], and you already installed Anaconda on your Linux system [6]. In this demo, I installed graph-tool on Ubuntu 18.04 on WSL on Windows 10.

Why not install it directly via package managers for GNU/Linux, you might ask? As instructed in Reference [7]? If you install graph-tool directly via package managers for GNU/Linux, you might encounter the following error, that might be caused by the package is missing in the your selected distribution:

What Is The Best Python Graph Tool graph tool vs NetworkX installing via WSL

You might need to change the distribution and try it one by one (available distribution: bookworm, bullseye, buster, sid, bionic, eoan, focal, hirsute, impish, jammy), that might be too time consuming. Read more about it in Reference [8]:

So, the more straightforward way is by installing it via Anaconda.

The easiest way to install graph-tool is to create a new environment and install it there:

What Is The Best Python Graph Tool graph tool vs NetworkX using Anaconda

You can also install graph-tool without creating a new environment, but the solver is likely to fail (or hang) if your environment already contains many packages, whose dependencies may conflict with the graph-tool dependencies. This is particularly likely if your environment contains packages from the official Anaconda distribution, rather than the conda-forge distribution.

Activate the newly-created graph-tool or gt environment:

After that, install additional packages as needed:

03 ubuntuinstallation03 8909617

How to start graph-tool interactive sessions? 

with IPython (for Docker users)

Run the following line on your Windows cmd, to activate the graph-tool using interactive Python (IPython) [7]:

If you successfully activate the IPython for graph-tool, your command prompt would look like the following screenshot:

What Is The Best Python Graph Tool graph tool vs NetworkX running an interactive session

with Jupyter Notebook (for WSL users)

First, activate your Anaconda environment that contains the graph-tool library:

Run Jupyter Notebook with this command on your Ubuntu on WSL cmd:

What Is The Best Python Graph Tool graph tool vs NetworkX using a Jupyter notebook
What Is The Best Python Graph Tool graph tool vs NetworkX interactive with Jupyter

How to create your first graphs?

The graph tool module includes a Graph class as well as several algorithms that work with it. For the best performance, the internals of this class, as well as most of its algorithms, are written in C++ using the Boost Graph Library.

Let’s create your first graph using graph-tool with the following scripts [9]:

A simple directed graph with two vertices and one edge would be created and saved as a two-nodes.png file by the script above.

What Is The Best Python Graph Tool graph tool vs NetworkX graph output 1

Are there more graph-tool advanced examples?

In this section, we will try a more advanced example, that starts by loading a dataset, and then performing stochastic block model inference and visualizing the result with graph-tool.

First, we will start by loading the graph-tool dataset collection [10]. 

Here is the excerpt of the graph-tool dataset descriptions:

What Is The Best Python Graph Tool graph tool vs NetworkX a graphic of datasets

For example, we will work with the “football” dataset. Run the following scripts on the graph-tool IPython:

What Is The Best Python Graph Tool graph tool vs NetworkX graph output the results of using printg
What Is The Best Python Graph Tool graph tool vs NetworkX graph output the football choices
What Is The Best Python Graph Tool graph tool vs NetworkX graph output

And let’s infer the best partition of the network using the following scripts [11]:

The script above will perform stochastic block model inference of a network of American college football teams. The colors correspond to inferred group membership of the nodes.

What Is The Best Python Graph Tool graph tool vs NetworkX a nice network graph

How to browse the graph-tool plotting results?

How to show all the graph-tool plotting results by exporting the container (for Docker users)

You may notice that opening the plot results in Docker is tricky. It won’t be easily accessible as in your usual Windows’ or even Linux File Explorer.

You can see the following screenshot showing that the usual Linux or cmd commands are not working in Docker’s terminal:

What Is The Best Python Graph Tool graph tool vs NetworkX the terminal before exporting

To tackle this problem, and get your graph-tool outputs, you may need to archive your container’s file system into a tar file first. Open your command prompt, and run the following command [12]:

For example:

The process might be time-consuming (and consuming large space as well), as it will export the entire contents of our selected container.

What Is The Best Python Graph Tool graph tool vs NetworkX using Anaconda

After the exporting contents are completed, open the contents.tar file, and go to the /home/user directory to get the file you need. For example, from our previous sections: two-nodes.pdf and football-sbm-fit.svg.

How do we browse all the graph-tool plotting results via File Explorer (for Ubuntu on WSL users)?

For long-time Windows users, you might also notice that browsing the plot results or accessing files in general using GUI in WSL is tricky too. It won’t be easily accessible as in your usual Windows’ or even native Linux File Explorer.

But, in the latest Windows 10 update, we can easily browse Linux files from File Explorer, like we usually browse Windows files.

For example on my laptop, Go to Linux -> home -> user, and you would find your graph-tool outputs:

What Is The Best Python Graph Tool graph tool vs NetworkX graph output the graph output in Windows explorer

Is there another easier alternative? Try NetworkX!

Whether graph-tool provides publication-quality outputs & collections of sophisticated algorithms, we can consider that graph-tools is not beginner friendly, due to the following reasons:

  • Not easy to install or configure. The installation steps are not conventional.
  • Not supported in Windows.
  • If you insist on using graph-tool on Windows, the easiest way to use graph-tool is via Docker (because it is OS agnostic), as we’ve already shown in the demo above, or using dual OS employing WSL.
  • By using graph-tool via Docker, it means you have limited options in using it (either with IPython or Jupyter Notebook). And using Jupyter Notebook would lead you to set up a notebook server using VM, which might increase the challenges for beginners.
  • This means you can’t use it with your familiar or favorite IDEs like PyScripter, Spyder, etc.  

In contrast with the challenges above, NetworkX can easily be installed via pip or conda on all OS, and can easily be used in any famous Python IDE.

We’ve reviewed NetworkX, how to install it, and all the requirements, even create Windows GUI for it in the following articles:

What Is The Best Python Graph Tool graph tool vs NetworkX NetworkX graph output
Solve a real AI problem Solve the traveling salesman problem with NetworkX and Python4Delphi

and you can watch the demo of NetworkX4D, along with other 5 famous Python libraries here:

In addition, if you’re looking for other powerful data visualization libraries, check out this article:


To get started with PyScripter, a free, feature-rich, and lightweight Python IDE, click here.

Download RAD Studio to make more powerful Python GUI Windows Apps in 5x less time.

Check out Python4Delphi, which simplifies the creation of Python GUIs for Windows using Delphi.

Consider DelphiVCL, which simplifies the creation of Windows GUIs with Python.


References & further readings

[1] Peixoto, T. P. (2022).

graph-tool official website. graph-tool.skewed.de

[2] Peixoto, T. P. (2022).

graph-tool performance comparison. graph-tool.skewed.de/performance

[3] Peixoto, T. P. (2022).

Welcome to graph-tool’s documentation! graph-tool 2.45 documentation. graph-tool.skewed.de/static/doc/index.html

[4] Uliana, R. (2017).

Installing graph-tool for Python 3 on Anaconda. Medium. ronie.medium.com/installing-graph-tool-for-python-3-on-anaconda-3f76d9004979

[5] Microsoft Learn. (2022).

Install Linux on Windows with WSL. learn.microsoft.com/en-us/windows/wsl/install

[6] DigitalOcean. (2018).

How To Install Anaconda on Ubuntu 18.04 [Quickstart]. digitalocean.com/community/tutorials/how-to-install-anaconda-on-ubuntu-18-04-quickstart

[7] Peixoto, T. P. (2022).

graph-tool installation instructions. graph-tool 2.45 GitLab repository. git.skewed.de/count0/graph-tool/-/wikis/installation-instructions

[8] Stack Overflow. (2020).

Error in installing graph-tool in Ubuntu 18.04. stackoverflow.com/questions/61485539/error-in-installing-graph-tool-in-ubuntu-18-04

[9] Peixoto, T. P. (2022).

Quick start using graph-tool. graph-tool 2.45 documentation. graph-tool.skewed.de/static/doc/quickstart.html

[10] Peixoto, T. P. (2022).

graph_tool.collection – Dataset collection. graph-tool 2.45 documentation. graph-tool.skewed.de/static/doc/collection.html

[11] Peixoto, T. P. (2022).

Inferring modular network structure. graph-tool 2.45 documentation.  graph-tool.skewed.de/static/doc/demos/inference/ inference.html

[12] Stack Overflow. (2014).

Exploring Docker container’s file system. stackoverflow.com/questions/20813486/exploring-docker-containers-file-system

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *