如何在Networkx中使用Single_Source_Shortest_Path

时间:2014-06-05 03:52:20

标签: python networkx graph-databases

背景

我使用 Networkx 包为大约80,000个节点和230,000个边创建了一个图形数据库。数据库中的每个节点代表寄存器,边缘代表寄存器之间的依赖关系。分析的目标是确定相对于定义的度数截止(当前设置为3)的依赖性移除节点子集的影响。

我的方法是填充图形数据库,即80k节点和230k边缘(参见下面代码中的 g ),然后迭代表示已注册指定列表的节点子集使用 single_source_shortest_path()方法删除(请参阅 all_registered_to_be_removed )。然后,该方法生成详细说明所有受影响的依赖项的路径列表(请参阅 all_dependant_paths )。此列表用于填充新的图形数据库(请参阅 impact_graph )。

问题

据我所知,这种方法适用于少数路径,但很快就会崩溃。由于它会生成错误,因此通常会针对不同的节点触发错误。任何人都可以提出不同的方法吗?

impact_graph = nx.Graph()
for register in all_registered_to_be_removed:
    all_dependant_paths = nx.single_source_shortest_path(g, source=register, cutoff=3)
    impact_graph.add_path(all_dependant_paths)

堆栈错误

KeyError                                  Traceback (most recent call last)
<ipython-input-206-4291f9128bde> in <module>()
      1 impact_graph = nx.Graph()
      2 for tag in all_registered_to_be_removed:
----> 3     all_dependant_paths = nx.single_source_shortest_path(g, source=tag, cutoff=3)
      4     impact_graph.add_path(all_dependant_paths)

/usr/lib/pymodules/python2.7/networkx/algorithms/shortest_paths/unweighted.pyc in single_source_shortest_path(G, source, cutoff)
    252         nextlevel={}
    253         for v in thislevel:
--> 254             for w in G[v]:
    255                 if w not in paths:
    256                     paths[w]=paths[v]+[w]

/usr/lib/pymodules/python2.7/networkx/classes/graph.pyc in __getitem__(self, n)
    319         {1: {}}
    320         """
--> 321         return self.adj[n]
    322 
    323

0 个答案:

没有答案