Hierarchical Drawing of Directed Graphs

(RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) attempts to draw the vertices of a graph in a series of layers, placing dominant vertices at the top, and vertices lower in the hierarchy progressively further down.

Hierarchical graph drawing.

(RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) draws a graph so that the edges point predominantly downward. The second argument of (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) specifies the position of the root. Possible values for this argument are (RefLink[Right, paclet:ref/Right]), (RefLink[Left, paclet:ref/Left]), (RefLink[Top, paclet:ref/Top]), and (RefLink[Bottom, paclet:ref/Bottom]).

This plots a directed graph:

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

This is the same graph, with edges pointing from left to right:

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

(RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) may produce slightly different output on different platforms, due to floating-point differences.

Options for LayeredGraphPlot

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

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

DirectedEdges

The option (RefLink[DirectedEdges, paclet:ref/DirectedEdges]) specifies whether to draw edges as 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[True, paclet:ref/True]).

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

LayeredGraphPlot[{4->3,5->2,5->3,5->4,6->1,6->2,6->4},DirectedEdges-> False,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:

LayeredGraphPlot[{1->5,1->6,2->4,2->6,3->4,3->5,{3->6, "edge 3->6"},4->6,5->6},VertexLabeling-> True]

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

LayeredGraphPlot[{1->5,1->6,2->4,2->6,3->4,3->5,{3->6, "edge 3->6"},4->6,5->6},EdgeLabeling-> Automatic,VertexLabeling-> True]

Alternatively, use (RefLink[Tooltip, paclet:ref/Tooltip])[(Subscript[v, i])->(Subscript[v, j]),lbl] to specify a tooltip for an edge. Place the cursor over the edge between vertices 3 and 6, as well as the edge label on the edge between vertices 3 and 5, to see the tooltips:

LayeredGraphPlot[{1->5,1->6,2->4,2->6,3->4,{3->5,Tooltip[ "edge 3->5","3->5"]},Tooltip[3->6, "3->6"],4->6,5->6},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:

LayeredGraphPlot[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. It 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 of 0.3 (in the graph's coordinate system):

LayeredGraphPlot[{1->2,2->3,3->4,4->5,5->6,6->7,7->1,11->12,12->13,13->14,14->15,15->16,16->17,17->11,1->11,2->12,3->13,4->14,5->15,6->16,7->17},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 70% along arrows:

<<GraphUtilities`

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

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 the graph is 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:

LayeredGraphPlot[{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:

LayeredGraphPlot[(({{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:

LayeredGraphPlot[{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 [6], 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 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/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:

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

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

LayeredGraphPlot[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[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]).

PlotStyle

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

Draw edges with thicker arrows, and both edges and vertices' labels in red:

LayeredGraphPlot[{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,Arrowheads[{{0.1,0.8}}],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 of 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 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:

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

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

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

This shows self-loops with diameter equal to 0.3 times the average length of the edges:

LayeredGraphPlot[{3->2,4->1,4->3,5->1,5->2,6->1,6->2,6->3,6->4,6->5,1->1,1->1},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 currently supported by (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]).

This draws the Petersen graph using known coordinates:

LayeredGraphPlot[{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:

LayeredGraphPlot[{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])->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:

LayeredGraphPlot[(({{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:

LayeredGraphPlot[{"A"->"B","A"->"a","B"->"C","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:

LayeredGraphPlot[{"A"->"B","A"->"a","B"->"C","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={5->3,5->4,6->2,6->4,7->1,7->4,7->5,7->6};

LayeredGraphPlot[g]

This draws the same graph, but without the vertices:

LayeredGraphPlot[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:

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

Example Gallery

Flow Chart

(RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) helps visualize flow charts, for example for business, economic, or technical presentations.

This shows a flow chart:

LayeredGraphPlot[{"total"->"TotalDispatch","TotalList"->"CheckThreading","TotalList"->"TotalDispatch","TotalSparse"->"TotalDispatch","TotalSparse"->"TotalDispatch","TotalDispatch"->"TotalDispatch","TotalDispatch"->"TotalList","TotalDispatch"->"TotalPacked","TotalDispatch"->"TotalSparse"},VertexLabeling->True]

This shows a flow chart that flows from left to right:

LayeredGraphPlot[{"ratiotest"->"ratiolimit","getdata"->"dir_inf _bad","getdata"->"evalderiv","NSPpreliminaries"->"getdata","findsp"->"termlimit","findsp"->"ratiotest","findsp"->"nSumW","findsp"->"nSumEM","findsp"->"nProductEM","findsp"->"nProductW","nSumW"->"nSumFinite","nSumW"->"nSumWtail","nSumEM"->"evalterm","nSumEM"->"nSumFinite","nSumEM"->"evalderiv","nProductEM"->"nPFinite","nProductW"->"nPFinite","nProductW"->"nPWtail","oNSum"->"CleanOptionList","oNSum"->"multidim","oNSum"->"NSPpreliminaries","oNSum"->"findsp","nSumFinite"->"evalterm","nSumWtail"->"evalterm","nSumWtail"->"epsalg","oNProduct"->"CleanOptionList","oNProduct"->"multidim","oNProduct"->"NSPpreliminaries","oNProduct"->"findsp","nPFinite"->"evalterm","nPWtail"->"evalterm","nPWtail"->"epsalg","oSequenceLimit"->"epsalg"},Left,VertexLabeling->True,AspectRatio-> 1,PlotRangePadding->0.02]

Food Chains

Food chains can be visualized with (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]).

This shows a small food chain:

LayeredGraphPlot[{"John" -> "plants", 
  "lion" -> "John",  "tiger" -> "John", "tiger"->"deer","lion"->"deer","deer"->"plants",
  "mosquito" -> "lion", "frog" -> "mosquito","mosquito"->"tiger","John"->"cow","cow"->"plants","mosquito"->"deer","mosquito"->"John", "snake" -> "frog", "vulture" -> "snake"}, Left,
 VertexLabeling -> True]

This shows another food chain:

LayeredGraphPlot[ {"Raccoon"->"Bird","Raccoon"->"Insect","Wildcat"->"Bird","Wildcat"->"Rodent","Fox"->"Bird","Fox"->"Garter snake","Fox"->"Salamander","Fox"->"Rabbit","Fox"->"Rodent","Wolf"->"Rabbit","Wolf"->"Rodent","Wolf"->"Skunk","Wolf"->"Deer","Bear"->"Deer","Bear"->"Rodent","Bear"->"Plant","Bird"->"Plant","Garter snake"->"Insect","Garter snake"->"Toad","Salamander"->"Insect","Rabbit"->"Plant","Skunk"->"Rodent","Skunk"->"Insect","Deer"->"Plant","Toad"->"Insect","Insect"->"Plant"},VertexLabeling->True,PlotRangePadding->Automatic]

History of Unix

(RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) is suitable for visualizing historical events.

This shows a history of Unix:

LayeredGraphPlot[{"5th Edition" -> "6th Edition", "5th Edition" -> "PWB 1.0", 
  "6th Edition" -> "1 BSD", "6th Edition" -> "Interdata", 
  "6th Edition" -> "LSX", "6th Edition" -> "Mini Unix", 
  "6th Edition" -> "Wollongong", "PWB 1.0" -> "PWB 1.2", 
  "PWB 1.0" -> "USG 1.0", "1 BSD" -> "2 BSD", "Interdata" -> "PWB 2.0", 
  "Interdata" -> "Unix/TS 3.0", "Interdata" -> "7th Edition", 
  "PWB 1.2" -> "PWB 2.0", "USG 1.0" -> "USG 2.0", "USG 1.0" -> "CB Unix 1", 
  "7th Edition" -> "2 BSD", "7th Edition" -> "32V", "7th Edition" -> "Xenix", 
  "7th Edition" -> "Ultrix-11", "7th Edition" -> "UniPlus+", 
  "7th Edition" -> "V7M", "PWB 2.0" -> "Unix/TS 3.0", "USG 2.0" -> "USG 3.0", 
  "CB Unix 1" -> "CB Unix 2", "32V" -> "3 BSD", 
  "Unix/TS 1.0" -> "Unix/TS 3.0", "USG 3.0" -> "Unix/TS 3.0", 
  "CB Unix 2" -> "CB Unix 3", "3 BSD" -> "4 BSD", "V7M" -> "Ultrix-11", 
  "Unix/TS 3.0" -> "TS 4.0", "CB Unix 3" -> "Unix/TS++", 
  "CB Unix 3" -> "PDP-11 Sys V", "4 BSD" -> "4.1 BSD", 
  "Unix/TS++" -> "TS 4.0", "4.1 BSD" -> "8th Edition", "4.1 BSD" -> "4.2 BSD",
   "4.1 BSD" -> "2.8 BSD", "2 BSD" -> "2.8 BSD", "TS 4.0" -> "System V.0", 
  "4.2 BSD" -> "4.3 BSD", "4.2 BSD" -> "Ultrix-32", "2.8 BSD" -> "2.9 BSD", 
  "2.8 BSD" -> "Ultrix-11", "System V.0" -> "System V.2", 
  "8th Edition" -> "9th Edition", "System V.2" -> "System V.3"},VertexLabeling-> True,AspectRatio->0.7,PlotRangePadding->Automatic]