Comments 0

Document transcript

Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm forDrawing Undirected GraphsTimGA:Un Algoritmo Genetico para Dibujar Grafos noDirigidosTimo Eloranta (@)Erkki Makinen (em@cs.uta.fi)Department of Computer and Information Sciences,P.O.Box 607,FIN-33014 University of Tampere,FinlandAbstractThe problem of drawing graphs nicely contains several computa-tionally intractable subproblems.Hence,it is natural to apply geneticalgorithms to graph drawing.This paper introduces a genetic algorithm(TimGA) which nicely draws undirected graphs of moderate size.Theaesthetic criteria used are the number of edge crossings,even distri-bution of nodes,and edge length deviation.Although TimGA usuallyworks well,there are some unsolved problems related to the geneticcrossover operation of graphs.Namely,our tests indicate that TimGA'ssearch is mainly guided by the mutation operations.Key words and phrases:Genetic algorithm,graph drawing,undi-rected graphs.ResumenEl problem de dibujar grafos apropiadamente contiene varios sub-problemas computacionalmente intratables.Por lo tanto es naturalaplicar algoritmos geneticos al dibujo de grafos.Este artculo introdu-ce un algoritmo genetico (TimGA) que dibuja bien grafos no dirigidosde tama~bo moderado.Los criterios esteticos usados son el numero decruces de aristas,la distribucion uniforme de los nodos y la desviacionde las longitudes de las aristas.Aunque TimGA usualmente trabajabien,hay algunos problems no resueltos relacionados con la operaciongenetica de cruzamiento de grafos.De hecho,nuestras pruebas indicanRecibido 2000/12/13.Revisado 2001/09/15.Aceptado 2001/10/10.MSC (2000):Primary 68R10,05C85.Work supported by the Academy of Finland (Project 35025).156 Timo Eloranta,Erkki Makinenque la busqueda realizada por TimGA esta guiada principalmente porlas operaciones de mutacion.Palabras y frases clave:algoritmo genetico,dibujo de grafos,grafosno dirigidos.1 IntroductionThe problem of drawing graphs nicely is completely solved only in some veryspecial cases [8].Irrespective of the aesthetic criteria used,the problemusuallycontains several computationally intractable subproblems [2].This motivatesthe use of methods of genetic algorithms and other soft-computing approaches.For earlier works following this line of research,see e.g.[4,6,10,12,13,14,16,17].This paper introduces a genetic algorithm TimGA (Timo's Genetic Algo-rithm) for drawing undirected graphs.TimGA owes some of its basic datastructures to Groves et al.'s algorithm [10].However,since undirected edgesinstead of directed ones are considered,most decisions dier from those madeby Groves et al.TimGA outputs grid drawings with straight line edges.In what follows we assume that the reader is familiar with the basics ofgenetic algorithms and graph theory as given e.g.in [15] and [11],respectively.2 Selection and the evaluation functionTimGA draws graphs in an N N matrix.Each node is located in a squareof the matrix and all edges are drawn as straight lines.To represent a graphwith n nodes and m edges we use a 2  n matrix to indicate the positionsof the nodes and a 2  m matrix to indicate the edges by storing pairs ofnodes.The corresponding end points are then found from the node matrix.Figure 1 shows a simple example of the representation used.Groves et al.[10]have used similar representation for nodes.It should be noted that while ourrepresentation of graphs resembles that in [10],the algorithms otherwise diera lot.For example,the evaluation fucntion used in [10] is totally dierent thanours.One of the crucial points of a genetic algorithm is the method of selectingchromosomes to the genetic operations.TimGA uses the linear normalizationsuggested by Davis [7] together with elitism.The linear normalization worksas follows.The chromosomes are sorted in decreasing order by their eval-uation function values.The best chromosome gets a certain constant valueDivulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 157123456782 4 5 6 7 81nxy311 22 33334444556618 55 664788527736EdgesNodesFigure 1:The representation of a sample graph.(e.g.100) and the other chromosomes get stepwise decreasing constant values(e.g.98,96,94,...).Chromosomes are then selected to the genetic opera-tions proportionally to the values so obtained.Depending on the length ofthe step (the dierence between the consecutive constant values;two in theabove example),this method can be parametrized to give a desired emphasisto the best chromosomes.TimGA allows the user to set the length of thestep.By default,TimGA uses elitist selection,i.e.,the best chromosome isalways chosen as such to the next generation.The aesthetic criteria used are imported to genetic graph drawing algo-rithms in the form of the evaluation function (also called the tness function).TimGA tries to minimize the number of edge crossings,to distribute the nodesevenly over the drawing area,and to minimize the deviation of edge lengths.The positive terms (to be maximized) in the evaluation function are Minimum Node Distance Sum:The distance of each node fromits near-est neighbour is measured,and the distances are added up.The biggerthe sum the more evenly the nodes are usually distributed over thedrawing area. MinimumNode Distance (Number of Nodes (MinimumNode Distance)2):This term helps in distributing the nodes.The square of minimum nodedistance is multiplied by the number of nodes.The negative terms (to be minimized) in the evaluation function areDivulgaciones Matematicas Vol.9 No.2(2001),pp.155{171158 Timo Eloranta,Erkki Makinen Edge Length Deviation:The length of each edge is measured and com-pared to the"optimal"edge length,which is little more than the mini-mum edge length found from the present layout. Edge Crossings:The number of edge crossings is multiplied by the sizeof the drawing grid.(The grid is always a square.)The evaluation function is combined from the above variables.The exe-cutions reported in this paper are run with the following default coecients:2 Minimum Node Distance Sum2 Edge Length Deviation212 (Edge Length Deviation/Minumum Node Distance)14 (Number of Nodes  (Minimum Node Distance)2)1 (Edge Crossings  (Grid Size)2).These coecients were found in our preliminary test runs.TimGA spends most of its computation time in evaluating the chromo-somes.One of the problematic issues is the counting of the number of edgecrossings.There is a well-known method based on cross productions to checkwhether two line segments intersect [4,pp.889-890].More advanced methodsare introduced by Bentley and Ottmann [1] and Chazelle and Edelsbrunner[3].Unfortunately,the method of Chazelle and Edelsbrunner,though asymp-totically time optimal,is too complicated for the present application.On theother hand,the Bentley and Ottman's algorithm is too slow.Thus,we haveto use a method of our own for counting the number of edge crossing.Wekeep track of the movements of the nodes,and update the number of edgecrossings only when a node is moved.This method outperforms the Bentleyand Ottman's algorithm in the present situation.3 The genetic operationsThe crossover operation transforms two chromosomes into two new chromo-somes.TimGA has two types of crossover operations.RectCrossover worksas follows.First it randomly chooses a rectangle from the drawing area ofthe parent chromosomes.Then a rectangle of equal size is chosen from thedrawing area of the child chromosomes.The parent chromosomes exchangethe positions of the nodes inside the chosen rectangles.The rest of the nodesare kept unchanged,if possible.A sample RectCrossover is shown in Figure2.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 15921485763Child-224567813Child-125467813Parent-212374568Parent-121485763Child-224567813Child-125467813Parent-212374568Parent-1Figure 2:A sample RectCrossover.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171160 Timo Eloranta,Erkki MakinenThe sample RectCrossover operation of Figure 2 uses rectangles of size33;these are painted grey in the gure.(Other sizes of rectangles were alsoused in our preliminary tests,but 33 seems to be the optimal rectangle size.)The parents change the positions of the nodes 3 and 6 (from Parent-1) andnodes 1 and 3 (formParent-2).The nodes 1 and 3 keep their relative positionsin the grey area when it is moved from Parent-2 to Child-1.Moreover,sincethe chosen rectangle in Child-1 is empty,the rest of the nodes in Child-1 cankeep their old positions,i.e.the positions they have in Parent-1.On the otherhand,in Child-2 there are two nodes in the chosen 3 3 rectangle (nodes 2and 7).These must be moved outside the area.The rst possible place isthe square where the corresponding node is in the other parent.Since node2 of Parent-1 is in the square (2,5),this is the new position of the node inChild-2.This method does not work with node 7,since the square (8,4) isalready occupied by node 8.So,we have to place node 7 to an randomlychosen free square (8,8).RectCrossover closely resembles the Cont-Crossoveroperation of [9].The other crossover operation in TimGA is called ThreeNodeCrossover.A connected subgraph consisting of three nodes is chosen.The parents thenexchange the positions of the three nodes in question.If some of the newpositions are already occupied,the nodes in question are kept unchanged.Asample ThreeNodeCrossover is shown in Figure 3.Groves et al.[10] introduced about a dozen dierent mutation operations.In our tests we have used 16 dierent mutations of which 11 are from [10] andthe ve rest are new.Our tests indicate that mutation operations appliedto edges usually have better performance than those applied to nodes.Thefollowing eight mutation operations performed best in our tests: SingleMutate:Choose a random node and move it to a random emptysquare [10]. SmallMutate:Choose randomly two squares fromthe drawing area suchthat at least one of them contains a node.If both contain a node,exchange the nodes.If only one of them contains a node,then move thenode from the present location to the empty square [10]. LargeContMutate:Choose two areas of equal size and shape from thegrid.Exchange the contents of the chosen areas [10]. EdgeMutation-1:Choose a random edge and move it to a random newposition.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 1611235467Child-27456213Child-15467213Parent-21745623Parent-1Figure 3:A sample ThreeNodeCrossover.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171162 Timo Eloranta,Erkki Makinen EdgeMutation-2:Like EdgeMutation-1,but the length and angle of theedge is kept unchanged,if possible. TinyEdgeMove:Like EdgeMutation-2,but the edge is moved only atmost one square both horizontally and vertically. TwoEdgeMutation:Like EdgeMutation-2,but two edges incident witha same node are moved. TinyMutate:Like SingleMutate,but the node is moved only at mostone square both horizontally and vertically.The probability of using a certain mutation type depends on its perfor-mance in our tests.The operations introduced above have the following rel-ative probabilities (the bigger the probability the better performance in ourtests):TwoEdgeMutation 12/65EdgeMuation-2 10/65SingleMutate 10/65EdgeMutation-1 5/65LargeContMutate 5/65SmallMutate 5/65TinyMoveEdge 5/65TinyMutate 5/65.Moreover,eight additional mutation operations introduced in [10] are usedwith relative probability 1/65.Note that the mutation operations clearly havedierent roles:some of them are more suitable for tentative searching andsome others for ne tuning.4 ParametersThis chapter deals with the test runs which were done to x the variousparameters of TimGA.We did over 4000 runs using mainly the following testgraphs: a cycle with 48 edges a triangular grid with 28 nodes and 63 edges a complete binary tree with 63 nodes.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 163The number of edge crossings was the only criterion used in evaluating theresults.This naturally follows from straightforwardness of measuring the cri-terion in question.We believe that despite of the small number of test graphsused,the results can be generalized also to other graphs of approximately thesame size.The size of the grid.What is the optimal size of the drawing areafor our test graphs?This was tested for grids from 10 10 to 70 70.Theoptimumsize was 4040,and this size was used in all the tests to be reported.There were only small dierences between all the grid sizes from 20 20 to70 70;grids smaller than 10 10 were clearly inferior (for obvious reasons).The size of population.Population size should be large enough to givean unbiased view of the search space.On the other hand,too large populationsize makes the algorithm inecient,if not intractable.Surprisingly,TimGAseems to works best with very small populations.Figure 4 shows the averagenumbers of edge crossings with dierent population sizes after the runningtime of 15 seconds on a Power Macintosh with our complete tree test graph.(All the tests were executed on a 100 MHz Power Macintosh.) The resultswith bigger populations were not considerably better even when somewhatlonger execution times were allowed.These results suggest that the population size should not exceed 10.Weuse the population size 10 in the rest of our tests.Such a small population sizemight not t the Schema Theorem,the Building Block Hypothesis [15],andother theoretical principles of genetic algorithms.However small populationsgive us the best results!We interpret this phenomenon so that the crossoveroperations used are unable to sift the good properties (called schemata in [15])of the chromosomes from parents to children,and the search is mainly guidedby the mutation operations.Selection.Our tests advice to use large steps in the linear normalization.This means that the best chromosomes are strongly favoured.This can beconsidered as a further evidence for the fact that our crossover operations donot help.(Michalewicz [15,p.57] has noted that the use of selection methodsneglecting the actual relative dierences between the tness of chromes is alsoagainst the theoretical basis of genetic algorithms.The linear normalizationis one of these methods.)Crossover and mutation rates.As already mentioned,our crossoveroperations seem to have no positive eect to the search process.In our testswe used crossover rate 5 %.On the other hand,increasing the mutationrate makes the search more ecient all the way to the level 40 - 45 %.Stillincreasing the mutation rate over 45 % again makes the results worse.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171164 Timo Eloranta,Erkki Makinenpopulation size0102030405060700 10 20 30 40 50edge crossingsFigure 4:The average numbers of edge crossings as a function of the popula-tion size.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 165(a) (b)Figure 5:A sample input and the corresponding output.5 Example layoutsIn this chapter we present the results of applying TimGA to some typicalgraphs.All the drawings (and their computation times) reported in thischapter are produced on a Power Macintosh.The computation times givenin this chapter are not averaged over several runs as was done in the resultsreported in the previous chapters.This means that randomly selected initialpopulations may distort the results.Our rst example demonstrates the aesthetic criteria used.In Figure 5(a)a set of separate edges is shown.Fromthis input TimGA outputs the drawingshown in Figure 5(b).There are no edge crossings,the edges are distributedevenly over the drawing area,and the edges are of about the same length.The drawing of Figure 5(b) was created in 20 seconds;eliminating all theedge crossings took about a second.Figure 6 shows how TimGA tends to draw a cycle.This gure indicatesthat although the evaluation fuction does not contain a valiable directly mea-suring the existence of symmetry in the resulting drawing,the combination ofmaximizing Minimum Node Distance and maximizing Edge Length Deviationproduces certain approximation of symmetry in the drawings.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171166 Timo Eloranta,Erkki MakinenFigure 6:An output for a cycle.(a) (b)Figure 7:The eect of the grid size.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 167(a)(b)(c)Figure 8:Three sample drawings of grid graphs.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171168 Timo Eloranta,Erkki MakinenFigure 9:A layout for a triangular grid graph.Figure 7 demonstrates the eect of the grid size.The same graph (thecubic graph) is drawn using the grid sizes 12 12 (Figure 7(a)) and 20 20(Figure 7(b)).The Figure 7(b) suers from the tendency of drawing graphswith edges of equal length.This tendency is more easily realized in a gridwith more squares.Figure 8 shows three drawings for square grid graphs of dierent sizes.Thegraph of Figure 8(a) is drawn in a drawing area of size 2222,while the othertwo are drawn in a drawing area of size 4040.Figure 8(a) was produced in 8seconds using less than 5000 generations.Figure 8(b) took almost 90 secondsalthough the result is not completely symmetric.Even worse is the situationwith Figure 8(c):after the running time of 10 minutes TimGA was still unableto nd a planar drawing.The evaluation function does not"understand"thatmoving the top right node of the grid graph upwards would only temporarilycause more edge crossings.Figure 9 shows a nice drawing of a triangular grid graph with 35 nodesDivulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 169Figure 10:A layout for K8;8and 135 edges.The computation time was about three and a half minutes(5400 generations).We end this chapter with some remarks concerning the Edge CrossingProblem (ECP).Given an undirected graph G,ECP is the problem of deter-mining the minimum number of edge crossings (denoted by (G)) among thelayouts of G.ECP is known to be NP-complete [9].The following approxi-mation is known for the crossing number of complete bipartite graphs [10,p.123](Km;n)  bm2cbm12cbn2cbn 12c:TimGA easily reaches the above bound for graphs Km;m,where m  12.Figure 10 shows a drawing for K8;8.6 ConclusionsTimGA nicely draws most graphs of moderate size.However,it suers fromthe lack of proper crossover operation which would speed up TimGA's com-putations by decreasing the number of generations needed.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171170 Timo Eloranta,Erkki MakinenReferences[1] Bentley,J.L.,Ottmann,T.A.Algorithms for reporting and countinggeometric intersections,IEEE Transactions on Computers,C-28(1979)643{647.[2] Brandenburg,F.J.Nice drawings of graphs and trees are computationallyhard,Tech.report MIP-8820,Fakult fur Mathematik und Informatik,Univ.Passau (1988).[3] Chazelle,B.,Edelsbrunner,H.An optimal algorithm for intersecting linesegments in the plane,Journal of the ACM,39(1992) 1{54.[4] Cimikowski,R.,Shope,P.A neural network algorithm for a graph layoutproblem,IEEE Transactions on Neural Networks,7(1996),341{349.[5] Cormen,T.H.,Leiserson,C.E.Rivest,R.L.,Introduction to Algorithms.The MIT Press,1990.[6] Davidson,R.,Harel,D.Drawing graphs nicely using simulated annealing,ACM Transactions on Graphics,15(1996) 301{331.[7] Davis,L.A genetic algorithms tutorial.In L.Davis (ed.),Handbook ofGenetic Algorithms.Van Nostrand Reinhold,1991,1{101.[8] G.Di Battista,P.Eades,R.Tamassia and I.G.Tollis,Annotated bibli-ography on graph drawing algorithms,Computational Geometry.Theoryand Applications 4 (1994) 235{282.[9] M.R.Garey and D.S.Johnson,Crossing number is NP-complete,SIAMJournal of Algebraic Discrete Methods 4 (1983) 312{316.[10] L.Groves,Z.Michalewicz,P.Elia and C.Janikow,Genetic algorithmsfor drawing directed graphs,in:Proceedings of the Fifth InternationalSymposium on Methodologies for Intelligent Systems,(Elsevier North-Holland,1990) 268{276.[11] F.Harary,Graph Theory.(Addison-Wesley,1969).[12] Kosak,C.,Marks,J.,Shieber,S.A parallel genetic algorithm for network-diagram layout,in:Proceedings of the 4th Int.Conf.on Genetic Algo-rithms,1990.Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171TimGA:A Genetic Algorithm for Drawing Undirected Graphs 171[13] Makinen,E.,Sieranta,M.,Genetic algorithms for drawing bipartitegraphs,International Journal of Computer Mathematics,53(1994) 157{166.[14] Markus,A.Experiments with genetic algorithms for displaying graphs,in:Proceedings of the 1991 IEEE Workshop on Visual Languages,IEEEComputer Society Press,1991,62{67.[15] Michalewicz,Z.Genetic Algorithms + Data Structures = Evolution Pro-grams,Springer,1992.[16] Rosete-Suarez,A.,Ochoa-Rodrguez,A.,Sebag,M.Automatic graphdrawing and stochastic hill climbing,in:Proceedings of the Genetic andEvolutionary Computation Conference,vol.2,Morgan Kaufmann,1999,1699{1706.[17] Stol,J.,do Nascimento,H.A.D.,de Mendonca,C.F.X.,Heuristics andpedigrees for drawing directed graphs,Journal of the Brazilian ComputerSociety,6 (1999).Divulgaciones Matematicas Vol.9 No.2(2001),pp.155{171