2017-06-21 42 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%的改善。