I have a node in a graph that acts as a sort of 'temporary connector' node. I'd like to remove that node and update the edges in the graph so that all of its immediate predecessors point to its immediate successors.
Is there baked-in functionality to do that in networkx, or will I need to roll my own solution?
Example:
I have a graph 1 > 2 > 3. I'd like to remove node 2 and end up with the graph 1 > 3.
Here's how I am currently doing it:
In [230]: g = nx.DiGraph()
In [231]: g.add_edges_from([(1,2),(2,3)])
In [232]: g.edges()
Out[232]: [(1, 2), (2, 3)]
In [233]: predecessors = g.predecessors(2)
In [234]: successors = g.successors(2)
In [235]: new_edges = [(p,s) for p in predecessors for s in successors]
In [236]: new_edges
Out[236]: [(1, 3)]
In [237]: g.remove_node(2)
In [238]: g.add_edges_from(new_edges)
In [239]: g.nodes()
Out[239]: [1, 3]
In [240]: g.edges()
Out[240]: [(1, 3)]