Drawing graphs with dot
Emden R.Gansner and Eleftherios Koutsofios and Stephen North
December22,2009
Abstract
dot draws directed graphs as hierarchies.It runs as a command line pro-gram,web visualization service,or with a compatible graphical interface. Its features include well-tuned layout algorithms for placing nodes and edge splines,edge labels,“record”shapes with“ports”for drawing data struc-tures;cluster layouts;and an underlyingfile language for stream-oriented graph tools.Below is a reduced module dependency graph of an SML-NJ compiler that took0.23seconds of user time on a3GHz Intel Xeon.
1
1Basic Graph Drawing
dot draws directed graphs.It reads attributed graph textfiles and writes drawings, either as graphfiles or in a graphics format such as GIF,PNG,SVG,PDF,or PostScript.
dot draws graphs in four main phases.Knowing this helps you to understand what kind of layouts dot makes and how you can control them.The layout proce-dure used by dot relies on the graph being acyclic.Thus,thefirst step is to break any cycles which occur in the input graph by reversing the internal direction of certain cyclic edges.The next step assigns nodes to discrete ranks or levels.In a top-to-bottom drawing,ranks determine Y coordinates.Edges that span more than one rank are broken into chains of“virtual”nodes and unit-length edges.The third step orders nodes within ranks to avoid crossings.The fourth step sets X coordi-nates of nodes to keep edges short,and thefinal step routes edge splines.This is the same general approach as most hierarchical graph drawing programs,based on the work of Warfield[War77],Carpano[Car80]and Sugiyama[STT81].We refer the reader to[GKNV93]for a thorough explanation of dot’s algorithms.
dot accepts input in the DOT language(cf.Appendix D).This language de-scribes three main kinds of objects:graphs,nodes,and edges.The main(outer-most)graph can be directed(digraph)or undirected graph.Because dot makes layouts of directed graphs,all the following examples use digraph.(A separate layout utility,neato,draws undirected graphs[Nor92].)Within a main graph,a subgraph defines a subset of nodes and edges.
Figure1is an example graph in the DOT language.Line1gives the graph name and type.The lines that f
ollow create nodes,edges,or subgraphs,and set attributes.Names of all these objects may be C identifiers,numbers,or quoted C strings.Quotes protect punctuation and white space.
A node is created when its namefirst appears in thefile.An edge is created when nodes are joined by the edge operator->.In the example,line2makes edges from main to parse,and from parse to execute.Running dot on thisfile(call it graph1.dot)
sortedlist$dot-Tps graph1.dot-o graph1.ps
yields the drawing of Figure2.The command line option-Tps selects PostScript (aph1.ps may be printed,displayed by a PostScript viewer,or embedded in another document.
It is often useful to adjust the representation or placement of nodes and edges in the layout.This is done by setting attributes of nodes,edges,or subgraphs in the inputfile.Attributes are name-value pairs of character strings.Figures3and4 illustrate some layout attributes.In the listing of Figure3,line2sets the graph’s
1:digraph G{
2:main->parse->execute;
3:main->init;
4:main->cleanup;
5:execute->make_string;
6:execute->printf
7:init->make_string;
8:main->printf;
9:execute->compare;
10:}
Figure1:Small graph
Figure2:Drawing of small graph
size to4,4(in inches).This attribute controls the size of the drawing;if the drawing is too large,it is scaled uniformly as necessary tofit.
Node or edge attributes are set off in square brackets.In line3,the node main is assigned shape box.The edge in line4is straightened by increasing its weight (the default is1).The edge in line6is drawn as a dotted line.Line8makes edges from execute to make string and printf.In line10the default edge color is set to red.This affects any edges created after this point in thefile.Line11 makes a bold ed
ge labeled100times.In line12,node make_string is given a multi-line label.Line13changes the default node to be a boxfilled with a shade of blue.The node compare inherits these values.
2Drawing Attributes
The main attributes that affect graph drawing are summarized in Appendices A,B and C.For more attributes and a more complete description of the attributes,you should refer to the Graphviz web site,specifically
2.1Node Shapes
Nodes are drawn,by default,with shape=ellipse,width=.75,height=.5 and labeled by the node name.Other common shapes include box,circle, record and plaintext.A list of the main node shapes is given in Appendix H. The node shape plaintext is of particularly interest in that it draws a node with-out any outline,an important convention in some kinds of diagrams.In cases where the graph structure is of main concern,and especially when the graph is moderately large,the point shape reduces nodes to display minimal content.When drawn,a node’s actual size is the greater of the requested size and the a
rea needed for its text label,unless fixedsize=true,in which case the width and height values are enforced.
Node shapes fall into two broad categories:polygon-based and record-based.1 All node shapes except record and Mrecord are considered polygonal,and are modeled by the number of sides(ellipses and circles being special cases),and a few other geometric properties.Some of these properties can be specified in a graph.If regular=true,the node is forced to be regular.The parameter
1There is a way to implement custom node shapes,using shape=epsf and the shapefile attribute,and relying on PostScript output.The details are beyond the scope of this user’s guide. Please contact the authors for further information.
1:digraph G{
2:size="4,4";
3:main[shape=box];/*this is a comment*/
4:main->parse[weight=8];
5:parse->execute;
6:main->init[style=dotted];
7:main->cleanup;
8:execute->{make_string;printf}
9:init->make_string;
10:edge[color=red];//so is this
11:main->printf[style=bold,label="100times"]; 12:make_string[label="make a\nstring"];
13:node[shape=box,style=filled,color=".7.3 1.0"]; 14:execute->compare;
15:}
Figure3:Fancy graph
Figure4:Drawing of fancy graph
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论