Function Repository Resource:

DistanceLayeredGraph

Source Notebook

Render a graph with vertices at heights based on their distance from the root

Contributed by: Stephen Wolfram and Jan Mangaldan

ResourceFunction["DistanceLayeredGraph"][g]

renders the graph g so that nodes that are a distance d from node 1 are at position d below the root.

ResourceFunction["DistanceLayeredGraph"][g,root]

uses the specified node root as the root.

Details and Options

ResourceFunction["DistanceLayeredGraph"] works with both directed and undirected graphs.

Examples

Basic Examples (1) 

Organize nodes in a graph based on distance from node 1:

In[1]:=
ResourceFunction["DistanceLayeredGraph"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5, 6, 7}, {{{1, 2}, {1, 2}, {1, 2}, {1, 3}, {1, 3}, {2, 4}, {2, 4}, {2, 3}, {2, 5}, {2, 5}, {3, 4}, {3, 6}, {3, 5}, {3, 7}, {
         3, 7}, {4, 6}, {4, 6}, {5, 7}}, Null}]]}, 
TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData["
1:eJxtlgs4VOkfx2fcZtyG3Nqykm6UtGqpZTe/d4tQKlFJV9luSyWxUnJNCmtV
aovE312XLVRbLvUi1wqhoqQVch1jZhh35s90zvTsefZ95nnm+c5555z3837m
fOfoOLvZ7Zeg0WjL6TTa1Pu/hzL6d2YSmQvEB2TGlM9p/87kcfFA/z1PfH78
3/PE14N/v3/9XLlsu65KTyKUB78qvK+rhNCJnNK6vWnwyuKY2x9cFkJ/ze2O
1b8N7IHvFOMes5BTUKnzXKu7sK6tNUcygIXKjELKy7dnQOAmibGmNSy07cK+
YjOTTPh4syDZQZGFMrZHcthPM+GitGZJaK0i4m56POtsdyZEi8bXfPDA1JAT
z3cbaTXxWiwnPt/crWY70vmy4uv53x2X1M6WRbbEev6eoWkx5i+LDIn1qsu+
4u+1nMwEj2FO5AV3JVlEI3hX+Hy30qaeiQh+TOHHFH5M4ccUfkzhxxR+TOHH
FH5M4ccUfkzhxxR+TOHHFH5M4cck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uCN0DF4sVLN
wiB18r7cfT1Y8YdRMKs9/DzxUR88PrTunVv1MJyuPrD0Jr8fbDgvBEVbhuD8
DxKnkMMA5HO+fa9QPABOTEg+9HkQ1F3MQiy1BSDzs1aGzo1hsHYyHPHc1wcP
ktfF1wePAPNn5tDYYT6crJ1Dq7cdheHN6trfuPBgc0VijTZ9DDRjHdxKdkze
H5deaGRcGYO6xXHVSsEcWDXnxsvzrHFoPDeokW3CBkff6f03XcfBwjr37vn6
TjibZBqh+tc4KLh3XvNzaoeSyzLXimvG4Qdh4JuY6lbQ3OSjlv9hHIr6gvc1
6jdD0abMXFbTOLw0Mrt3T/MfkFIuWPOENw5pviMBKxa/h/rwWw9fTP5uZxSa
mdmFvgFUfIRpiiZAS0K+XbisGlbmyQHTdwLcfZ4OBNg9h6pjgY7LyybgTafX
gG59IQiaqx2KdIRQFOcX09nzCDJUxkyyQoVwLGM3+0ZuKvjbhzUuqebguaqr
5Kvi2PD7tuAzzaEcDNMLnpgUdsPi4vVL16zn4L8asJF3cxe8P1db7qvJwYui
o7pZtC44aFJVktrfg6XWyHLPaHaC4Ii9bn19D/ZKk4nabdwBt9SWOM8p68Hm
Goe/0d3QDoUCvZ2RhT14n6jP2sRZ9Pfzfa14vketblXmL9Xi8+k4f75/dlGV
+Hod7e+Be/EFNBDrsb2eualIWAoGxHrNL9IMtL2KIILgmUgcZc4YzIcAgvft
gd6Uc7K5gN6aXjx0lSvmz6ld+ZSVyxXzu2g5tu98xxXzq04+rf7M54r5bX65
FntHmifml7Heyo5S5Yn5bws6KjmaPDG/9tmpf1iemJ/MJD85n+Qnz0fyk9cj
+VWI9ZD85HpJfpKH5Cd5Sf7/Ax3tR7Q=
"], {
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], ArrowBox[
            BezierCurveBox[{1, {0.16444074718311652`, 3.4999999999999987`}, 2}], 0.03700564971751412], ArrowBox[{1, 2}, 0.03700564971751412], ArrowBox[
            BezierCurveBox[{1, {-0.16444074718311652`, 3.4999999999999987`}, 2}], 0.03700564971751412], ArrowBox[BezierCurveBox[{1, 45, 46, 47, 48, 49, 50, 51, 52,
             53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 3}], 0.03700564971751412], ArrowBox[BezierCurveBox[{1, 71, 72, 73, 74, 75, 76, 77, 78,
             79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 3}], 0.03700564971751412], ArrowBox[{2, 3}, 0.03700564971751412], ArrowBox[BezierCurveBox[{2, 97, 98, 99, 100, 101, 102, 103,
             104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 4}], 0.03700564971751412], ArrowBox[BezierCurveBox[{2, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 4}], 0.03700564971751412], ArrowBox[BezierCurveBox[{2, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 5}], 0.03700564971751412], ArrowBox[BezierCurveBox[{2, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 5}], 0.03700564971751412], ArrowBox[{3, 4}, 0.03700564971751412], ArrowBox[{3, 5}, 0.03700564971751412], ArrowBox[BezierCurveBox[{3, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 6}], 0.03700564971751412], ArrowBox[BezierCurveBox[{3, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 7}], 0.03700564971751412], ArrowBox[BezierCurveBox[{3, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 7}], 0.03700564971751412], ArrowBox[
            BezierCurveBox[{4, {-0.8355592528168823, 0.5000000000000041}, 6}], 0.03700564971751412], ArrowBox[
            BezierCurveBox[{4, {-1.1644407471831195`, 0.5000000000000049}, 6}], 0.03700564971751412], ArrowBox[{5, 7}, 0.03700564971751412]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], DiskBox[1, 0.03700564971751412], DiskBox[2, 0.03700564971751412], DiskBox[3, 0.03700564971751412], DiskBox[4, 0.03700564971751412], DiskBox[5, 0.03700564971751412], DiskBox[6, 0.03700564971751412], DiskBox[7, 0.03700564971751412]}}]],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
ExpressionUUID -> "52e5d8ec-d65d-410f-bf93-3a4da6e5599a",
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{134.6171614141531, Automatic}]\)]
Out[1]=

Applications (1) 

Create a layered rendering of a 2D grid graph:

In[2]:=
ResourceFunction["DistanceLayeredGraph"][GridGraph[{5, 5}]]
Out[2]=

Properties and Relations (1) 

In a complete graph, all nodes are distance one from node 1:

In[3]:=
ResourceFunction["DistanceLayeredGraph"][CompleteGraph[5]]
Out[3]=

Version History

  • 2.0.0 – 16 March 2020
  • 1.0.0 – 04 December 2019

License Information