I am implementing (considering it actually) a control that allows users to create a web diagram out of a series of nodes. The purpose will be to create a “flow chart” of sorts out of a series of questions to be asked by another part of the software; for each question, the answer chosen determines which question should be asked next. It’s a little FSMish but much smarter than a linear progression of questions of the form “If you answered X to question Y, please answer the following…”. The graph is a web and not guaranteed to be a tree, because the user defining the graph may want to ask a couple follow-up questions and then return to the “normal” line of questioning, thus two different nodes may “merge” to the same child node. However, the web is guaranteed not to be circular and to have one starting point, thus there are a finite number of paths through the graph and all of a finite length.
Here’s the question. I would like to have an “arrange” button (or simply auto-arrange) that rearranges the nodes so that the number of lines connecting nodes of the web that have to cross each other is minimized. Most flow-charting tools have a feature to do this, but my Google-fu has failed to find a general-purpose algorithm of this type. I’ve identified it as the “crossing number” problem, but it appears that there is no general solution to finding the minimal crossing number of a graph with V nodes and E paths, and that any such solution would be NP-hard (if a particular sub-problem could be solved in a single operation, then the full problem can be solved in polynomial time). On top of that, none of the references I’ve found detail algorithms that can lay out a graph with a minimal (to say nothing of “minimum”) crossing number.
Any hints?
EDIT: I gave Sharkos the tick for his excellent references. However, assuming that the graph has a definite start point, is one-way and non-circular, a working algorithm (maybe not optimal) is actually pretty simple. In pseudocode:
"
Give all nodes an initial XScore, YScore and LinkScore of 0
Determine the start node (either designated, or the one not linked to by any other)
Set start node's XScore and YScore to 1
Set running YScore = 1
Start recursion
for each path from node
if node on other end has XScore <= current
set other node's XScore to current + 1
if node on other end has YScore <= running YScore
set other node's YScore to running YScore
increment other node's LinkScore
Recurse with node on other end
Increment running YScore
Order nodes by XScore, then YScore.
--If the graph happens to be planar, we're done.
--To minimize crossover:
for each XScore
for each node with that XScore
if the next node with the same XScore has a higher LinkScore
swap the two nodes, exchanging YScores
Here’s a Master’s thesis on the topic, which gives some algorithms with discussion, and which gives references to lots and lots of different people’s approaches, from exact algorithms to approximation algorithms.
However, for a fairly simple, concrete pseudocode version of the ‘planarization method’, apparently (not an expert, though I have studied graph theory and it sounds plausibly useful) a common approximation, see section 2.5 in this chapter from the Handbook of Graph Drawing and Visualization, which is freely available online.
Hopefully that’s the sort of thing you wanted?