views:

90

answers:

2

I have a question: is there any reference (e.g. paper) with a proof of the planarity of flowchart layouts? Can anyone suggest an algorithm for generating flowchart (planar) layouts?

I know that there are some code-to-flowchart tools out there, but i'm unaware of their internals.

+1  A: 

It depends on what you call a "flowchart". If the flowchart is the simple kind, ie. a directed graph where no node points upward (to a node that could have possibly been visited previously), then what you've described is a tree whose embedding in the plane is trivial.

If however your flowchart has loops (cycles) then it is simple to construct a counterexample, a graph that is not not embeddable in the plane. For a contrived example (as no restrictions were stated) consider the complete graph K5, in which every node is connected to every other. This graph is not planar.

As for drawing graphs, I'd like to recommend the excellent tool GraphViz which draws (among other things) beautiful flowcharts with automatic layout. You can choose a rendering engine that tries to preserve some order in your graph and there exists an explicit option for hierarchical graphs.

Hooked
Hi Hooked my flowchart would have to model classic imperative-style loops (such as repeat-until or while-do). I have seen some approaches that generate planar layouts. However, in case this is only possible via node addition (not sure) then you are right of course. I recall that the detecting complete (sub)graphs K3,3 and K5 in a graph proves non-planarity.I'm aware of Graphviz and use it for quite some time now. As of version 2.22 that i'm using it has no layout engine specific to flowcharts. Kind regards-kavi
Nikolaos Kavvadias
You are correct in that detecting complete subgraphs of K3,3 and K5 could prove/disprove planarity (Kuratowski's theorem), but that method is an NP-complete problem. There are some O(N^3) approaches out there for planarity as well. As for GraphViz I was referring to the 'dot' layout, which draws a 'good enough' layout anytime I've needed a flowchart. If you don't like what you see you could always tweek an element by hand. Good luck!
Hooked
Planarity testing can be done in linear time: Efficient Planarity Testing (Hopcroft, Tarjan, 1974) (http://portal.acm.org/citation.cfm?id=321852)
Dimitris Andreou
+3  A: 

I disagree with Hooked. Flow charts, with some restrictions (using loops is NOT one of them), are planar. Some examples:

  • A single primitive command translates to a planar graph (a single node)
  • A sequence of statements: if the statements can be translated to planar graphs, the sequence can itself be translated to a planar graph (simply by connecting those subgraphs)
  • A function: the same as above
  • A (repeat-until, while-do, etc) loop is a sequence of statements that forms a cycle. Cycles are fine too, as long as they properly nest (and such constructs are designed to properly nest).
  • Goto statements (goto, or break/continue/return statements that may jump) are not ok. If you have a nested loop, and from inside that you jump out of the outer loop, such an edge would clearly intersect the cycle (loop, function) that contains it. If the code is translated to exit one loop at a time, these are fine too. (This translation is not too different from simply introducing nodes to model the intersections).

There must be a more systematic way to formally prove that a flow-chart deriving from compositions of a particular set of constructs is planar, I wish I could think of it in 5 minutes, but no luck :)

update: By the way, gotos can trivially create a K3,3 or K5, for example this is a K5 (in good old-fashioned QBasic!):

00 GOTO (INT(RND * 5) * 10) 
10 GOTO (INT(RND * 5) * 10)
20 GOTO (INT(RND * 5) * 10)
30 GOTO (INT(RND * 5) * 10)
40 GOTO (INT(RND * 5) * 10)
Dimitris Andreou
In hindsight I realize that using the word loops was a bad choice - I meant loops in the graph-theoretic sense, ie. a loop in the underlying graph was required (but not sufficient) for a non-planar graph. What was intended with the word you covered above, the idea of `goto`-like statements. Thank you for the Planarity O(N) Testing link in the comment!
Hooked