Function Resource

NestGraphTagged

A version of NestGraph in which edges corresponding to different outputs are tagged differently

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

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

Details and Options

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 function f[expr] returns values of the form exprtag, then those tags are used instead. This can be modified with "TagHead" option.
NestGraph takes the same options as Graph.
ResourceFunction["NestGraphTagged"] accepts the following options:
"StateLabeling"Falsewhether to render states as boxes
"RuleStyling"Automaticlist of styles for edges/rules
"FormattingFunction"Automatichow to format state
"PostProcessGraph"Identitypost process graph
"TagHead"Rulecustom head to associate tags with output values
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[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {2 n, n + 1}, 1, 4]
Out[1]=

Use pure function:

In[2]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
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[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][f, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4]
Out[4]=

Add a legend to explain colors:

In[5]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
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[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][{2 #, # + 1} &, {1}, 4, "StateLabeling" -> True]
Out[6]=

Scope (4) 

Edge colorings can be overridden:

In[7]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, "RuleStyling" -> {Black, Orange}]
Out[7]=

Add labels to edges:

In[8]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
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 tooltips to edges:

In[9]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, EdgeLabels -> Placed["EdgeTag", Tooltip]]
Out[9]=

An alternate type of edge labeling:

In[10]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {n + 2, n + 3}, 0, 4, "StateLabeling" -> True,
  EdgeLabels -> {
   1 -> Placed["+2", {.7, {-.2, 0}}], 2 -> Placed["+3", {.7, {-.2, 0}}]
   }]
Out[10]=

Make custom edge tags by constructing rule values:

In[11]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][
 s |-> # -> # & /@ StringReplaceList[s, {"A" -> "AB", "B" -> "A"}], "A", 3, "StateLabeling" -> True, EdgeLabels -> "EdgeTag"]
Out[11]=

Options (5) 

StateLabeling (1) 

Turn on special vertex shapes:

In[12]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][{2 #, # + 1} &, {1}, 4, "StateLabeling" -> True]
Out[12]=

FormattingFunction (1) 

Change the formatting for vertex 31:

In[13]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
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[13]=

RuleStyling (1) 

Use custom styles for output values:

In[14]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {2 n, n + 1}, {1}, 4, "RuleStyling" -> {Black, Orange}]
Out[14]=

PostProcessGraph (1) 

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

In[15]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][n |-> {2 n, n + 1, n - 1}, {1}, 4, "PostProcessGraph" -> ReverseGraph, PlotLegends -> TraditionalForm /@ {2 n, n + 1, n - 1}]
Out[15]=

TagHead (1) 

Instead of Rule, use custom head to specify edge tags:

In[16]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][
 s |-> Labeled[#, #] & /@ StringReplaceList[s, {"A" -> "AB", "B" -> "A"}], "A", 3, "StateLabeling" -> True, EdgeLabels -> "EdgeTag", "TagHead" -> Labeled]
Out[16]=

Neat Examples (2) 

Vertices can be number pairs:

In[17]:=
ResourceFunction[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
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[
 "https://www.wolframcloud.com/obj/nikm/DeployedResources/Function/\
NestGraphTagged"][v |-> Mod[{v + 1, 10 v}, 7], Range[1], 10, "StateLabeling" -> True, GraphLayout -> "CircularEmbedding"]
Out[18]=