Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Networkx网边_Python_Networkx - Fatal编程技术网

Python Networkx网边

Python Networkx网边,python,networkx,Python,Networkx,我有一个文件的边缘是重复的一个很好的理由 复制是双向的,这意味着我有A->B多次,也有B->A多次 我需要净边,意思是加上所有A->B,减去所有B->A。其结果应该是A->B或B->A边的正值,如A->B中的负值应该是B->A 我从这个脚本开始(在这里找到),它可以很好地执行所有A->B的求和,但是它也可以将B->A求和到A->B边,当我需要它进行减法时 初始代码 import networkx as nx # weighted MultiGraph M = nx.MultiGraph() M.

我有一个文件的边缘是重复的一个很好的理由

复制是双向的,这意味着我有A->B多次,也有B->A多次

我需要净边,意思是加上所有A->B,减去所有B->A。其结果应该是A->B或B->A边的正值,如A->B中的负值应该是B->A

我从这个脚本开始(在这里找到),它可以很好地执行所有A->B的求和,但是它也可以将B->A求和到A->B边,当我需要它进行减法时

初始代码

import networkx as nx
# weighted MultiGraph
M = nx.MultiGraph()
M.add_edge(1,2,weight=7)
M.add_edge(1,2,weight=19)
M.add_edge(2,3,weight=42)

# create weighted graph from M
G = nx.Graph()
for u,v,data in M.edges(data=True):
    w = data['weight'] if 'weight' in data else 1.0
    if G.has_edge(u,v):
        G[u][v]['weight'] += w
    else:
        G.add_edge(u, v, weight=w)

print(G.edges(data=True))
# [(1, 2, {'weight': 26}), (2, 3, {'weight': 42})]
#trying to subtract from 1->2 edge above
M.add_edge(2,1,weight=26)

#reprocess entire calculations
G = nx.Graph()
for u,v,data in M.edges(data=True):
    w = data['weight'] if 'weight' in data else 1.0
    if G.has_edge(u,v):
        G[u][v]['weight'] += w
    else:
        G.add_edge(u, v, weight=w)

print(G.edges(data=True))
#results shows edge above added 26, not subtracted
#[(1, 2, {'weight': 52}), (2, 3, {'weight': 42})]
然后我添加一个B->a边

import networkx as nx
# weighted MultiGraph
M = nx.MultiGraph()
M.add_edge(1,2,weight=7)
M.add_edge(1,2,weight=19)
M.add_edge(2,3,weight=42)

# create weighted graph from M
G = nx.Graph()
for u,v,data in M.edges(data=True):
    w = data['weight'] if 'weight' in data else 1.0
    if G.has_edge(u,v):
        G[u][v]['weight'] += w
    else:
        G.add_edge(u, v, weight=w)

print(G.edges(data=True))
# [(1, 2, {'weight': 26}), (2, 3, {'weight': 42})]
#trying to subtract from 1->2 edge above
M.add_edge(2,1,weight=26)

#reprocess entire calculations
G = nx.Graph()
for u,v,data in M.edges(data=True):
    w = data['weight'] if 'weight' in data else 1.0
    if G.has_edge(u,v):
        G[u][v]['weight'] += w
    else:
        G.add_edge(u, v, weight=w)

print(G.edges(data=True))
#results shows edge above added 26, not subtracted
#[(1, 2, {'weight': 52}), (2, 3, {'weight': 42})]

如果希望代码正常工作,则应将
M.add_-edge(2,1,weight=26)
更改为
M.add_-edge(2,1,weight=-26)
,否则需要插入If-else语句来修改代码。如果边是a->B类型,则
G[u][v]['weight']+=w
,否则
G[u][v]['weight']-=w
。节点标签是数字类型吗?按照你的玩具示例,你可以做一些类似于
w=w的事情,如果u
在我看来,你的最终图形
G
应该是定向的,而不是无向的。因为如果它是一个无向图,那么它无法区分
A
B
边和
B
A
边之间的区别。