簡體   English   中英

更新python igraph中的容量

[英]Updating the capacity in python igraph

我目前正在審查一個工作系統,以確定可以優化的區域。 我發現下面的循環將運行時間增加了約70%

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

系統需要更新自模型時間的上一次迭代以來已更改的邊的容量。 為什么添加邊緣功能如此緩慢而難以添加邊緣的問題中給出了答案

原因是igraph在C層中使用索引邊緣列表作為其數據結構。 索引使得可以在恆定時間內查詢特定頂點的鄰居。 如果圖形很少更改,則很好,但是當修改操作比查詢頻繁得多時,它將成為負擔,因為無論何時添加或刪除邊,都必須更新索引。

有沒有更好的方法來執行此更新。

如果其他人正在尋求幫助,或者有更好的解決方案。 閱讀文檔后,我進行了以下更改:

    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%的改善。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM