反转networkx图中的一条边 [英] Reverse one edge in networkx graph

查看:80
本文介绍了反转networkx图中的一条边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 DiGraph.reverse()可以反转有向图中所有边的方向,但是有没有办法只改变特定边的方向呢?

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?

推荐答案

当然可以手动完成,但是API中没有任何内容.

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})]
$ 

这篇关于反转networkx图中的一条边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆