# Introduction to Graph Tool

I started reading about network statistics this morning and I wanted to experiment with the available Python tools. It looks like the state of the art is graph-tool. I had a little trouble installing the software on my Mac, but there is an evolving guide to this process here.

# Introductory Terms

I found a great introductory resource, the Introduction to Social Network Methods by Robert A. Hanneman and Mark Riddle. In this post I have reproduced the `KNOKI` data set they used throughout their book. The advantage of this data set is that it is reasonably small, complex, and widely used an example in other resources. Here are some definitions I compiled from Chapter 7, Connection and Distance:

Out-degree statistics
Calculated from the rows, tells us about sources of information.
In-degree statistics
Calculated from the columns, tells us about consumers of information.
Density
The number of connections relative to the number of possible connections.
Reachability
Binary, tells us if there is a path between two actors.
Connectivity
Integer, counts the number of nodes that have to be removed in order for one node to be disconnected from another.
Distance
Integer, count of the number of walks of a given number of hops, or minimum distance in a weighted graph.
Geodesic distance
The number of hops, or distance, of the shortest path between two nodes.
Diameter
The largest geodesic distance.
Geodesic count
There is one geodesic distance, but there may be many paths for that geodesic distance.
Maximum flow
The number of paths between a source neighborhood and a target.

# A Code Example

The graph-tool documentation is simply extraordinary. I borrowed heavily from it. I’m not sure that there isn’t an easier way to define a graph, but my method below isn’t painfully tedious. First we import all of graph-tool, then we define a two dimensional array representing the Knoke Information data set.

```from graph_tool.all import *

knoki = [ [ 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ]
, [ 1, 0, 1, 1, 1, 0, 1, 1, 1, 0 ]
, [ 0, 1, 0, 1, 1, 1, 1, 0, 0, 1 ]
, [ 1, 1, 0, 0, 1, 0, 1, 0, 0, 0 ]
, [ 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 ]
, [ 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ]
, [ 0, 1, 0, 1, 1, 0, 0, 0, 0, 0 ]
, [ 1, 1, 0, 1, 1, 0, 1, 0, 1, 0 ]
, [ 0, 1, 0, 0, 1, 0, 1, 0, 0, 0 ]
, [ 1, 1, 1, 0, 1, 0, 1, 0, 0, 0 ] ]
```

Next we build the graph object,

```N = len( knoki )
g = Graph()
g.set_directed( True )
v = [ g.add_vertex() for i in range(N) ]
for i in range(N):
for j in range(N):
if knoki[i][j] == 1:
```

Then we can visualize different things like the closeness of the nodes,

```g = GraphView( g, vfilt=label_largest_component(g) )
c = closeness( g )
graph_draw( g, vertex_fill_color=c,
vertex_size=prop_to_size(c, mi=5, ma=15),
vcmap=matplotlib.cm.gist_heat,
vorder=c, output="KNOKI_closeness.png",
vertex_text=g.vertex_index,
vertex_font_size=18 )
``` ## 2 thoughts on “Introduction to Graph Tool”

1. Tiago Peixoto says:

Maybe you would like to know that graph-tool has been included in homebrew, and the installation instructions you linked to are now outdated. It is now much simpler to install. See here

Also, if you have an adjacency matrix to begin with, you can populate your graph with the function Graph.add_edge_list().

1. cjohnson318 says:

Thanks Tiago! I’ll update this when I get a chance!

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.84607434,"math_prob":0.9864512,"size":3287,"snap":"2021-31-2021-39","text_gpt3_token_len":1001,"char_repetition_ratio":0.15625952,"word_repetition_ratio":0.14754099,"special_character_ratio":0.31153026,"punctuation_ratio":0.22516556,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97531056,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,4,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-17T10:48:04Z\",\"WARC-Record-ID\":\"<urn:uuid:3f30b8b8-2ee0-4c1b-8409-478343b6652f>\",\"Content-Length\":\"42395\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0f09bf18-75da-4c69-b3a5-d937780a0cb0>\",\"WARC-Concurrent-To\":\"<urn:uuid:c6fcf31e-411b-4b9c-a82c-be32d6b97b3b>\",\"WARC-IP-Address\":\"192.124.249.55\",\"WARC-Target-URI\":\"https://connor-johnson.com/2015/04/14/introduction-to-graph-tool/\",\"WARC-Payload-Digest\":\"sha1:DMUG7YKGCTQCQZELZMVLOQPP43YPUZ35\",\"WARC-Block-Digest\":\"sha1:FYCY6GRBE5RZICKSYVGUPOMCKAAWHL5V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780055632.65_warc_CC-MAIN-20210917090202-20210917120202-00142.warc.gz\"}"}