GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — NestGraph—Wolfram Documentation

NestGraph[f,expr,n]

gives the graph obtained by starting with expr and applying f successively n times.

NestGraph[f,{expr1,expr2,…},n]

gives the graph obtained by applying f to expr1, expr2, ….

NestGraph[f,graph,n]

gives the graph obtained by applying f to the vertices of graph and extending the graph.

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Options  
AnnotationRules  
DirectedEdges  
EdgeLabels  
Show More Show More
EdgeShapeFunction  
EdgeStyle  
EdgeWeight  
GraphHighlight  
GraphHighlightStyle  
GraphLayout  
PlotTheme  
Base Themes  
Feature Themes  
VertexCoordinates  
VertexLabels  
VertexShape  
VertexShapeFunction  
VertexSize  
VertexStyle  
VertexWeight  
Applications  
Properties & Relations  
Neat Examples  
See Also
Related Guides
Related Links
History
Cite this Page

NestGraph

NestGraph[f,expr,n]

gives the graph obtained by starting with expr and applying f successively n times.

NestGraph[f,{expr1,expr2,…},n]

gives the graph obtained by applying f to expr1, expr2, ….

NestGraph[f,graph,n]

gives the graph obtained by applying f to the vertices of graph and extending the graph.

Details and Options

  • NestGraph is also known as crawl graph or tree growing.
  • expr can be an expression or a list of expressions or graphs.
  • NestGraph[f,expr] is equivalent to NestGraph[f,expr,1].
  • NestGraph[f,expr] gives a graph with edges {exprexpr1,…,exprexprk}, where f[expr] evaluates to {expr1,…,exprk}.
  • NestGraph[f,{expr1,expr2,…}] is the graph union of NestGraph[f,expr1] and NestGraph[f,expr2],….
  • NestGraph[f,graph] is the union of graph and NestGraph[f,{v1,…}], where vi are the vertices of graph.
  • NestGraph[f,graph,n] is the union of NestGraph[f,graph,n-1] and NestGraph[f,{v1,…}], where vi are the vertices of NestGraph[f,graph,n-1].
  • NestGraph[f,expr,n] is equivalent to NestGraph[f,graph,n], where graph is the singleton graph with only one vertex expr and no edges.
  • NestGraph takes the same options as Graph.
  • The option DirectedEdges can be used to control whether an undirected or directed graph is constructed.
  • List of all options

    • AlignmentPointCenterthe default point in the graphic to align with
      AnnotationRules {}annotations for graph, edges and vertices
      AspectRatioAutomaticratio of height to width
      AxesFalsewhether to draw axes
      AxesLabelNoneaxes labels
      AxesOriginAutomaticwhere axes should cross
      AxesStyle{}style specifications for the axes
      BackgroundNonebackground color for the plot
      BaselinePositionAutomatichow to align with a surrounding text baseline
      BaseStyle{}base style specifications for the graphic
      ContentSelectableAutomaticwhether to allow contents to be selected
      CoordinatesToolOptionsAutomaticdetailed behavior of the coordinates tool
      DirectedEdges Automaticwhether to interpret Rule as DirectedEdge
      EdgeLabels Nonelabels and label placements for edges
      EdgeLabelStyleAutomaticstyle to use for edge labels
      EdgeShapeFunction Automaticgenerate graphic shapes for edges
      EdgeStyle Automaticstyle used for edges
      EdgeWeight Automaticweights for edges
      Epilog{}primitives rendered after the main plot
      FormatTypeTraditionalFormthe default format type for text
      FrameFalsewhether to put a frame around the plot
      FrameLabelNoneframe labels
      FrameStyle{}style specifications for the frame
      FrameTicksAutomaticframe ticks
      FrameTicksStyle{}style specifications for frame ticks
      GraphHighlight {}graph elements to highlight
      GraphHighlightStyle Automaticstyle for highlight
      GraphLayout Automatichow to lay out vertices and edges
      GridLinesNonegrid lines to draw
      GridLinesStyle{}style specifications for grid lines
      ImageMargins0.the margins to leave around the graphic
      ImagePaddingAllwhat extra padding to allow for labels etc.
      ImageSizeAutomaticthe absolute size at which to render the graphic
      LabelStyle{}style specifications for labels
      MethodAutomaticdetails of graphics methods to use
      PerformanceGoalAutomaticaspects of performance to try to optimize
      PlotLabelNonean overall label for the plot
      PlotRangeAllrange of values to include
      PlotRangeClippingFalsewhether to clip at the plot range
      PlotRangePaddingAutomatichow much to pad the range of values
      PlotRegionAutomaticthe final display region to be filled
      PlotTheme $PlotThemeoverall theme for the graph
      PreserveImageOptionsAutomaticwhether to preserve image options when displaying new versions of the same graphic
      Prolog{}primitives rendered before the main plot
      RotateLabelTruewhether to rotate y labels on the frame
      TicksAutomaticaxes ticks
      TicksStyle{}style specifications for axes ticks
      VertexCoordinates Automaticcoordinates for vertices
      VertexLabels Nonelabels and placements for vertices
      VertexLabelStyleAutomaticstyle to use for vertex labels
      VertexShape Automaticgraphic shape for vertices
      VertexShapeFunction Automaticgenerate graphic shapes for vertices
      VertexSize Mediumsize of vertices
      VertexStyle Automaticstyles for vertices
      VertexWeight Automaticweights for vertices

Examples

open all close all

Basic Examples  (4)

Construct a graph by starting with x and applying f successively 3 times:

Wolfram Language code: NestGraph[f, x, 3, VertexLabels -> Automatic, ImagePadding -> 40]

The function to nest can be a pure function:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 10]&, Range[0, 9], VertexLabels -> Automatic]

Generate a binary tree of nested functions:

Wolfram Language code: NestGraph[{f[#], g[#]}&, x, 3, VertexLabels -> Automatic]

Generate a graph of neighboring countries around Switzerland:

Wolfram Language code: NestGraph[#["BorderingCountries"]&, ["switzerland"], 2, VertexLabels -> Automatic]

Scope  (3)

NestGraph works with any function including an expression:

Wolfram Language code: NestGraph[f, x, 3]

A pure function:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 10]&, Range[0, 9], 2]

NestGraph works with any expression including integers:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 10]&, {1, 2, 3, 4, 5}, 2]

Strings:

Wolfram Language code: NestGraph[WordData[#, "Synonyms", "List"] &, "tree", 2]

Graphs:

Wolfram Language code: NestGraph[WordData[#, "Synonyms", "List"] &, [image], 2]

Limit the depth of the tree:

Wolfram Language code: NestGraph[WordData[#, "Synonyms", "List"] &, "tree", 3]

Options  (80)

AnnotationRules  (2)

Specify an annotation for vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, AnnotationRules -> {1 -> {VertexLabels -> "hello"}}]

Edges:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, AnnotationRules -> {01 -> {EdgeLabels -> "hello"}}]

DirectedEdges  (1)

By default, a directed graph is generated:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 4], 2]

Use DirectedEdges->False to generate an undirected graph:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 4], 2, DirectedEdges -> False]

EdgeLabels  (7)

Label the edge 12:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> "Hello"}]

Label all edges individually:

Wolfram Language code: el = EdgeList[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2]]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]

Use any expression as a label:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {01 -> [image], 12 -> [image], 20 -> [image]}]

Use Placed with symbolic locations to control label placement along an edge:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]

Use explicit coordinates to place labels:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 4, 1 / 3}}]

Vary positions within the label:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]

Place multiple labels:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> {12 -> Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]}]

Use automatic labeling by values through Tooltip and StatusArea:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> Placed["Name", Tooltip]]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeLabels -> Placed["Name", StatusArea]]

EdgeShapeFunction  (6)

Get a list of built-in settings for EdgeShapeFunction:

Wolfram Language code: ResourceData["EdgeShapeFunction"]

Undirected edges including the basic line:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> "Line"]

Lines with different glyphs on the edges:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]

Directed edges including solid arrows:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]

Line arrows:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]

Open arrows:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]

Specify an edge function for an individual edge:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {12 -> "DotLine"}]

Combine with a different default edge function:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> {12 -> "BoxLine", "DotLine"}]

Draw edges by running a program:

Wolfram Language code: ef[pts_List, e_] := Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeShapeFunction -> ef]

EdgeShapeFunction can be combined with EdgeStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]

EdgeShapeFunction has higher priority than EdgeStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]

EdgeStyle  (2)

Style all edges:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeStyle -> style, PlotLabel -> style], {style, {Gray, Dashed, Thick}}]

Style individual edges:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeStyle -> {12 -> Blue, 01 -> Dashed}]

EdgeWeight  (2)

Specify a weight for all edges:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeWeight -> RandomInteger[5, 3]]
Wolfram Language code: WeightedAdjacencyMatrix[%]//MatrixForm

Use any numeric expression as a weight:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, EdgeWeight -> {a, b, c}]
Wolfram Language code: WeightedAdjacencyMatrix[%]//MatrixForm

GraphHighlight  (3)

Highlight the vertex 1:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> Tiny, GraphHighlight -> {1}]

Highlight the edge 23:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> Tiny, GraphHighlight -> {12}]

Highlight vertices and edges:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> Tiny, GraphHighlight -> {1, 2, 01, 12}]

GraphHighlightStyle  (2)

Get a list of built-in settings for GraphHighlightStyle:

Wolfram Language code: ResourceData["GraphHighlightStyle"]

Use built-in settings for GraphHighlightStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, GraphHighlight -> {1, 12}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]

GraphLayout  (5)

By default, the layout is chosen automatically:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 10], 2, GraphLayout -> Automatic]

Specify layouts on special curves:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 10], 2, GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]

Specify layouts that satisfy optimality criteria:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 20], 2, GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]

VertexCoordinates overrides GraphLayout coordinates:

Wolfram Language code: {NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 5], 2, GraphLayout -> "SpringElectricalEmbedding"], WheelGraph[5, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 4}]]}

Use AbsoluteOptions to extract VertexCoordinates computed using a layout algorithm:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 5], 2]
Wolfram Language code: AbsoluteOptions[%, VertexCoordinates]

PlotTheme  (4)

Base Themes  (2)

Use a common base theme:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, PlotTheme -> "Business"]

Use a monochrome theme:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, PlotTheme -> "Monochrome"]

Feature Themes  (2)

Use a large graph theme:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, PlotTheme -> "LargeGraph"]

Use a classic diagram theme:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, PlotTheme -> "ClassicDiagram"]

VertexCoordinates  (3)

By default, any vertex coordinates are computed automatically:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2]

Extract the resulting vertex coordinates using AbsoluteOptions:

Wolfram Language code: AbsoluteOptions[%, VertexCoordinates]

Specify a layout function along an ellipse:

Wolfram Language code: ellipseLayout[n_, {a_, b_}] := Table[{a Cos[2Pi / n u], b Sin[2Pi / n u]}, {u, 1, n}]
Wolfram Language code: Graphics[Point[ellipseLayout[20, {2, 1}]]]

Use it to generate vertex coordinates for a graph:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 19], 2, VertexCoordinates -> ellipseLayout[20, {2, 1}]]

VertexCoordinates has higher priority than GraphLayout:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 3], 2, VertexCoordinates -> Table[{i, i}, {i, 4}], GraphLayout -> "CircularEmbedding"]

VertexLabels  (13)

Use vertex names as labels:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> "Name"]

Label individual vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> {2 -> "one"}]

Label all vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Table[i -> Subscript[v, i], {i, 0, 2}]]

Use any expression as a label:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> {1 -> [image], 2 -> [image], 0 -> [image]}, ImagePadding -> 20]

Use Placed with symbolic locations to control label placement, including outside positions:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 0, 2}], PlotLabel -> p, ImagePadding -> 20], {p, {Before, After, Below, Above}}]

Symbolic outside-corner positions:

Wolfram Language code: pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};
Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.1, VertexShapeFunction -> "Square", ImagePadding -> 20, VertexLabels -> Table[i -> Placed["■■■", p], {i, 0, 2}], PlotLabel -> p], {p, pl}]

Symbolic inside positions:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.45, VertexLabels -> Table[i -> Placed["■■■", p], {i, 0, 2}], VertexShapeFunction -> "Square", PlotLabel -> p], {p, {Left, Top, Right, Bottom}}]

Symbolic inside-corner positions:

Wolfram Language code: pl = {{Left, Bottom}, {Right, Bottom}, {Left, Top}, {Right, Top}};
Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.45, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 0, 2}], PlotLabel -> p], {p, pl}]

Use explicit coordinates to place the centers of labels:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 0, 2}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]

Place all labels at the upper-right corner of the vertex and vary the coordinates within the label:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.35, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], {{1, 1}, p}], {i, 0, 2}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]

Place multiple labels:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]

Any number of labels can be used:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]}]

Use the argument Placed to control formatting including Tooltip:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Placed["Name", Tooltip]]

Or StatusArea:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Placed["Name", StatusArea]]

Use more elaborate formatting functions:

Wolfram Language code: rotateLabel[lbl_] := Rotate[lbl, 45Degree]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 0, 2}]]
Wolfram Language code: panelLabel[lbl_] := Panel[lbl, FrameMargins -> 0, Background -> StandardYellow]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 0, 2}]]
Wolfram Language code: hyperlinkLabel[lbl_] := Hyperlink[lbl, "http://www.wolfram.com"]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 0, 2}]]

VertexShape  (5)

Use any Graphics, Image, or Graphics3D as a vertex shape:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]

Specify vertex shapes for individual vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShape -> {2 -> [image]}, VertexSize -> Medium]

VertexShape can be combined with VertexSize:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Small, Large}}]

VertexShape is not affected by VertexStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]

VertexShapeFunction has higher priority than VertexShape:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexShapeFunction -> "Square", VertexShape -> [image]]

VertexShapeFunction  (10)

Get a list of built-in collections for VertexShapeFunction:

Wolfram Language code: ResourceData["VertexShapeFunction"]

Use built-in settings for VertexShapeFunction in the "Basic" collection:

Wolfram Language code: ResourceData["VertexShapeFunction", "Basic"]

Simple basic shapes:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]

Common basic shapes:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]

Use built-in settings for VertexShapeFunction in the "Rounded" collection:

Wolfram Language code: ResourceData["VertexShapeFunction", "Rounded"]
Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]

Use built-in settings for VertexShapeFunction in the "Concave" collection:

Wolfram Language code: ResourceData["VertexShapeFunction", "Concave"]
Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]

Draw individual vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]

Combine with a default vertex function:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]

Draw vertices using a predefined graphic:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> (Inset[[image], #]&)]

Draw vertices by running a program:

Wolfram Language code: vf[{xc_, yc_}, name_, {w_, h_}] := Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h}, Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}] ];
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> vf, VertexSize -> 0.2]

VertexShapeFunction can be combined with VertexStyle:

Wolfram Language code: vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]

VertexShapeFunction has higher priority than VertexStyle:

Wolfram Language code: vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]

VertexShapeFunction has higher priority than VertexSize:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexShapeFunction -> "Star", VertexSize -> {1 -> Small, Medium}]

VertexShapeFunction has higher priority than VertexShape:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]

VertexSize  (8)

By default, the size of vertices is computed automatically:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> Automatic]

Specify the size of all vertices using symbolic vertex size:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]

Use a fraction of the minimum distance between vertex coordinates:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]

Use a fraction of the overall diagonal for all vertex coordinates:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]

Specify size in both the and directions:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]

Specify the size for individual vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> {1 -> 0.2, 2 -> 0.3}]

VertexSize can be combined with VertexShapeFunction:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]

VertexSize can be combined with VertexShape:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]

VertexStyle  (5)

Style all vertices:

Wolfram Language code: Table[NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexStyle -> style, VertexSize -> 0.3, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]

Style individual vertices:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexStyle -> {1 -> Blue, 2 -> Red}, VertexSize -> 0.2]

VertexShapeFunction can be combined with VertexStyle:

Wolfram Language code: vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]

VertexShapeFunction has higher priority than VertexStyle:

Wolfram Language code: vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}
Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]

VertexStyle can be combined with BaseStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]

VertexStyle has higher priority than BaseStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]

VertexShape is not affected by VertexStyle:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]

VertexWeight  (2)

Set the weight for all vertices:

Wolfram Language code: WheelGraph[4, VertexWeight -> {2, 3, 4, 5}]
Wolfram Language code: AnnotationValue[{%, 1}, VertexWeight]

Use any numeric expression as a weight:

Wolfram Language code: NestGraph[Mod[# ^ 2 + 1, 5]&, Range[0, 2], 2, VertexWeight -> {a, b, c}]
Wolfram Language code: AnnotationValue[{%, 1}, VertexWeight]

Applications  (2)

Generate a network of "nearby" functions in the Wolfram Language:

Wolfram Language code: NestGraph[WolframLanguageData[#, EntityProperty["WolframLanguageSymbol", "RelatedSymbols"]]&, Entity["WolframLanguageSymbol", "Nest"], 2, VertexLabels -> "Name"]

Visualize file systems:

Wolfram Language code: NestGraph[FileNames["*", #] &, $InstallationDirectory, 3, VertexLabels -> Placed["Name", Tooltip]]

Properties & Relations  (5)

Use VertexCount and EdgeCount to count vertices and edges:

Wolfram Language code: g = NestGraph[Mod[# ^ 2 + 1, 10]&, {1, 2, 3, 4, 5}, 2]
Wolfram Language code: {VertexCount[g], EdgeCount[g]}

Use VertexList and EdgeList to enumerate vertices and edges in standard order:

Wolfram Language code: g = NestGraph[WordData[#, "Synonyms", "List"] &, "tree", 2]
Wolfram Language code: {VertexList[g], EdgeList[g]}

Compute the AdjacencyMatrix from a graph:

Wolfram Language code: g = NestGraph[f, x, 3];
Wolfram Language code: AdjacencyMatrix[g]//MatrixForm

NeighborhoodGraph can be found by using NestGraph:

Wolfram Language code: g = GridGraph[{4, 4}];
Wolfram Language code: g1 = NeighborhoodGraph[g, 1, 2]
Wolfram Language code: g2 = NestGraph[Rest@VertexOutComponent[g, #, 1]&, 1, 2, DirectedEdges -> False]//SimpleGraph
Wolfram Language code: IsomorphicGraphQ[g1, g2]

Generate a tree of nested functions as lists:

Wolfram Language code: NestList[{f[#], g[#]}&, x, 3]

Generate as a graph:

Wolfram Language code: NestGraph[{f[#], g[#]}&, x, 3, VertexLabels -> "Name"]

Neat Examples  (1)

Create a nested graph showing closest same-length words starting with "math":

Wolfram Language code: word = "math";
Wolfram Language code: words = Select[WordList[], StringLength[#] == StringLength[word]&];
Wolfram Language code: SmallestEditDistances[word_] := Nearest[Complement[words, {word}], word]
Wolfram Language code: HighlightGraph[NestGraph[SmallestEditDistances, word, 2, VertexLabels -> All], word]

See Also

Nest  Graph  NestList  NearestNeighborGraph

Function Repository: NestGraphTagged

Related Guides

    ▪
  • Functional Programming
  • ▪
  • Functional Iteration
  • ▪
  • Graph Construction & Representation
  • ▪
  • Iterated Maps & Fractals

Related Links

  • An Elementary Introduction to the Wolfram Language : Applying Functions Repeatedly

History

Introduced in 2015 (10.2)

Wolfram Research (2015), NestGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NestGraph.html.

Text

Wolfram Research (2015), NestGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NestGraph.html.

CMS

Wolfram Language. 2015. "NestGraph." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestGraph.html.

APA

Wolfram Language. (2015). NestGraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestGraph.html

BibTeX

@misc{reference.wolfram_2026_nestgraph, author="Wolfram Research", title="{NestGraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/NestGraph.html}", note=[Accessed: 12-November-2026]}

BibLaTeX

@online{reference.wolfram_2026_nestgraph, organization={Wolfram Research}, title={NestGraph}, year={2015}, url={https://reference.wolfram.com/language/ref/NestGraph.html}, note=[Accessed: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.