我需要能够在python中操作大型(10 ^ 7节点)图。与每个节点/边对应的数据是最小的,例如少量的字符串。就内存和速度而言,最有效的方法是什么?
字典更灵活,实现起来更简单,但我直观地期望列表的列表更快。列表选项还要求我将数据与结构保持分开,而字典允许这样的事情:
graph[I][J]["Property"]="value"
我强烈建议你NetworkX...。这是经过测试的,也是大多数“研究”类型在需要分析基于网络的数据时所使用的第一个工具。我在笔记本电脑上操作了100个数千边的图形,没有问题。它的功能丰富,非常容易使用。您会发现自己更多地关注眼前的问题,而不是底层实现中的细节。
例ERD-Rényi随机图生成与分析
"""
Create an G{n,m} random graph with n nodes and m edges
and report some properties.
This graph is sometimes called the Erd##[m~Qs-Rényi graph
but is different from G{n,p} or binomial_graph which is also
sometimes called the Erd##[m~Qs-Rényi graph.
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
__credits__ = """"""
# Copyright (C) 2004-2006 by
# Aric Hagberg
# Dan Schult
# Pieter Swart
# Distributed under the terms of the GNU Lesser General Public License
# http://www.gnu.org/copyleft/lesser.html
from networkx import *
import sys
n=10 # 10 nodes
m=20 # 20 edges
G=gnm_random_graph(n,m)
# some properties
print "node degree clustering"
for v in nodes(G):
print v,degree(G,v),clustering(G,v)
# print the adjacency list to terminal
write_adjlist(G,sys.stdout)
可视化也很简单:
二次
二次
尽管这个问题现在已经很老了,但我认为值得提及我自己的python模块,用于进行图形操作。由于数据结构和算法是用C++实现的,采用模板元编程,使用Boost图库,因此效率很高。因此,它的性能(在内存使用和运行时)都可以与纯C++库相媲美,并且可以比典型的python代码好几个数量级,而不牺牲易用性。我经常用它来处理非常大的图表。