Function Repository Resource:

NestGraphTagged

Source Notebook

Create a graph by successively applying a function and differently tagging edges corresponding to different outputs

Contributed by: Nikolay Murzin and Ed Pegg Jr.

ResourceFunction["NestGraphTagged"][f,expr,n]

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

Details and Options

The expr can be any expression.
ResourceFunction["NestGraphTagged"][f,expr] is equivalent to ResourceFunction["NestGraphTagged"][f,expr,1].
ResourceFunction["NestGraphTagged"][f,expr] gives a graph with tagged edges , where f[expr] evaluates to {expr1,,exprk}.
If a function f[expr] returns a list values of the form tagexpr or an association <|tag1expr1,tag2expr2,|>, then those tags are used instead.
ResourceFunction["NestGraphTagged"] takes the same options as Graph with the following additions:
"StateLabeling"Falsewhether to render states as boxes
"RuleStyling"Automaticlist of styles for edges/rules
"FormattingFunction"Automatichow to format a state
"PostProcessGraph"Identitypost-process graph
Any None on the right-hand side of the rule is dropped from the graph.
One can use integers in place of tagged directed edges to specify EdgeLabels.
PlotLegends can be used to get a legend for different edge rules.

Examples

Basic Examples (5) 

Make a nested graph with edge coloring:

In[1]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1}, 1, 4]
Out[1]=

Use a pure function:

In[2]:=
ResourceFunction["NestGraphTagged"][{# - 1, 2 #} &, 1, 4]
Out[2]=

Use a list of initial expressions:

In[3]:=
f[n_] := {n + 1, 2 n, 3 n - 1}
ResourceFunction[
 "NestGraphTagged"][f, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4]
Out[4]=

Add a legend to explain colors:

In[5]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1, n - 1}, {1}, 4,
  PlotLegends -> TraditionalForm /@ {2 n, n + 1, n - 1}]
Out[5]=

Label the vertex states in a nested graph:

In[6]:=
ResourceFunction["NestGraphTagged"][{2 #, # + 1} &, {1}, 4, "StateLabeling" -> True]
Out[6]=

Scope (6) 

Edge colorings can be overridden:

In[7]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, "RuleStyling" -> {Black, Orange}]
Out[7]=

Add labels to edges with EdgeLabels and a pattern:

In[8]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, EdgeLabels -> {
   DirectedEdge[_, _, t_] /; OddQ[t] :> Placed[t, {1/2, {-1, 1/2}}],
   DirectedEdge[_, _, t_] /; EvenQ[t] :> Placed[t, {1/2, {1, 1/2}}]}, EdgeLabelStyle -> Directive[12, Bold]]
Out[8]=

Add edge labels using output index:

In[9]:=
ResourceFunction["NestGraphTagged"][n |-> {n + 2, n + 3}, 0, 4, "StateLabeling" -> True, EdgeLabels -> {
   1 -> Placed["+2", {.7, {-.2, 0}}], 2 -> Placed["+3", {.7, {-.2, 0}}]
   }]
Out[9]=

Add tooltips to edges:

In[10]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, EdgeLabels -> Placed["EdgeTag", Tooltip]]
Out[10]=

Make custom edge tags by returning a list of rules (tags will be pairs of the form {key, index}):

In[11]:=
ResourceFunction["NestGraphTagged"][
 s |-> # -> # & /@ StringReplaceList[s, {"A" -> "AB", "B" -> "A"}], "A", 3, "StateLabeling" -> True, EdgeLabels -> "EdgeTag"]
Out[11]=

Make custom edge tags by returning an association:

In[12]:=
ResourceFunction["NestGraphTagged"][
 n |-> <|"\[Times]2" -> 2 n, "+1" -> n + 1|>, 2, 3, "StateLabeling" -> True, EdgeLabels -> "EdgeTag"]
Out[12]=

Options (4) 

StateLabeling (1) 

Turn on special vertex shapes:

In[13]:=
ResourceFunction["NestGraphTagged"][{2 #, # + 1} &, {1}, 4, "StateLabeling" -> True]
Out[13]=

FormattingFunction (1) 

Change the formatting for vertex 31:

In[14]:=
ResourceFunction["NestGraphTagged"][
 n |-> Mod[{2 n + 1, 3 n + 1}, 144], {1}, 200, "StateLabeling" -> True, "FormattingFunction" -> (# /. {31 -> Style[31, Red, Bold, 16], x_ :> Style[x, Black]} &)]
Out[14]=

RuleStyling (1) 

Use custom styles for output values:

In[15]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, "RuleStyling" -> {Black, Orange}]
Out[15]=

PostProcessGraph (1) 

If the output is graphics with a legend, it useful to apply some custom function to the graph itself:

In[16]:=
ResourceFunction["NestGraphTagged"][n |-> {2 n, n + 1, n - 1}, {1}, 4,
  "PostProcessGraph" -> ReverseGraph, PlotLegends -> TraditionalForm /@ {2 n, n + 1, n - 1}]
Out[16]=

Neat Examples (2) 

Vertices can be number pairs:

In[17]:=
ResourceFunction["NestGraphTagged"][
 v |-> {{{1, 0}, {1, 1}} . v, {{1, 1}, {0, 0}} . v}, {{1, 1}}, 3, "StateLabeling" -> True]
Out[17]=

For a remainder graph, to find 2143 (mod 7), start at 0 and follow (2143) to get remainder 1:

In[18]:=
ResourceFunction["NestGraphTagged"][v |-> Mod[{v + 1, 10 v}, 7], Range[1], 10, "StateLabeling" -> True, GraphLayout -> "CircularEmbedding"]
Out[18]=

Version History

  • 2.0.1 – 03 October 2022
  • 2.0.0 – 27 September 2022
  • 1.1.0 – 11 January 2022

Related Resources

License Information