def shallow_reverse ( g ) : new_g = networkx . DiGraph ( ) new_g . add_nodes_from ( g . nodes ( ) ) for src , dst , data in g . edges ( data = True ) : new_g . add_edge ( dst , src , * * data ) return new_g
reverse edges of a graph python
Make a shallow copy of a directional graph and reverse the edges . This is a workaround to solve the issue that one cannot easily make a shallow reversed copy of a graph in NetworkX 2 since networkx . reverse ( copy = False ) now returns a GraphView and GraphViews are always read - only .
def shallow_reverse(g):
    """
    Make a shallow copy of a directional graph and reverse the edges. This is a workaround to solve the issue that one
    cannot easily make a shallow reversed copy of a graph in NetworkX 2, since networkx.reverse(copy=False) now returns
    a GraphView, and GraphViews are always read-only.

    :param networkx.DiGraph g:  The graph to reverse.
    :return:                    A new networkx.DiGraph that has all nodes and all edges of the original graph, with
                                edges reversed.
    """

    new_g = networkx.DiGraph()

    new_g.add_nodes_from(g.nodes())
    for src, dst, data in g.edges(data=True):
        new_g.add_edge(dst, src, **data)

    return new_g