简体   繁体   English

NetworkX DiGraph()到Graph()的边权重未求和,如何求和权重?

[英]NetworkX DiGraph() to Graph() with edge weights not summed, how to sum weights?

I have some relational data that I want to load into NetworkX, and ultimately convert it to a weighted Graph. 我有一些关系数据要加载到NetworkX中,并最终将其转换为加权图。

By nature, the relational edges are directed and weighted, and I want to retain the weight attribute during when I transform the graph. 从本质上讲,关系边是有方向的和加权的,我想在变换图形时保留weight属性。 Using the following code, I've been able to load the relational edges from a dictionary to a MultiDiGraph() : 使用以下代码,我已经能够将关系边缘从字典加载到MultiDiGraph()

MG = nx.MultiDiGraph([(i['source'],i['target']) for i in edges ])

Then, I convert the MultiDiGraph() to a DiGraph() , and condense duplicate edges into one and update the edge weight for each edge: 然后,我将MultiDiGraph()转换为DiGraph() ,并将重复的边压缩为一个并更新每个边的边权重:

G = nx.DiGraph()
for (u,v) in MG.edges():
    G.add_edge(u, v, weight=len(MG[u][v]))

From here, I want to convert the DiGraph() to a Graph() , and again retain and condense the edge weight: 从这里,我想将DiGraph()转换为Graph() ,并再次保留和压缩边缘权重:

g = G.to_undirected()

But the problem that I have is that it just seems to retain the first edge weight found for 'a' -> 'b' or 'b' -> 'a' . 但是我的问题是,它似乎保留了'a' -> 'b''b' -> 'a'的第一边缘权重。

What I want is the sum of these edges to be maintained as a weight when going to an undirected edge. 我想要的是将这些边缘的总和作为去往无向边缘时的权重。

Below is a sample show what I'm working with: 以下是显示我正在使用的示例:

# relational directed edge data containing duplicate edges 
edges = [{'source': 'a', 'target': 'b'},
         {'source': 'a', 'target': 'b'},
         {'source': 'a', 'target': 'b'},
         {'source': 'b', 'target': 'a'},
         {'source': 'a', 'target': 'c'},
         {'source': 'c', 'target': 'a'},
         {'source': 'c', 'target': 'd'},
         {'source': 'c', 'target': 'd'},
         {'source': 'd', 'target': 'c'}]

# load edges into a MultiDiGraph to maintain direction and duplicate edges
MG = nx.MultiDiGraph([(i['source'],i['target']) for i in edges ])

MG.edges(data=True) = [('a', 'c', {}),
                       ('a', 'b', {}),
                       ('a', 'b', {}),
                       ('a', 'b', {}),
                       ('c', 'a', {}),
                       ('c', 'd', {}),
                       ('c', 'd', {}),
                       ('b', 'a', {}),
                       ('d', 'c', {})]

# convert MultiDiGraph to a DiGraph and update edge weight
G = nx.DiGraph()
for (u,v) in MG.edges():
    G.add_edge(u, v, weight=len(MG[u][v]))

G.edges(data=True) = [('a', 'c', {'weight': 1}),
                      ('a', 'b', {'weight': 3}),
                      ('c', 'a', {'weight': 1}),
                      ('c', 'd', {'weight': 2}),
                      ('b', 'a', {'weight': 1}),
                      ('d', 'c', {'weight': 1})]

# convert DiGraph to a Graph, but edge weight not updated as sum, but first value
g = G.to_undirected()

g.edges(data=True) = [('a', 'c', {'weight': 1}),
                      ('a', 'b', {'weight': 1}),
                      ('c', 'd', {'weight': 1})]

Ultimately, I want the edge weight in the undirected graph to be as follows, but I can't figure out if this is an option with G.to_undirected or how to do this: 最终,我希望无向图中的边权重如下所示,但是我不知道这是否是G.to_undirected的选项或如何执行:

g.edges(data=True) = [('a', 'c', {'weight': 2}),
                      ('a', 'b', {'weight': 4}),
                      ('c', 'd', {'weight': 3})]

G.to_undirected() can not be used to control what data the undirected edges get, see networkx docs G.to_undirected()不能用于控制无向边获取的数据,请参见networkx docs

You may instead do the following: 您可以改为执行以下操作:

import networkx as nx

G = nx.DiGraph()
G.add_edges_from([('a', 'c', {'weight': 1}),
                  ('a', 'b', {'weight': 3}),
                  ('c', 'a', {'weight': 1}),
                  ('c', 'd', {'weight': 2}),
                  ('b', 'a', {'weight': 1}),
                  ('d', 'c', {'weight': 1})])

print G.edges(data=True)

g = nx.Graph()
g.add_edges_from(G.edges_iter(), weight=0)

print g.edges(data=True)

for u, v, d in G.edges_iter(data=True):
    g[u][v]['weight'] += d['weight']

print g.edges(data=True)

Basically, you create a new undirected Graph g and populate it with all edges in the directed Graph G. At this point you also initialize the edges' weights to 0. Lastly, you just add the weights to each edge in the undirected graph. 基本上,您将创建一个新的无向图g并将其填充到有向图G中的所有边缘。这时,还将边缘的权重初始化为0。最后,您只需将权重添加到无向图的每个边缘。 Note that in the undirected graph edge (u, v) is the same as (v, u). 请注意,在无向图的边缘(u,v)与(v,u)相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM