Graph-tool tutorial

The following tutorial demonstrates working with graphs using the graph-tool python module. In the process, you will learn how to:

  • create a graph 'by-hand'
  • perform basic network analysis
  • visualize graphs and their properties

In [1]:
import graph_tool.all as gt
import pandas as pd
import numpy as np
from IPython.display import display
%matplotlib inline
print("graph-tool version: {}".format(gt.__version__.split(' ')[0]))


graph-tool version: 2.29

Show datasets in collection


In [ ]:
with pd.option_context('display.max_colwidth', -1):
    display(pd.DataFrame.from_records(gt.collection.descriptions,
                                      index=['description']).transpose())

In [ ]:
g = gt.collection.data['karate']
g

In [ ]:
# construct a simple drawing of this graph

Another graph example


In [2]:
# If you run this for the first time, download the data with the command below
#!wget https://git.skewed.de/count0/graph-tool/raw/2c8c9899dd05549eaef728dabd93dc0759a2d4e0/doc/search_example.xml

In [8]:
gs = gt.load_graph("search_example.xml")

In [9]:
# TODO: print available edge and vertex properties
# TODO: visualize edge weight and names

Social graph drawing 101


In [123]:
X_knows = {
    'Mary': ['Peter', 'Albert', 'DavidF', 'Peter'],
    'Judy': ['Bob', 'Alan'],
    'Peter': ['Mary', 'DavidF', 'Jon'],
    'DavidF': ['Albert', 'Joseph', 'Peter', 'Mary'],
    'Jon': ['Peter', 'Joseph', 'DavidE'],
    'DavidE': ['Jon', 'Joseph', 'Albert'],
    'Joseph': ['DavidE', 'Jon', 'DavidF'],
    'Bob': ['Judy', 'Alan'],
    'Alan': ['Bob', 'Mary', 'Judy'],
    'Albert': ['DavidF', 'Mary', 'DavidE'],
}

In [124]:
g = gt.Graph(directed=True)

Create a graph using Python iterations

Below is a slow and tedious version of what can be done with a single call to add_edge_list(...) on a Graph.


In [105]:
# Create edge tuples and list of unique names
X_edges = list((n,k) for n in X_knows for k in X_knows[n])
from functools import reduce
X_names = reduce(lambda a,b: set(a).union(b),
                 (X_knows[n] for n in X_knows)
                ).union(X_knows.keys())
X_names = list(X_names)

In [106]:
# Construct a 'StringIndexer' to convert strings to integers
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
lem = le.fit(list(X_names))
X_edges = list(map(lem.transform, X_edges))

In [107]:
# Create Graph object and add a string property for names
g2 = gt.Graph()
v_name = g2.new_vertex_property('string')
g2.vertex_properties['name'] = v_name

In [109]:
for vn in lem.classes_:
    v = g2.add_vertex()
    v_name[v] = vn

In [110]:
for f,t in X_edges:
    g2.add_edge(f,t)

In [7]:
# TODO: Same as above, make a tidy, undirectional drawing of this graph

Fast graph construction


In [125]:
# TODO: find one-line call to g.add_edge_list that constructs the X_knows graph
# hint: use nested list comprehension to reshape the dictionary

In [126]:
# TODO: Create an undirected view of this graph

In [127]:
# Tidy up parallel edges

In [128]:
# Try two different layouts presented in the tutorial

In [6]:
# Produce a tidy drawing of the undirected graph

Graph analysis

Work through the graph filtering examples to draw a view of a relevant graph measure, such as betweenness.

Use one of the graphs constructed above.


In [ ]: