如果你有一个10^6-10^7顶点的稠密图(是一些医学数据还是社交图?它可以改变一切),你不应该使用networkx,因为它是用纯Python编写的,所以它比graph-tool或{}慢10-100倍。在您的例子中,我建议您使用graph-tool。它是最快的(~asigraph)Python图形处理库。在
graph-tool行为与networkx不同。当您创建networkx节点时,它的标识符是您在节点构造函数中编写的,这样您就可以通过节点的ID来获取该节点。在graph tool中,每个顶点ID都是从1到graph\u SIZE的整数:Each vertex in a graph has an unique index, which is always between 0 and N−1, where N is the number of vertices. This index can be obtained by using the vertex_index attribute of the graph (which is a property map, see Property maps), or by converting the vertex descriptor to an int.
关于图、顶点或边的所有附加信息都存储在property maps中。当您将.add_edge_list()与hashed=True一起使用时,新的属性映射将作为.add_edge_list()的结果返回。所以在你的例子中,你应该像这样处理你的顶点:# Create graph
g = grt.Graph(directed=False)
# Create edge list
# Why frozensets? You don't really need them. You can use ordinary sets