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

Expanding the first comment by Stanley on the page for OEIS A001333:
Let
a(n)
be the number of
n
-step self-avoiding paths with steps E, W, and N. Paths ending in N are obtained by appending N to any path of length
n−1
, giving
a(n−1)
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
a(n−1)
possibilities, with final pairs EE, NE, or WW. The only remaining case is NW, obtained by appending NW to any path of length
n−2
, giving
a(n−2)
possibilities. Therefore,
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
n−1
—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
a(n−2)
term.
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//Numerator
Out[]=
{1,3,7,17,41,99,239,577,1393,3363,8119}
In[]:=
ResourceFunction["OEISLookup"][%]
Out[]=
A001333OEISLookup
Name: Pell-Lucas numbers: numerators of continued fraction convergents to sqrt(2).
Sample: {1,1,3,7,17,41,99,19,4478554083,10812186007,26102926097,63018038201,152139002499,367296043199}
,A078057OEISLookup
Name: Expansion of (1+x)/(1-2*x-x^2).
Sample: {1,3,7,17,41,99,20,10812186007,26102926097,63018038201,152139002499,367296043199,886731088897}
,A123335OEISLookup
Name: a(n) = -2*a(n-1) + a(n-2) for n>1, a(0)=1, a(1)=-1.
Sample: {1,-1,3,-7,17,-41,19,-1855077841,4478554083,-10812186007,26102926097,-63018038201,152139002499}

In[]:=
ResourceFunction["OEISSequenceData"]["A001333","Name"]
Out[]=
Pell-Lucas numbers: numerators of continued fraction convergents to sqrt(2).

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

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

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
Type:
{Integer64}ByteTrie

It takes about 20 seconds to compute 18 levels:
In[]:=
AbsoluteTiming[trie1=cf[18]]
Out[]=
20.4541,DataStructure
Type:ByteTrie
Data not saved. Save now

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

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