【问题标题】:Using graph6 format with graph-tool使用带有图形工具的 graph6 格式
【发布时间】:2020-12-04 21:04:08
【问题描述】:

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

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

【问题讨论】:

    标签: graph graph-tool


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!我正在沿着类似的路线工作,但希望有一些不错的选择。遗憾的是,graph-tool 没有为 graph6 提供高效的加载器,也许格式没有被广泛使用......
    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 2019-06-05
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多