Python 多重图中的聚集边属性
我有一个在两个节点之间有多条边的图,如下例所示。我希望将满足条件的所有边聚合为一条边。在示例中:如果一条边属于同一组,那么我想将该边合并为一条边,并将1添加到Python 多重图中的聚集边属性,python,networkx,Python,Networkx,我有一个在两个节点之间有多条边的图,如下例所示。我希望将满足条件的所有边聚合为一条边。在示例中:如果一条边属于同一组,那么我想将该边合并为一条边,并将1添加到'freq'属性中 G = nx.MultiGraph() G.add_edge(1,2, group=1) G.add_edge(1,2, group=1) G.add_edge(1,2, group=2) G.add_edge(1,2, group=2) G.add_edge(1,2, group=3) G.add_edge(1,2,
'freq'
属性中
G = nx.MultiGraph()
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=4)
G.edges(data=True)
OUT: MultiEdgeDataView([(1, 2, {'group': 1, 'freq': 1}), (1, 2, {'group': 1, 'freq': 1}), (1, 2, {'group': 2, 'freq': 1}), (1, 2, {'group': 2, 'freq': 1}), (1, 2, {'group': 3, 'freq': 1})])
我想要的结果应该是:
OUT:MultiEdgeDataView([(1,2,{'group':1,'freq':2}),(1,2,{'group':2,'freq':2}),(1,2,{'group':3,'freq':1})])
此代码基本上适用于任意数量的边属性,并相应地更新频率。为了更清楚,我添加了这些评论
import networkx as nx
G = nx.MultiGraph()
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=4)
G.edges(data=True)
def get_same_attrib_key(u, v, data, G1, G2):
# First check if edge exists in new Graph
if G2.has_edge(u, v) is None:
return None
# Get data for all edges between u and v
new_edge_data = G2.get_edge_data(u, v)
if new_edge_data:
# This index will be used to update frequency in new graph
idx = 0
# For each edge between u and v, check the attributes
for dict_attrs in new_edge_data:
# Example 1: If G1 has edge from 1-->2 with data {'group': 1}
# and G2 has edge from 1-->2 with data {'group': 1, 'freq': 2},
# this if statement will return True
#
# Example 2: If G1 has edge from 1-->2 with data {'group': 1}
# and G2 has edge from 1-->2 with data {'group': 1, 'freq': 2, 'xyz':3},
# this if statement will return False
if len(new_edge_data[dict_attrs].items()-data.items())==1:
return idx
idx +=1
# No match found, hence return None
return None
G_agg = nx.MultiGraph()
for u, v, data in G.edges(data=True):
# Check if the current edge with same attribute dictionary
# exists in new Graph. This key is used for accessing data
# in Multigraphs.
key = get_same_attrib_key(u, v, data, G, G_agg)
# Update frequency if same edge exists
if key is not None:
G_agg[u][v][key]['freq'] += 1
# Else create a new edge with same data and a new key `freq` set to 1
else:
G_agg.add_edge(u, v, **dict({'freq': 1}, **data))
这将返回以下边:
MultiEdgeDataView([(1,2,{'freq':2,'group':1}),(1,2,{'freq':2,'group':2}),(1,2,{'freq':2,'group':3}),(1,2,{'freq':1,'group':4})]
现在,假设要添加任意数量的边缘属性关键点并获取频率,则此代码仍然有效,例如,对于下图:
G = nx.MultiGraph()
G.add_edge(1,2, group=1, other=5) #<------This edge attribute is diff. from others
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=1)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=3)
G.add_edge(1,2, group=4)
G.add_edge(1,2, group=2)
G.add_edge(1,2, group=2)
G.edges(data=True)
G=nx.MultiGraph()
G.添加_边(1,2,组=1,其他=5)#