An Algorithm for Drawing Planar Graphs
Total Page:16
File Type:pdf, Size:1020Kb
An Algorithm for Drawing Planar Graphs Bor Plestenjak IMFMTCS University of Ljubljana Jadranska SI Ljubljana Slovenia email b orplestenjakfmfuniljsi AN ALGORITHM FOR DRAWING PLANAR GRAPHS SUMMARY A simple algorithm for drawing connected planar graphs is presented It is derived from the Fruchterman and Reingold spring emb edding algorithm by deleting all repulsive forces and xing vertices of an outer face The algorithm is implemented in the system for manipulating discrete mathematical structures Vega Some examples of derived gures are given Key words Planar graph drawing Forcedirected placement INTRODUCTION A graph G is planar if and only if it is p ossible to draw it in a plane without any edge intersections Every connected planar graph admits a convex drawing ie a planar drawing where every face is drawn as a convex p olygon We present a simple and ecient algorithm for convex drawing of a connected planar graph In addition to a graph most existing algorithms for planar drawing need as an input all the faces of a graph while our algorithm needs only one face of a graph to draw it planarly Let G b e a connected planar graph with a set of vertices V fv v g and a set of edges E 1 n fu v u v g Let W fw w w g V b e an ordered list of vertices of an arbitrary face in 1 1 m m 1 2 k graph G which is chosen for the outer face in the drawing The basic idea is as follows We consider graph G as a mechanical mo del by replacing its vertices with metal rings and its edges with elastic bands of zero length Vertices of the outer face W are xed in a vertices of a regular p olygon of a size k while all other vertices are let free Under the inuence of attractive forces in bands free vertices will move until the system nally reaches an equilibrium The layout in the equilibrium is a convex drawing The algorithm is included in package Vega under the name SchlegelDiagram Vega is Mathematica based system for manipulating graphs and other combinatorial structures such as groups maps etc devel op ed by IMFMTCS Ljubljana Vega is mainly written in Mathematica however it also includes external programs for time complex op erations eg the implementation of our algorithm ALGORITHM The algorithm is based on the forcedirected placement graph drawing algorithm by Fruchterman and Rein gold In each step the algorithm calculates the eect of attractive forces on each vertex and then moves the vertex in the direction of the resultant force The displacement in step i is limited to some maximum value cool i that we call temp erature and which satises lim cool i As the temp erature decreases i!1 to zero smaller and smaller displacements are allowed and after some numb er of steps displacements are so small that we can say that the layout freezes The algorithm ends after the prescrib ed numb er of iterations and if the forces and the co oling function cool i are chosen prop erly the nal layout is so near to the equilibrium that it is planar AN ALGORITHM FOR DRAWING PLANAR GRAPHS For the force b etween two adjacent vertices u and v we use the third order law 3 F C d uv where C is a constant and d kupos v posk is the distance We tested dierent order p owers and the result is a compromise b etween go o d results and an ecient calculation The algorithm consists of the following steps Position all vertices of an outer face W in vertices of a regular p olygon of size k inscrib ed into the unit circle and put all other vertices in the origin For i to iter ations a For all vertices v V set the resultant force F to zero v F v b For all edges u v E calculate the attractive force F and up date the resultant forces F and uv u F v 3 F C v pos upos uv F F F u u uv F F F v v uv c For all vertices v V W move vertex v in the direction of the resultant force F for the size v of the force but not more than for the value of cool i F v v pos v pos min jF j cool i v jF j v Constant C in has to b e in harmony with the maximum displacement Namely if C is to o large then in every vertex is shifted for the maximum displacement irresp ective of the force size On the other hand if C is to o small then the displacements are small and the algorithm do es not reach the equilibrium in a prescrib ed numb er of steps If the temp erature decreases to o slowly then displacements are to o large and to o many steps are needed to reach the equilibrium Yet if the temp erature decreases to o fast then the layout freezes in a non planar drawing A suggested values for constant C and function cool i are r n C p n cool i 32 i n p The expression is the average area b elonging to one vertex and therefore we take for the average n n in and distance b etween adjacent vertices This justies the presence of term n AN ALGORITHM FOR DRAWING PLANAR GRAPHS Figures to demonstrate how the algorithm works The graph is the icosahedral fullerene isomer of C derived in Vega from the do decahedron using two leapfrog transformations We usually pick the 180 largest face for the outer face but since p entagons are arranged in a shap e of a do decahedron we cho ose a p entagon in order to exhibit symmetry For C and cool i we apply and resp ectively It can b e seen from these gures how the edges closer to the b order untwist rst and how they force those in the middle to unfold until nally the layout b ecomes planar Figure C after iterations Figure C after iterations Figure C after iterations 180 180 180 MODIFICATIONS Periphericity If the algorithm is applied to a graph with a large numb er of faces eg a fullerene on many vertices then it pro duces a drawing with large faces on the b order and large numb er of crowded small faces in the middle of a gure 0 0 1 1 2 2 2 1 3 2 2 2 1 1 0 0 Figure Periphericity of a vertex In order for the algorithm to terminate with approximately equally arranged faces we assume that the co ecient C in is not equal for all bands Bands at the b order should b e stronger and bands in the middle should b e weaker For this purp ose we intro duce a p eriphericity of a vertex as the path distance from the vertex to the outer cycle The denition of the p eriphericity is more evident in Figure where vertices AN ALGORITHM FOR DRAWING PLANAR GRAPHS of a graph are lab eled according to their p eriphericities The co ecient of the band b etween vertices u and v is a function of p eriphericities uper and v per and we denote it by C uper v per Let maxper b e the maximum p eriphericity in the graph As we go through all edges u v E the bands with uper v per should b e the strongest and the bands with uper v per maxper should b e the weakest Best results are obtained when the co ecients C uper v per form a geometric progression in the form r n maxper uper v per expA C uper v per maxper For the constant A we prop ose A but other values can b e used as well pro ducing dierent planar layouts Figure C with A after Figure C with A after Figure C with A after 540 540 540 iterations iterations iterations Figures to demonstrate the eect of dierent values of A The graph is the icosahedral fullerene isomer of C In Figure A which equals the original algorithm In Figure A and faces in 540 the middle are more equally arranged The value A in Figure is to o large and the obtained drawing has large faces in the middle that squeeze the rest of the faces In all three case the co oling function is applied Oscil lation After some initial numb er of steps the vertices b egin to oscillate b etween two states If we compare the co ordinates with the co ordinates from two steps ago we can exit the algorithm when the dierence for every vertex is b elow the prescrib ed constant This approach reduces the numb er of steps without doing any real harm to the layout quality For the example we take the graph C and 180 3 4 5 with A For and the algorithm ends after and steps resp ectively Dierences b etween layouts are not visible with the naked eye For instance the maximum dierence b etween 3 the co ordinates of the same vertex after and steps is Nonplanar graphs Although the algorithm is derived for connected planar graphs it can b e applied to other graphs as well For every graph we can x an arbitrary set of vertices on the regular p olygon apply a mechanical mo del to a graph and search for an equilibrium Although the nal layout do es not need to b e AN ALGORITHM FOR DRAWING PLANAR GRAPHS planar it usually contains useful information ab out the symmetry and other prop erties of the graph EXAMPLES The following examples are all obtained using Vega We use Vega to determine faces of a planar graph using a PQtree algorithm for planarity testing and emb edding in linear time implemented by J Marincek Each face of a planar graph is a go o d choice for the outer face but usually we get nicer drawings if we cho ose the largest face It is advisable to compare drawings for dierent choices of the outer face and then cho ose 5 the most suitable one For all examples in this section we use with A and When present the term it in gure captions denotes the iteration in which the algorithm reached and exited Figure and Figure are drawings of the Herschel graph The Herschel graph is a wellknown example of a planar nonhamiltonian p olyhedron Notice how the dierent choice of the outer face aects the layout Figure Herschel graph Figure Herschel graph n m it n m it Next two gures represent the Tutte graph and the Grinb erg graph These two graphs are wellknown examples of planar