将 graph6 格式与图形工具一起使用

Using graph6 format with graph-tool

我从http://users.cecs.anu.edu.au/~bdm/data/graphs.html下载了一堆图表,我想做一些分析。我想为此使用 graph-tool Python 模块,但我找不到方便的方法将 graph6 格式转换为与 graph-tools 兼容的格式。必须有一个简单的方法来做到这一点...任何帮助将不胜感激。

-- 编辑: 一个可能的解决方案是从 g6 格式转换为 gt 格式...但我还没有找到任何工具可以做到这一点。

使用 graph6 格式看起来很烦人,但幸运的是 documentation 提到了一个名为 showg 的工具,用于打印漂亮的图形。简单地解析该程序的输出很容易。

首先,构建 showg 工具。 (根据您的系统使用 clanggcc。或者只下载他们在其网站上提供的二进制文件。)

$ curl -s http://users.cecs.anu.edu.au/%7Ebdm/data/showg.c > showg.c
$ clang -o showg showg.c
$ ./showg --help

下载一些示例数据并查看。我认为 -e 选项产生最容易处理的输出。

$ curl -s 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

这是一个简单的脚本,它从 ./showg -p<N> -e 读取边缘列表并创建一个 graph_tool.Graph 对象:

# 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())

让我们试一试:

$ ./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]]