2017-06-21 46 views
0

我目前正在审查一个工作系统,以确定可以优化的区域。我发现,在循环低于%左右,70在Python中更新容量igraph

for t in G.get_edgelist(): 
    eid = G.get_eid(*t) 
    origin = G.vs[t[0]]['name'] 
    destin = G.vs[t[1]]['name'] 

    if fc.cpdict[origin]['node_type'] == 'dependency': 
     cp_func[nodes.index(destin)] *= cp_func[nodes.index(origin)] 

    cap = cp_func[nodes.index(origin)] 
    G.es[eid]["capacity"] = cap 

系统需要更新,因为模型时最后一次迭代已经改变边缘的容量增加了运行时间。在why-is-add-edge-function-so-slow-ompared-to-add-edges答案状态

原因是igraph在C层中使用索引边界列表作为其数据结构。该索引使得可以在固定时间内查询特定顶点的邻居。如果图形很少发生变化,这很好,但是当修改操作比查询要频繁得多时,这会变成一种负担,因为每当添加或删除边缘时,都必须更新索引。

有没有更好的方法来做这个更新。

回答

0

如果别人正在寻求帮助,或有更好的解决方案。回顾我有以下改动去的文档后:

def update_capacity(self, components, comp_sample_func): 
     for comp_index, (comp_id, component) in enumerate(components.iteritems()): 
     for dest_index, dest_comp_id in enumerate(component.destination_components.iterkeys()): 
      if component.node_type == 'dependency': 
       comp_sample_func[dest_index] *= comp_sample_func[comp_index] 

      edge_id = self.comp_graph.get_eid(comp_id, dest_comp_id) 
      self.comp_graph.es[edge_id]['capacity'] = comp_sample_func[comp_index] 

我创建的节点具有相同顺序我的有序字典,然后检索与该指数的顶点。这给了10-20%的改善。