Tree Drawing

(RefLink[TreePlot, paclet:ref/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, (RefLink[TreePlot, paclet:ref/TreePlot]) lays out its vertices on the basis of a spanning tree of each component of the graph.

Tree drawing.

A simple graph and its tree plot:

g={1->2,2->4,3->6,4->8,5->10,6->12,1->3,2->5,3->7,4->9,5->11,6->13};

TreePlot[g,VertexLabeling->True]

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

This plots the tree by placing the root left:

TreePlot[g,Left,VertexLabeling->True]

This places the root at the center:

TreePlot[g,Center,VertexLabeling->True]

Options for TreePlot

In addition to options for (RefLink[Graphics, paclet:ref/Graphics]), the following options are accepted for (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]).

Options for (RefLink[TreePlot, paclet:ref/TreePlot]).

DirectedEdges

The option (RefLink[DirectedEdges, paclet:ref/DirectedEdges]) specifies whether to draw edges as directed arrows. Possible values for this option are (RefLink[True, paclet:ref/True]) or (RefLink[False, paclet:ref/False]). The default value for this option is (RefLink[False, paclet:ref/False]).

This shows a graph with edges represented by arrows instead of lines:

TreePlot[{1->8,2->4,3->7,4->10,5->7,5->8,6->10,7->10,8->9},DirectedEdges-> True,VertexLabeling -> True]

EdgeLabeling

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

This displays the specified edge label:

TreePlot[{1->7,2->5,2->6,3->8,4->5,{4->7,"edge {4,7}"},7->8,7->10,9->10},VertexLabeling-> True]

This displays the edge label as a tooltip. Place the cursor over the edge between vertices 4 and 7 to see the tooltip:

TreePlot[{1->7,2->5,2->6,3->8,4->5,{4->7,"edge {4,7}"},7->8,7->10,9->10},EdgeLabeling-> Automatic,VertexLabeling-> True]

This displays the labels as tooltips. Place the cursor over the edge between vertices 4 and 7 to see the tooltip:

TreePlot[{1->7,2->5,2->6,3->8,4->5,Tooltip[4->7,"edge {4,7}"],7->8,7->10,9->10},VertexLabeling-> True]

EdgeRenderingFunction

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

This draws vertices only:

TreePlot[Table[1,{10},{10}],EdgeRenderingFunction->None,VertexLabeling->True]

With (RefLink[EdgeRenderingFunction, paclet:ref/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 g[{(Subscript[r, i]),…,(Subscript[r, j])},{(Subscript[v, i]),(Subscript[v, j])},(Subscript[lbl, ij]),…], where (Subscript[r, i]),(Subscript[r, j]) are the coordinates of the beginning and ending points of the edge, (Subscript[v, i]),(Subscript[v, j]) are the beginning and ending vertices, and (Subscript[lbl, ij]) is any label specified for the edge or (RefLink[None, paclet:ref/None]). Explicit settings for (RefLink[EdgeRenderingFunction, paclet:ref/EdgeRenderingFunction])->g override settings for (RefLink[EdgeLabeling, paclet:ref/EdgeLabeling]) and (RefLink[DirectedEdges, paclet:ref/DirectedEdges]).

This plots edges as gray arrows with ends set back from vertices by a distance 0.3 (in the graph's coordinate system):

TreePlot[{1->4,1->5,2->4,3->6,3->9,4->8,4->10,6->7,8->9},EdgeRenderingFunction->({GrayLevel[0.5], Arrow[#1,0.3]}&)]

This displays edges and self-loops with black and red arrows, respectively. The function (PackageLink[LineScaledCoordinate, paclet:GraphUtilities/ref/LineScaledCoordinate, GraphUtilities Package Symbol]) from the Graph Utilities Package adds text at 50% along arrows:

<<GraphUtilities`

TreePlot[{1->4,1->1,1->5,2->4,3->6,3->9,4->8,4->10,6->7,8->9},EdgeRenderingFunction->({If[First[#2]===Last[#2],Red,Black],Arrow[#1,.1],Text[#2,LineScaledCoordinate[#1,.5],Background->White]}&),VertexLabeling->True]

LayerSizeFunction

The (RefLink[LayerSizeFunction, paclet:ref/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:

g={1->4,2->6,2->7,2->8,3->8,4->5,5->6};

TreePlot[g]

This plots the same tree, with the first layer a relative height of 1, the second 2, and the third 3:

TreePlot[g,LayerSizeFunction-> (#&)]

MultiedgeStyle

The option (RefLink[MultiedgeStyle, paclet:ref/MultiedgeStyle]) specifies whether to draw multiple edges between two vertices. Possible values for (RefLink[MultiedgeStyle, paclet:ref/MultiedgeStyle]) are (RefLink[Automatic, paclet:ref/Automatic]) (the default), (RefLink[True, paclet:ref/True]), (RefLink[False, paclet:ref/False]), or a positive real number. With the default setting (RefLink[MultiedgeStyle, paclet:ref/MultiedgeStyle])->(RefLink[Automatic, paclet:ref/Automatic]), multiple edges are shown for a graph specified by a list of rules, but not shown if specified by an adjacency matrix. With (RefLink[MultiedgeStyle, paclet:ref/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:

TreePlot[{1->2,2->1,1->2,1->2,2->3,3->5,4->5,5-> 6,5->1},VertexLabeling->True]

But multiple edges are not shown for graphs specified by an adjacency matrix:

TreePlot[(({{0, 3, 0, 0, 0, 0}, {1, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0}, {1, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0}})),VertexLabeling->True]

This spreads multiple edges by the specified amount:

TreePlot[{1->2,2->1,1->2,1->2,2->3,3->5,4->5,5-> 6,5->1},MultiedgeStyle->0.25,VertexLabeling->True]

PackingMethod

The option (RefLink[PackingMethod, paclet:ref/PackingMethod]) specifies the method used for packing disconnected components. Possible values for the option are (RefLink[Automatic, paclet:ref/Automatic]) (the default), "ClosestPacking", "ClosestPackingCenter", "Layered", "LayeredLeft", "LayeredTop", and "NestedGrid". With (RefLink[PackingMethod, paclet:ref/PackingMethod])->"ClosestPacking", components are packed as close together as possible using a polyomino method, starting from the top left. With (RefLink[PackingMethod, paclet:ref/PackingMethod])->"ClosestPackingCenter", components are packed starting from the center. With (RefLink[PackingMethod, paclet:ref/PackingMethod])->"Layered", components are packed in layers starting from the top left. With (RefLink[PackingMethod, paclet:ref/PackingMethod])->"LayeredLeft" or (RefLink[PackingMethod, paclet:ref/PackingMethod])->"LayeredTop", components are packed in layers starting from the top or left respectively. With (RefLink[PackingMethod, paclet:ref/PackingMethod])->"NestedGrid", components are arranged in a nested grid. The typical effective default setting is (RefLink[PackingMethod, paclet:ref/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:

TreePlot[Table[i->Mod[i^3,221],{i,0,221}]]

This shows the packing of disconnected components using the "ClosestPackingCenter" method:

TreePlot[Table[i->Mod[i^3,221],{i,0,221}],PackingMethod->"ClosestPackingCenter"]

PlotRangePadding

(RefLink[PlotRangePadding, paclet:ref/PlotRangePadding]) is a common option for graphics functions inherited by (RefLink[TreePlot, paclet:ref/TreePlot]).

PlotStyle

(RefLink[PlotStyle, paclet:ref/PlotStyle]) is a common option for graphics functions inherited by (RefLink[TreePlot, paclet:ref/TreePlot]). The option (RefLink[PlotStyle, paclet:ref/PlotStyle]) specifies the style in which objects are drawn.

Draw edges with thicker lines, and draw both edges and vertex labels in red:

TreePlot[{1->2,2->3,3->4,4->1,6->5,7->5,5->4,9->8,10->8,8->3,12->11,13->11,11->1,15->14,16->14,14->2},VertexLabeling->True,PlotStyle->{Red,Thickness[0.02]}]

SelfLoopStyle

The option (RefLink[SelfLoopStyle, paclet:ref/SelfLoopStyle]) specifies whether and how to draw loops for vertices that are linked to themselves. Possible values for the option are (RefLink[Automatic, paclet:ref/Automatic]) (the default), (RefLink[True, paclet:ref/True]), (RefLink[False, paclet:ref/False]), or a positive real number. With (RefLink[SelfLoopStyle, paclet:ref/SelfLoopStyle])->(RefLink[Automatic, paclet:ref/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 (RefLink[SelfLoopStyle, paclet:ref/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:

TreePlot[{1->4,1->5,2->3,2->5,2->6,2->2,2->2},VertexLabeling->True]

Self-loops are not shown if the graph is specified by an adjacency matrix:

TreePlot[(({{0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 1, 2, 1, 1}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}})),VertexLabeling->True]

This shows self-loops whose diameters equal 0.3 times the average length of the edges:

TreePlot[{1->4,1->5,2->3,2->5,2->6,2->2,2->2},VertexLabeling->True,SelfLoopStyle->0.3]

VertexCoordinateRules

The option (RefLink[VertexCoordinateRules, paclet:ref/VertexCoordinateRules]) specifies the coordinates of the vertices. Possible values are (RefLink[None, paclet:ref/None]) or a list of coordinates. Coordinates specified by a list of rules are not supported by (RefLink[TreePlot, paclet:ref/TreePlot]) currently.

This draws the Petersen graph using known coordinates:

TreePlot[{1->3,1->4,2->4,2->5,3->5,6->7,7->8,8->9,9->10,6->10,1->6,2->7,3->8,4->9,5->10},VertexCoordinateRules->{{0.30901699437494745`,0.9510565162951535`},{-0.8090169943749476`,-0.587785252292473`},{0.30901699437494723`,-0.9510565162951536`},{-0.8090169943749473`,0.5877852522924732`},{1.`,0},{0.6180339887498949`,1.902113032590307`},{-1.6180339887498947`,1.1755705045849465`},{-1.6180339887498951`,-1.175570504584946`},{0.6180339887498945`,-1.9021130325903073`},{2.`,0}}]

This draws with the default method:

TreePlot[{1->3,1->4,2->4,2->5,3->5,6->7,7->8,8->9,9->10,6->10,1->6,2->7,3->8,4->9,5->10}]

VertexLabeling

The option (RefLink[VertexLabeling, paclet:ref/VertexLabeling]) specifies whether to show vertex names as labels. Possible values for this option are (RefLink[True, paclet:ref/True]), (RefLink[False, paclet:ref/False]), (RefLink[Automatic, paclet:ref/Automatic]) (the default) and (RefLink[Tooltip, paclet:ref/Tooltip]). (RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[True, paclet:ref/True]) shows the labels. For graphs specified by an adjacency matrix, vertex labels are taken to be successive integers 1,2,…,n, where n is the size of the matrix. For graphs specified by a list of rules, labels are the expressions used in the rules. (RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[False, paclet:ref/False]) displays each vertex as a point. (RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[Tooltip, paclet:ref/Tooltip]) displays each vertex as a point, but gives its name in a tooltip. (RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[Automatic, paclet:ref/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 (RefLink[Tooltip, paclet:ref/Tooltip])[(Subscript[v, k]),(Subscript[v, lbl])] anywhere in the list of rules to specify an alternative tooltip for a vertex (Subscript[v, k]).

This draws the graph with labels given as indices of the adjacency matrix:

TreePlot[(({{0, 1, 0, 0, 0}, {0, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}})),VertexLabeling->True]

This uses the labels specified in the list of rules:

TreePlot[{"A"->"B","A"->"a","C"->"A"},VertexLabeling->True]

This plots vertices as points, and displays vertex names in tooltips. Place the cursor above the vertices to see the labels:

TreePlot[{"A"->"B","A"->"a","C"->"A"},VertexLabeling->Tooltip]

VertexRenderingFunction

The option (RefLink[VertexRenderingFunction, paclet:ref/VertexRenderingFunction]) specifies graphical representation of the graph edges. Possible values for this option are (RefLink[Automatic, paclet:ref/Automatic]), (RefLink[None, paclet:ref/None]), or a function that gives a proper combination of graphics primitives and directives. With the default setting of (RefLink[Automatic, paclet:ref/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:

g={1->3,1->4,2->3,2->5,2->6,5->7};

TreePlot[g]

This draws the same graph, but without the vertices:

TreePlot[g,VertexRenderingFunction->None]

With (RefLink[VertexRenderingFunction, paclet:ref/VertexRenderingFunction])->g, each vertex is rendered with the graphics primitives given by g[(Subscript[r, i]),(Subscript[v, i]),…], where (Subscript[r, i]) is the coordinate of the vertex and (Subscript[v, i]) is the label of the vertex. Explicit settings for (RefLink[VertexRenderingFunction, paclet:ref/VertexRenderingFunction])->g override settings for (RefLink[VertexLabeling, paclet:ref/VertexLabeling]).

This shows vertices as yellow disks:

TreePlot[{1->4,1->5,2->8,3->4,4->8,6->8,7->8,7->9},VertexRenderingFunction->({EdgeForm[Black],Yellow,Disk[#1,0.2],Black,Text[#2,#1]}&)]

Example Gallery

k-ary tree

This plots a 5-ary tree of 4 levels:

TreePlot[CompleteKaryTree[5,5],Center]

This plots the same graph, but with the height of each level i proportional to 1/(i^2):

TreePlot[CompleteKaryTree[5,5],Center,LayerSizeFunction-> (1/#^2&)]

This sets the height of each level i proportional to (((-0.5))^i):

TreePlot[CompleteKaryTree[5,5],Center,LayerSizeFunction-> ((-0.5)^#&)]