How can I reverse the direction of every edge in a Graphviz (dot language) graph?

dot, graphviz

Solution

Easiest way is to include a graph-level `dir` statement where you reverse the direction of the arrows. By default, the direction is `forward`. If you reverse it at the top of your graph, then without changing a single other line, the graph will show up the way you want.

What you have now is this:

digraph G
{
    edge [dir="forward"]; /* implied */
    A -> B [label="foo"];
    A -> B [label="bar"];
    B -> A;
    C;
}

What you want is this:

digraph G
{
    edge [dir="back"]; /* note the change to this line */
    A -> B [label="foo"];
    A -> B [label="bar"];
    B -> A;
    C;
}

Problem

I have a directed graph specified in Graphviz's `dot` language, e.g. ``` digraph G { A -> B [label="foo"]; A -> B [label="bar"]; B -> A; C; } ``` I want to automatically process this into a graph with its edges reversed, i.e. ``` digraph G { B -> A [label="foo"]; B -> A [label="bar"]; A -> B; C; } ``` I would like to use a robust solution (i.e. one that understands the graph and therefore probably doesn't use `sed`) that preserves any existing edge labels and other attributes. Note that I am not merely talking about getting `dot` to render my graph with the arrows pointing backward; I really need a graph whose edges are reversed. (In this case, I intend to reverse the edges, apply `prune`, then reverse the edges again.) How can I reverse the direction of every edge in a Graphviz (`dot`-language) graph?

Original source