This post shows how NestTree and NestGraph provide a direct way to generate, visualize, and quickly enumerate self-avoiding lattice paths with east, west, and north steps. The resulting sequence also has a surprising connection to the continued-fraction convergents to the square root of 2.
Proof Sketch: The Exact Number of Paths
Proof Sketch: The Exact Number of Paths
Expanding the first comment by Stanley on the page for OEIS A001333:
Let be the number of -step self-avoiding paths with steps E, W, and N. Paths ending in N are obtained by appending N to any path of length , giving possibilities. For paths ending horizontally, append E after a path ending in E or N, and append W after one ending in W. This gives another possibilities, with final pairs EE, NE, or WW. The only remaining case is NW, obtained by appending NW to any path of length , giving possibilities. Therefore, .
a(n)
n
n−1
a(n−1)
a(n−1)
n−2
a(n−2)
a(n)=2a(n−1)+a(n−2)
Although this recurrence looks simple, its coefficient 2 is not trivial: it represents two distinct copies of the paths of length —one obtained by moving north, and the other by making the uniquely determined horizontal continuation, with E chosen as the default direction after N. The alternative choice W after N accounts for the remaining term.
n−1
a(n−2)
Surprisingly, the resulting sequence 1, 3, 7, 17, 41, 99, 239,… also appears in the OEIS as the numerators of the continued-fraction convergents to the square root of 2.
In[]:=
RecurrenceTable[{a[n]==2a[n−1]+a[n−2],a[0]==1,a[1]==3},a,{n,0,15}]
Out[]=
{1,3,7,17,41,99,239,577,1393,3363,8119,19601,47321,114243,275807,665857}
In[]:=
FindSequenceFunction[%]
Out[]=
1
2
#1
(1-
2
)#1
(1+
2
)In[]:=
Convergents
2
,11//NumeratorOut[]=
{1,3,7,17,41,99,239,577,1393,3363,8119}
In[]:=
ResourceFunction["OEISLookup"][%]
Out[]=
A001333OEISLookup,A078057OEISLookup,A123335OEISLookup
In[]:=
ResourceFunction["OEISSequenceData"]["A001333","Name"]
Out[]=
Pell-Lucas numbers: numerators of continued fraction convergents to sqrt(2).
Visualization with NestGraph version
Visualization with NestGraph version
The Pell-Lucas Numbers corresponds to the number of nodes on each level:
In[]:=
buildPathG[w_]:=With[{lw=StringPart[w,-1]},w~StringJoin~#&/@If[lw=="N",{"N","W","E"},{"N",lw}]]
Let “O” be the root vertex:
In[]:=
g=UndirectedGraph@EdgeAdd[NestGraph[buildPathG,{"N","W","E"},6],{"O"->"N","O"->"E","O"->"W"}];
In[]:=
GraphPlot[g,GraphLayout->{"HyperbolicRadialEmbedding","RootVertex"->"O"},VertexSize->Tiny]
Out[]=
In[]:=
myTree=GraphTree[g];Length/@Table[TreeLevel[myTree,{lvl}->"Data"],{lvl,0,6}]
Out[]=
{1,3,7,17,41,99,239}
NestTree version
NestTree version
Let “O” be the root vertex:
In[]:=
buildPathTree[w_]:=With[{lw=StringPart[w,-1]},If[lw=="N"||lw=="O",{"N","W","E"},{"N",lw}]]
In[]:=
myTree=NestTree[buildPathTree,"O",6,TreeElementLabel->All->None,TreeElementStyle->All->Tiny]
Out[]=
The number of nodes on each level:
In[]:=
Length/@Table[TreeLevel[myTree,{lvl}->"Data"],{lvl,0,6}]
Out[]=
{1,3,7,17,41,99,239}
Optimize computation with ByteTrie and FunctionCompile
Optimize computation with ByteTrie and FunctionCompile
Because the number of paths increase exponentially, we need better data structure to store the result. The compiled data data structure is very efficient:
In[]:=
cf=FunctionCompile[Function[Typed[arg,"MachineInteger"],Module[{ds,trie,temp,temp2},ds=TypeHint[CreateDataStructure["Deque"],"Deque"::["String"]];trie=CreateDataStructure["ByteTrie","E","W"];ds["PushBackList",{"E","W","N"}];trie["Insert","E"];trie["Insert","W"];trie["Insert","N"];Do[temp=ds["PopFront"];temp2=temp~StringJoin~#&/@If[StringPart[temp,-1]=="N",{"N","W","E"},{"N",StringPart[temp,-1]}];Do[trie["Insert",ele],{ele,TypeHint[temp2,"ListVector"::["String"]]}];ds["PushBackList",temp2],{r,arg},{i,ds["Length"]}];trie]]]
Out[]=
CompiledCodeFunction
It takes about 20 seconds to compute 18 levels:
In[]:=
AbsoluteTiming[trie1=cf[18]]
Out[]=
20.4541,DataStructure
Close to 39 million paths and 883 MB:
In[]:=
IntegerName[trie1["Length"]]
Out[]=
38 million 613 thousand 963
In[]:=
trie1["ByteCount"]/1024.^2
Out[]=
883.804
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Visualize OEIS A001333 Pell-Lucas numbers with NestGraph and NestTree
by Shenghui Yang
Wolfram Community, STAFF PICKS, August 11, 2026
https://community.wolfram.com/groups/-/m/t/3778606
by Shenghui Yang
Wolfram Community, STAFF PICKS, August 11, 2026
https://community.wolfram.com/groups/-/m/t/3778606