3

I need an algorithm to (efficiently) solve a problem that has come up in some diagramming software that I am writing.

I have two sets of nodes, N and M. Each node n0 in N has 0 to M connections to a unique (for n0) node in M. These sets of nodes are to be arranged on two parallel horizontal lines, with the N nodes in the line above the M nodes. The connections will be drawn as straight lines from N to M.

What I need to do is to rearrange the N and M nodes within their horizontal lines, to minimize crossings. Is their any way to do this that is more efficient than just naively enumerating all possible arrangements, which is O(N!*M!)? (actually, its considerably worse than that, since each connection has to be checked for crossing also).

References to relevant literature are welcome also, though an explanation as to why its relevant is desired.


As has been pointed out, in the general case this could be considered a bipartite graph (the sets N and M) planarization algorithm. However, this specific problem is considerably more restricted than that (which I hope can make it faster) and is required to produce additional information (which may make it slower), as follows:

  1. the diagram's restrictions are that the connections must be drawn as straight lines from N to M. In graph theory, this practically means that the connections cannot go behind the nodes in N or M, only between them. Thus, the 2x2 case with four connectors can be planarized in the general Bipartite graph case, but cannot in the case of my diagram.

  2. The additional information, is that if it cannot be planarized, I still need a minimal-crossing solution (or close to it). Generally, minimal-crossing algorithims are significantly different from those that only planarize.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137

4 Answers4

1

Your problem can be solved with force graph alignment, as long as you don't require that the nodes keep a specific position on their lines (even if you do require that with some alteration you could pull it off)

The main thing you need to change is make force alignment 1D instead of 2D, only aligning the nodes on the X axes instead of aligning nodes on both X and Y.

The premise of the algorithm is that you have forces acting on the nodes, so the nodes have repulsion acting on them causing them to move further away from each other, however nodes that are connected to one another have attraction acting on them that is greater then the repulsion. In each loop you add the forces up causing the nodes that are attracted to one another to come in closer to one another while nodes that are not will repel, your alignment is done when you sum a total force thats lower then some threshold, something like 0.001.

http://en.wikipedia.org/wiki/Force-based_algorithms_(graph_drawing)

1

The model you described is called bipartite graph, and what you request is a planarization algorythm. The best way to answer your question is to google a bit.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
  • Yes, it is a Bipartite Graph, thanks for that reminder. However, graphing and diagramming are not *quite* the same thing and this is not strictly speaking a planarization algorithm being requested since the diagramming constraints are *considerably* more restricted than that. – RBarryYoung Apr 10 '11 at 14:54
  • I understand the issue, but i see the algo requested to be quite non-generic and I just pointed out the direction in abscence of algo itself. What you may like is some algorithm (maybe greedy one) based on balanced tree search of the N set enumeration based on M set coverage of N elements. – iehrlich Apr 10 '11 at 15:06
  • I am considering this. Any idea how close an approach like this would come to an optimal solution, on average? – RBarryYoung Apr 10 '11 at 15:14
  • Good Q. Let's define the metric as the number of intersections. 'x1->y1' and 'x2->y2' can only intersect if (x1 != x2)||(y1 != y2) it means that first of all you should gather such Mi which are children of only particular Nj and place them 'under' corresponding Nj. Other Mk of M will cause possible intersections, so the problem is naturally reduced to (N, {Mk}) problem, where each Mk has more than one incoming edge. If |IN(Mk)|=2 for particular Mk, PRED(Mk) are two candidates to be pleced near w/ Mk between them. – iehrlich Apr 10 '11 at 15:28
  • Some reversal coverage approach like in boolean forms can be used. I think (don't ask me why) that an algo w/ precision of 2 can be built. – iehrlich Apr 10 '11 at 15:28
1

suddnely_me is right, but maybe you don't need a perfect solution. you could also try a really simple greedy algorithm. sorting all the nodes depending on the number of connections. then add one by one to the horizontal line.. didn't thought it through though.. but could lead to a simple approach..

duedl0r
  • 9,289
  • 3
  • 30
  • 45
  • Any idea how close an approach like this would come to an optimal solution, on average? – RBarryYoung Apr 10 '11 at 15:14
  • hmm...probably not that good.. I can't prove it, I guess it's not trivial :) it's just a feeling that a greedy algorithm could perform not entirely bad, since you said you have low connectivity. – duedl0r Apr 10 '11 at 18:24
  • and I think your problem is fairly complex, so you can also go for a genetic algorithm. the disadvantage would be, that your diagram doesn't look the same after every recalculation :) – duedl0r Apr 10 '11 at 18:26
0

How large are N and M? There's a solution based on dynamic programming that runs in time O(min(N, M)! * 2**max(N, M) * poly(N, M)), but I'm not sure if that's a significant improvement over brute force in your view.

zxc
  • 194
  • 2
  • generally in the range 10 to 99, and it needs to run in say 5 or less seconds on a typical PC (figure 2ghz). The good news is that the average connectivity is low, about 2 per node (2*(N+M)). So, I can absorb some badness (performance-wise) but not a lot. – RBarryYoung Apr 10 '11 at 18:05