2013-06-04 48 views
5

通过指定节点列表,我们可以很容易地从NetworkX图形中提取子图形,但我无法找到有效的方法来通过边缘执行子图提取。例如,提取子图由边的权重超过用户定义的阈值组成。根据NetworkX中的某些边缘属性有效提取子图形

目前我正在做下列方式:

## extracts all edges satisfy the weight threshold (my_network is directed): 
eligible_edges = [(from_node,to_node,edge_attributes) for from_node,to_node,edge_attributes in my_network.edges(data=True) if edge_attributes['weight'] > threshold] 
new_network = NetworkX.DiGraph() 
new_network.add_edges_from(eligible_edges) 

有没有更好的方式来做到这一点?

感谢您的答复。

回答

5

看起来像最好的解决方案。

您可以使用Graph.edges_iter()而不是Graph.edges()来节省内存。

>>> G = nx.DiGraph(((source, target, attr) for source, target, attr in my_network.edges_iter(data=True) if attr['weight'] > threshold)) 
+0

太好了,谢谢你的记忆保存提示! –

相关问题