WOLFRAM LANGUAGE TUTORIAL

Tree Drawing

TreePlot lays out the vertices of a graph in a tree of successive layers, or a collection of trees. If the graph g is not a tree, TreePlot lays out its vertices on the basis of a spanning tree of each component of the graph.

TreePlot[{vi 1->vj 1,vi 2->vj 2,}]generate a tree plot of the graph in which vertex is connected to vertex
TreePlot[{{vi 1->vj 1,lbl1},}]associate labels with edges in the graph
TreePlot[g,pos]place roots of trees in the plot at position pos
TreePlot[g,pos,vk]use vertex as the root node in the tree plot
TreePlot[m]generate a layered plot of the graph represented by the adjacency matrix m

Tree drawing.

A simple graph and its tree plot.
In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

By default, TreePlot places each tree root at the top. TreePlot[g,pos] places the roots at position pos. Possible positions are: Top, Bottom, Left, Right, and Center.

This plots the tree by placing the root left.
In[3]:=
Click for copyable input
Out[3]=
This places the root at the center.
In[4]:=
Click for copyable input
Out[4]=

Options for TreePlot

In addition to options for Graphics, the following options are accepted for LayeredGraphPlot.

option name
default value
DataRangeAutomaticthe range of vertex coordinates to generate
DirectedEdgesFalsewhether to show edges as directed arrows
EdgeLabelingTruewhether to include labels given for edges
EdgeRenderingFunctionAutomaticfunction to give explicit graphics for edges
LayerSizeFunction1&the height to allow for each layer
MultiedgeStyleAutomatichow to draw multiple edges between vertices
PackingMethodAutomaticmethod to use for packing components
PlotRangePaddingAutomatichow much padding to put around the plot
PlotStyleAutomaticstyle in which objects are drawn
SelfLoopStyleAutomatichow to draw edges linking a vertex to itself
VertexCoordinateRulesAutomaticrules for explicit vertex coordinates
VertexLabelingAutomaticwhether to show vertex names as labels
VertexRenderingFunctionAutomaticfunction to give explicit graphics for vertices

Options for TreePlot.

DirectedEdges

The option DirectedEdges specifies whether to draw edges as directed arrows. Possible values for this option are True or False. The default value for this option is False.

This shows a graph with edges represented by arrows instead of lines.
In[5]:=
Click for copyable input
Out[5]=

EdgeLabeling

The option EdgeLabeling specifies whether and how to display labels given for the edges. Possible values for this option are True, False, or Automatic. The default value for this option is True, which displays the supplied edge labels on the graph. With EdgeLabeling->Automatic, the labels are shown as tooltips.

This displays the specified edge label.
In[6]:=
Click for copyable input
Out[6]=
This displays the edge label as a tooltip. Place the cursor over the edge between vertices 4 and 7 to see the tooltip.
In[7]:=
Click for copyable input
Out[7]=
This displays the labels as tooltips. Place the cursor over the edge between vertices 4 and 7 to see the tooltip.
In[8]:=
Click for copyable input
Out[8]=

EdgeRenderingFunction

The option EdgeRenderingFunction specifies graphical representation of the graph edges. Possible values for this option are Automatic, None, or a function that gives a proper combination of graphics primitives and directives. With the default setting of Automatic, a dark red line is drawn for each edge. With EdgeRenderingFunction->None, edges are not drawn.

This draws vertices only.
In[9]:=
Click for copyable input
Out[9]=

With EdgeRenderingFunction->g, each edge is rendered with the graphics primitives and directives given by the function g that can take three or more arguments, in the form , where are the coordinates of the beginning and ending points of the edge, are the beginning and ending vertices, and is any label specified for the edge or None. Explicit settings for EdgeRenderingFunction->g override settings for EdgeLabeling and DirectedEdges.

This plots edges as gray arrows with ends set back from vertices by a distance 0.3 (in the graph's coordinate system).
In[10]:=
Click for copyable input
Out[10]=
This displays edges and self-loops with black and red arrows, respectively. The function LineScaledCoordinate from the Graph Utilities Package adds text at 50% along arrows.
In[11]:=
Click for copyable input
In[12]:=
Click for copyable input
Out[12]=

LayerSizeFunction

The LayerSizeFunction option specifies the relative height to allow for each layer. By default the height is 1. Possible values include a function that gives real machine numbers.

This defines and plots a tree.
In[13]:=
Click for copyable input
In[14]:=
Click for copyable input
Out[14]=
This plots the same tree, with the first layer a relative height of 1, the second 2, and the third 3.
In[15]:=
Click for copyable input
Out[15]=

MultiedgeStyle

The option MultiedgeStyle specifies whether to draw multiple edges between two vertices. Possible values for MultiedgeStyle are Automatic (the default), True, False, or a positive real number. With the default setting MultiedgeStyle->Automatic, multiple edges are shown for a graph specified by a list of rules, but not shown if specified by an adjacency matrix. With MultiedgeStyle->δ, the multiedges are spread out to a scaled distance of δ.

By default, multiple edges are shown if a graph is given as a list of rules.
In[16]:=
Click for copyable input
Out[16]=
But multiple edges are not shown for graphs specified by an adjacency matrix.
In[17]:=
Click for copyable input
Out[17]=
This spreads multiple edges by the specified amount.
In[18]:=
Click for copyable input
Out[18]=

PackingMethod

The option PackingMethod specifies the method used for packing disconnected components. Possible values for the option are Automatic (the default), , , , , , and . With PackingMethod->"ClosestPacking", components are packed as close together as possible using a polyomino method, starting from the top left. With PackingMethod->"ClosestPackingCenter", components are packed starting from the center. With PackingMethod->"Layered", components are packed in layers starting from the top left. With PackingMethod->"LayeredLeft" or PackingMethod->"LayeredTop", components are packed in layers starting from the top or left respectively. With PackingMethod->"NestedGrid", components are arranged in a nested grid. The typical effective default setting is PackingMethod->"Layered", and the packing starts with components of the largest bounding box area.

This shows the packing of disconnected components by the default method.
In[19]:=
Click for copyable input
Out[19]=
This shows the packing of disconnected components using the method.
In[20]:=
Click for copyable input
Out[20]=

PlotRangePadding

PlotRangePadding is a common option for graphics functions inherited by TreePlot.

PlotStyle

PlotStyle is a common option for graphics functions inherited by TreePlot. The option PlotStyle specifies the style in which objects are drawn.

Draw edges with thicker lines, and draw both edges and vertex labels in red.
In[21]:=
Click for copyable input
Out[21]=

SelfLoopStyle

The option SelfLoopStyle specifies whether and how to draw loops for vertices that are linked to themselves. Possible values for the option are Automatic (the default), True, False, or a positive real number. With SelfLoopStyle->Automatic, self-loops are shown if the graph is specified by a list of rules, but not if it is specified by an adjacency matrix. With SelfLoopStyle->δ, the self-loops are drawn with a diameter of δ (relative to the average edge length).

By default, self-loops are displayed for a graph specified by a list of rules.
In[22]:=
Click for copyable input
Out[22]=
Self-loops are not shown if the graph is specified by an adjacency matrix.
In[23]:=
Click for copyable input
Out[23]=
This shows self-loops whose diameters equal 0.3 times the average length of the edges.
In[24]:=
Click for copyable input
Out[24]=

VertexCoordinateRules

The option VertexCoordinateRules specifies the coordinates of the vertices. Possible values are None or a list of coordinates. Coordinates specified by a list of rules are not supported by TreePlot currently.

This draws the Petersen graph using known coordinates.
In[25]:=
Click for copyable input
Out[25]=
This draws with the default method.
In[26]:=
Click for copyable input
Out[26]=

VertexLabeling

The option VertexLabeling specifies whether to show vertex names as labels. Possible values for this option are True, False, Automatic (the default) and Tooltip. VertexLabeling->True shows the labels. For graphs specified by an adjacency matrix, vertex labels are taken to be successive integers , where is the size of the matrix. For graphs specified by a list of rules, labels are the expressions used in the rules. VertexLabeling->False displays each vertex as a point. VertexLabeling->Tooltip displays each vertex as a point, but gives its name in a tooltip. VertexLabeling->Automatic displays each vertex as a point, giving its name in a tooltip if the number of vertices is not too large. You can also use Tooltip[vk,vlbl] anywhere in the list of rules to specify an alternative tooltip for a vertex .

This draws the graph with labels given as indices of the adjacency matrix.
In[27]:=
Click for copyable input
Out[27]=
This uses the labels specified in the list of rules.
In[28]:=
Click for copyable input
Out[28]=
This plots vertices as points, and displays vertex names in tooltips. Place the cursor above the vertices to see the labels.
In[29]:=
Click for copyable input
Out[29]=

VertexRenderingFunction

The option VertexRenderingFunction specifies graphical representation of the graph edges. Possible values for this option are Automatic, None, or a function that gives a proper combination of graphics primitives and directives. With the default setting of Automatic, vertices are displayed as points, with their names given in tooltips.

By default, vertices are displayed as points and, for small graphs, labeled in tooltips. Point the cursor at a vertex to see the tooltip.
In[30]:=
Click for copyable input
In[31]:=
Click for copyable input
Out[31]=
This draws the same graph, but without the vertices.
In[32]:=
Click for copyable input
Out[32]=

With VertexRenderingFunction->g, each vertex is rendered with the graphics primitives given by , where is the coordinate of the vertex and is the label of the vertex. Explicit settings for VertexRenderingFunction->g override settings for VertexLabeling.

This shows vertices as yellow disks.
In[33]:=
Click for copyable input
Out[33]=

Example Gallery

k-ary tree

This plots a 5-ary tree of 4 levels.
In[14]:=
Click for copyable input
Out[14]=
This plots the same graph, but with the height of each level proportional to .
In[15]:=
Click for copyable input
Out[15]=
This sets the height of each level proportional to .
In[16]:=
Click for copyable input
Out[16]=