def combine_graphs(graph1,graph2,graph2_weight = 1): ''' Given two graphs of different edge (but same node) structure (and the same type),combine the two graphs,summing all edge attributes and multiplying the second one's attributes by the desired weights. E.g. if graph1.edge[a][b] = {'a': 1,'b':2} and graph2.edge[a][b] = {'a': 3,'c': 4},with a weight of 1 the final graph edge should be final_graph.edge[a][b] = {'a': 4,'b': 2,'c': 4} and with a weight of .5 the final graph edge should be {'a': 2.5,'c': 2}. inputs: Two graphs to be combined and a weight to give to the second graph ''' if type(graph1) != type(graph2) or len(set(graph2.nodes()) - set(graph1.nodes())) > 0: raise Exception('Graphs must have the same type and graph 2 cannot have nodes that graph 1 does not have.') # make a copy of the new graph to ensure that it doesn't change new_graph = graph1.copy() # iterate over graph2's edges,adding them to graph1 for node1,node2 in graph2.edges(): # if that edge already exists,Now iterate over the attributes if new_graph.has_edge(node1,node2): for attr in graph2.edge[node1][node2]: # if that attribute exists,sum the values,otherwise,simply copy attrs if new_graph.edge[node1][node2].get(attr) is not None: # try adding weighted value: if it fails,it's probably not numeric so add the full value (the only other option is a List) try: new_graph.edge[node1][node2][attr] += graph2.edge[node1][node2][attr] * graph2_weight except: new_graph.edge[node1][node2][attr] += graph2.edge[node1][node2][attr] else: try: new_graph.edge[node1][node2][attr] = graph2.edge[node1][node2][attr] * graph2_weight except: new_graph.edge[node1][node2][attr] = graph2.edge[node1][node2][attr] # otherwise,add the new edge with all its atributes -- first,iterate through those attributes to weight them else: attr_dict = graph2.edge[node1][node2] for item in attr_dict: try: attr_dict[item] = attr_dict[item] * graph2_weight except: continue new_graph.add_edge(node1,node2,attr_dict = attr_dict) return new_graph解决方法 内存将在您的代码中扩展两个位置:
1)制作graph1的副本(也许你需要保留一份副本)
2)使用graph2.edges()生成内存中所有边的列表,graph2.edges_iter()迭代边而不创建新列表
您也可以通过不同方式处理边缘数据来加快速度.您可以在迭代边缘时获取数据对象,而不必像字典查找那样执行:
def combined_graphs_edges(G,H,weight = 1.0): for u,v,hdata in H.edges_iter(data=True): # multply attributes of H by weight attr = dict( (key,value*weight) for key,value in hdata.items()) # get data from G or use empty dict if no edge in G gdata = G[u].get(v,{}) # add data from g # sum shared items shared = set(gdata) & set(hdata) attr.update(dict((key,attr[key] + gdata[key]) for key in shared)) # non shared items non_shared = set(gdata) - set(hdata) attr.update(dict((key,gdata[key]) for key in non_shared)) yIEld u,attr returnif __name__ == '__main__': import networkx as nx G = nx.Graph([('a','b',{'a': 1,'b':2})]) H = nx.Graph([('a',{'a': 3,'c':4})]) print List(combined_graphs_edges(G,weight=0.5)) # or to make a new graph graph = G.copy() graph.add_edges_from(combined_graphs_edges(G,weight=0.5))总结
以上是内存溢出为你收集整理的python – 在NetworkX中组合两个加权图全部内容,希望文章能够帮你解决python – 在NetworkX中组合两个加权图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)