Create graph with Graphviz limiting the depth of the representation

dot, graphviz

Solution

You can't use `dot` to show only part of a graph but you can use `gvpr` to programmatically edit the tree and then display the edited tree with `dot`. See the `gvpr` manual here for all the details.

As an example, given the file `tree.dot`:

digraph G {
    n [level = 1]
    n -> nL
    nL [level = 2]
    nL -> nLL
    nLL [level = 3]
    nLL -> nLLL
    nLLL [level = 4]
    nLL -> nLLR
    nLLR [level = 4]
    nL -> nLR
    nLR [level = 3]
    nLR -> nLRL
    nLRL [level = 4]
    nLR -> nLRR
    nLRR [level = 4]
    n -> nR
    nR [level = 2]
    nR -> nRL
    nRL [level = 3]
    nRL -> nRLL
    nRLL [level = 4]
    nRL -> nRLR
    nRLR [level = 4]
    nR -> nRR
    nRR [level = 3]
    nRR -> nRRL
    nRRL [level = 4]
    nRR -> nRRR
    nRRR [level = 4]
}

... which looks like this when plotted with `dot`:

... the command `gvpr -i 'N [level < 4]' tree.dot | dot -Tpng -otree3.png` produces the following image:

There are more sophisticated ways to use `gvpr` that might allow the part of the graph that is to be viewed to be selected without the need to label the nodes with their levels as I have done, but I'm not sufficiently expert with `gvpr` to say exactly how that might be done without more research than I have time for right now.

Problem

I have a .dot file that encodes a binary decision tree with lot of levels. In order, to visualise it, I use the command `dot -Tpng sample.dot > sample.png`. But the created .png is huge and hard to understand it. So I wonder if it is possible to limit the number of levels that are rendered in the output file form the command line, i.e. I don't want to modify the -dot file, I just want to modulate how the .png output is generated. I read the documentation but I couldn't find anything. For example, if I just want the three first levels of the decision tree, is there any modifier like: `dot -Tpng sample.dot > sample.png -L 3`?

Original source