# Is this graph problem NP-hard / NP-complete?

I have a certain kind of graphs. They are DAGs similar to dependency graphs but strictly hierarchical, that is, each node belong to a certain level in the hierarchy and cannot be moved up or down when the graph is laid out onto a plane. The nodes can be arbitrarily exchanged with others in the same level.

Here's an example:

The problem:

Find an ordering (permutation) of the nodes in each level of the hierarchy such that the total number of edge crossings is minimal.

The problem could be, I suppose, rephrased as a decision problem: Given an ordering of nodes in each level, decide whether that ordering is minimal in terms of number of edge crossings.

For example, in the above graph embedding there are 3 edge crossings, however, by reordering the nodes in a certain way we obtain an embedding that only has 1 edge crossing (I believe this is minimum for this particular graph):

(Nodes that have been moved are highlighted.)

The question:

Is this problem NP-hard? I'm having a hard time finding correspondence to one of the more well-known NP-hard/NP-complete problems but at the same time I'm having a hard time finding a polytime solution.

• What specifically have you tried? Where did you get stuck? – Raphael Jun 9 '17 at 11:32
• First thing I tried was adapting the Sugiyama approach (that was quite some time ago and I do remember the specific details today) and then variants of force-directed layouting. Both of these methods unfortunatelly are usually only able to find a local minimum which may be quite far from the global one. Recently I looked into planarity testing, specifically Tremaux trees, whether that would somehow be beneficial. There are common features to both problems, but the planarity one is more restricted and so I couldn't really apply that here. – kralyk Jun 9 '17 at 12:16
• Next I'm planning to look into exact cover problem, that seems to be sortof kindof in the same vein, specifically, seems like plynomio solving algorithms might be relevant. I've yet to do that. As for proving NP-hardness, I haven't found a problem similar enough to reduce to mine, but I'm not very good at that so it's probably just me. – kralyk Jun 9 '17 at 12:19
• To be clear, in the resulting graph drawing, the vertices in each level of the graph must be on a straight line, and the lines for the different rows must be parallel? But the vertices at a particular level don't have to be equally spaced, so it's more than just a permutation that you're looking for, right? – David Richerby Jun 9 '17 at 20:20
• @DavidRicherby That's right. – kralyk Jun 9 '17 at 20:30

Turns out this is indeed an NP-complete/NP-hard problem according to wikipedia and Eades et al.

• The NP-hardness applies even when there are only two layers, so the number of layers being small doesn't qualitatively help. ​ On the other hand, when the layers themselves are all small, this can be solved efficiently by dynamic programming, one layer at a time. ​ ​ ​ ​ – user12859 Sep 1 '17 at 2:04
• @RickyDemer I know the general idea of dynamic programming and some applications, but it's unclear to me how it would be applied in this case. Can you be more specific? – kralyk Sep 1 '17 at 13:06
• For all permutations of the top layer, the minimum number of crossings above that layer with that permutation is zero (since there are no edges above the top layer). ​ For each other layer L and each permutation of that layer, the minimum number of crossings above L is the minimum, taken over the permutations of the layer L' that is just above L, of [[the number of crossings between L and L' with their permutations] plus [the minimum number of crossings above L' with that layer's permutations]]. ​ ​ ​ ​ – user12859 Sep 1 '17 at 19:04
• @RickyDemer Ah, ok, that's interesting. Thanks! – kralyk Sep 2 '17 at 16:28