Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse one edge in networkx graph

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?

like image 777
orange Avatar asked Oct 06 '14 02:10

orange


1 Answers

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})]
$ 
like image 56
Nick Russo Avatar answered Oct 20 '22 13:10

Nick Russo