Is it possible to remove empty space that appears when using layers?

I’ve recently started to experiment with layers in Graphviz. Let’s assume that I specify that the layerselect attribute have a value of a. Then the output diagram doesn’t contain nodes and edges that are not assigned the layer a. But there is empty space left in the places where you’d expect the nodes and edges to appear. It looks out of place to me. This is especially visible if I select layers assigned to only a small number of nodes and edges, because then the empty space is bigger than the space taken by the nodes and edges rendered (e.g. half the diagram is empty). Can this empty space be removed?

Can you say more about what “remove empty space” means (and doesn’t mean).
I know of no way to “remove empty space” without doing a re-layout of the subset graph. The result may be quite different from the original.

Maybe “removal” isn’t the best term. Let’s see an example graph. If we want to get the whole graph, we run

echo '
    graph {
        subgraph cluster {
            a [ layer = "1" ]
            b [ layer = "2" ]
        }
    }
' \
    | dot \
    -Tpng

I’ve made a cluster because it has a border, so you can better see the empty space. So, we get

Now if we want to see only the nodes having assigned layer “1”, we run

echo '
    graph {
        subgraph cluster {
            a [ layer = "1" ]
            b [ layer = "2" ]
        }
    }
' \
    | dot \
    -Glayers='1 2' \
    -Glayerselect='1' \
    -Tpng

and we get

You can see that there is empty space where we’d expect node “b” to be rendered. This is the space I’m talking about.


PS I think it would look more intuitive to me if the second command above resulted in

Of course I don’t know to what extent it is feasible, this is only my view.

Maybe is the best I can guess.
Do you have a more complex example you can share?

You mean, do I have a real-life example that I could be helped with removing the space? No, so far I don’t, I’m still about to figure out how I’m going to use layers. I was just hoping that there is a general solution, like setting an attribute. But OK, if I happen to make a more complex graph in the future, I will post it.

Inspired by https://stackoverflow.com/questions/2674313/how-do-i-only-show-some-nodes-edges-on-graphviz-and-not-others/2848403, I thought that I could achieve the expected behavior with gvpr.

So, say we have a graph

graph {
    a [ layer = 1 ]
    b [ layer = 1 ]
    c [ layer = 2 ]

    a -- b
    a -- c
}

It looks

Now, if I want to have rendered only nodes on layer “1”, I do

gvpr -i 'N [ layer == 1 ]'

and I get

example-reduced

I need to say I like the simplicity of the gvpr script! (That said, I still don’t know gvpr in general, and I don’t like not to know languages I use.) I’ll check how I can utilize gvpr as a replacement of layers.


PS I wanted to compare it to the “layer mechanism”. But as I got a dangling edge, I needed to change the graph above. I have assigned layers to edges, too. Now the graph looks

graph {
    a [ layer = 1 ]
    b [ layer = 1 ]
    c [ layer = 2 ]

    a -- b [ layer = 1 ]
    a -- c [ layer = 2 ]
}

If I run

dot -Tpng -Glayers='1 2' -Glayerselect='1'

I get

We see that there is empty space where node “c” would appear.

If you only apply the layer attribute to nodes, you may be done. But layer can be applied to nodes, edges, and clusters, so the gvpr program might need a bit of enhancement.
The bigger problem is that as you delete objects from the input, the layout engine may create a wildly different output.
That may be desired or it may not.
Below are two outputs, one with a single node deleted. Made up, but it shows how small tweaks can make large differences.

This is how we intended it to work - there’s only one layout for the graph, and layers just controls subsets that are visible.

Full incremental (dynamic) layout is a different, harder, problem. Recently, Gordon Woodhull finished some work on this, including a demo.

Steve, yes, I want to stick to the simplest solution, at least for now. I’m going to assign layers only to nodes.

As for the differences in how the diagram looks like, I guess you mean that I have some layout in mind when I create a graph? Well, I don’t know if ideally I would have it or not, but from what I know Graphviz, I’m under the impression that it doesn’t support such approach anyway. I take it for granted that if I remove or add a node, the diagram might change more than one could assume. But I appreciate your mentioning about it.

Stephen, OK, I understand that it works this way. It’s not much of a problem since we have gvpr. Possibly it’s even how I would expect Graphviz to work if I consider that I might want the layout to change or not (what Steve has just mentioned). Like, I’d choose gvpr if I wanted the layout to change, or didn’t care about it, and I’d choose layers otherwise. Good to have two options instead of one.