Reverse one edge in networkx graph

混江龙づ霸主 提交于 2020-12-08 07:33:36

问题


I found DiGraph.reverse() to reverse the direction of all edges in the directed graph, but is there a way to change the direction of a specific edge only?


回答1:


It can certainly be done manually, but there's nothing in the API for it.

$ cat edges.py; echo; python edges.py 
import networkx as nx
G=nx.DiGraph()
G.add_edge(1,2,{'weight':.5})
G.add_edge(3,4,{'weight':1.0})
attrs = G[1][2]
G.remove_edge(1,2)
G.add_edge(2,1,attrs)
print G.edges(data=True)

[(2, 1, {'weight': 0.5}), (3, 4, {'weight': 1.0})]
$ 


来源:https://stackoverflow.com/questions/26209310/reverse-one-edge-in-networkx-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!