I have downloaded a bunch of graphs from http://users.cecs.anu.edu.au/~bdm/data/graphs.html and I want to do some analysis. I want to use the graph-tool
Python module for this but I cant find a convenient way to convert from graph6
format to a format compatible with graph-tools
. There must be an easy way to do this... any help would be appreciated.
-- EDIT: A possible solution is convert from g6 to gt format... but I haven't found any tools that do this.
The graph6
format looks annoying to work with, but fortunately the documentation mentions a tool named showg
for pretty-printing graphs. It's easy to simply parse the output of that program.
First, build the showg
tool. (Use clang
or gcc
as appropriate for your system. Or just download the binary they provide on their website.)
$ curl -sL http://users.cecs.anu.edu.au/%7Ebdm/data/showg.c > showg.c
$ clang -o showg showg.c
$ ./showg --help
Download some example data and look at it. I think the -e
option produces the easiest output to work with.
$ curl -sL http://users.cecs.anu.edu.au/%7Ebdm/data/graph4.g6 > graph4.g6
$ ./showg -p10 -e graph4.g6
Graph 10, order 4.
4 5
0 2 0 3 1 2 1 3 2 3
Here's a simple script that reads the edge list from ./showg -p<N> -e
and creates a graph_tool.Graph
object:
# load_graph.py
import sys
import graph_tool as gt
# Read stdin and parse last line as a list of edge pairs
line = sys.stdin.readlines()[-1]
nodes = [int(n) for n in line.split()]
n0 = nodes[0::2]
n1 = nodes[1::2]
edges = list(zip(n0, n1))
# Load graph
g = gt.Graph()
g.add_edge_list(edges)
print("Loaded graph with the following edges:")
print(g.get_edges())
Let's give it a try:
$ ./showg -p10 -e graph4.g6 | python load_graph.py
Loaded graph with the following edges:
[[0 2]
[0 3]
[1 2]
[1 3]
[2 3]]