Search code examples
graphgraphvizdecision-treegraph-visualization

Force the left to right order of nodes in graphviz?


I want to draw a decision tree chart using graphviz.

The graph I want to draw looks like this:

enter image description here

I am using the following dot language:

graph a {
  A [shape=box; label="A"]
  B [shape=box; label="B"]
  al [shape=none; label="0"]
  bl [shape=none; label="1"]
  br [shape=none; label="0"]

  A -- al [label="0"];
  A -- B [label="1"];
  B -- bl [label="0"];
  B -- br [label="1"];

}

However my resulting graph looks like this:

enter image description here

How can I force the left to right order of the nodes generated by graphviz? Furthermore, as far as decision trees go are these two trees exactly the same even though the left to right ordering is different?


Solution

  • To fix this, you can just change the order of the B and al nodes:

    graph a {
      A [shape=box; label="A"]
      al [shape=none; label="0"]
      B [shape=box; label="B"]
      bl [shape=none; label="1"]
      br [shape=none; label="0"]
    
      A -- al [label="0"];
      A -- B [label="1"];
      B -- bl [label="0"];
      B -- br [label="1"];
    }
    

    By the way, the graphviz website had a forum post about this problem which you can have a look at if you get a chance.