Function Repository Resource:

MultiwayGeneralizedShiftGraph

Source Notebook

Multiway state graph of a non-deterministic generalized shift

Contributed by: Pavel Hajek

ResourceFunction["MultiwayGeneralizedShiftGraph"][{n,shift,rewrite}, init]

generates a multiway state graph of a generalized shift, defined by non-deterministic shift and rewrite rules on a window of length n over a cyclic tape, starting from the initial condition init.

Details and Options

GeneralizedShift is a stateless machine that operates on a tape; at each step, it rewrites the content of a fixed-length window and shifts it to a new position.
The shift and rewrite operations are specified by lists of rules:
shift{{p1,,pn}int,}
rewrite{{p1,,pn}{r1,,rn},}
Patterns such as x_, x__, and x___ can be used in place of the symbols pi, and the bound variable x can be used in place of ri.
The initial condition init is specified as follows:
{a1,,ak}cyclic tape of values ai with the window initially at position 1
{x,{a1,,ak}}cyclic tape with the window intially at position x
Non-determinism arises from multiple rule matches per window, enumerated by ReplaceList.
Vertices of the multiway state graph represent all distinct states produced by the generalized shift; directed edges denote single computational steps, and branching captures non-determinism.
Besides Graph options, the following are supported:
HaltingStateQNonedefines halting state
MaxStepsInfinitymaximum steps pre branch
PruningProbability0controls random pruning of branches

Examples

Basic Examples (2) 

Create a multiway state graph of a "universal" non-deterministic generalized shift that computes all binary sequences of length 3:

In[1]:=
ResourceFunction[
 "MultiwayGeneralizedShiftGraph"][{1, {{_} -> 1, {_} -> -1}, {{_} -> {1}, {_} -> {0}}}, {0, 0, 0}]
Out[1]=

Show a multiway state graph after 3 steps with improved state visualization:

In[2]:=
ResourceFunction[
 "MultiwayGeneralizedShiftGraph"][{2, {{__} -> 1}, {{x_, y_} :> {y, x}, {0, 0} -> {1, 0}}}, {0, 0, 0, 0, 0}, "MaxSteps" -> 3, GraphStyle -> "Basic", VertexLabels -> None, GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Top}, VertexShapeFunction -> (
   Inset[
     RulePlot[TuringMachine[{1, 1, 2}], {{1, #2[[1, 1]]}, #2[[2]]}, 0,
       Frame -> False, Mesh -> All], #1, Center, 0.8] &)]
Out[2]=

Scope (3) 

Create a multiway state graph:

In[3]:=
gr = ResourceFunction[
  "MultiwayGeneralizedShiftGraph"][{2, {{_ 1} -> 1, {0, _} -> -1}, {{x_, _} :> {x, 1}, {x_, _} :> {x, 0}, {1, 0} -> {1, 1}}}, {0, 0, 0, 0}]
Out[3]=

Identify all cyclic computations:

In[4]:=
Flatten[FindCycle[gr, Infinity], 1][[All, 1]]
Out[4]=

Determine the worst-case runtime to reach each state:

In[5]:=
VertexEccentricity[gr, init ]
Out[5]=

Options (3) 

HaltingStateQ (1) 

Terminate the computation at halting states:

In[6]:=
haltingStateQ[{{pos_, relOrigin_}, tape_}] := Last @ tape === 1;
ResourceFunction[
 "MultiwayGeneralizedShiftGraph"][{1, {{_} -> 1, {_} -> -1}, {{_} -> {1}, {_} -> {0}}}, {0, 0, 0}, "HaltingStateQ" -> haltingStateQ]
Out[7]=

MaxSteps (1) 

Terminate the computation once the maximum number of steps has been reached:

In[8]:=
ResourceFunction[
 "MultiwayGeneralizedShiftGraph"][{1, {{_} -> 1, {_} -> -1}, {{_} -> {1}, {_} -> {0}}}, {0, 0, 0}, "MaxSteps" -> 2]
Out[8]=

PruningProbability (1) 

Randomly reduce the computational outcomes of each state, ensuring that at least one outcome remains:

In[9]:=
ResourceFunction[
 "MultiwayGeneralizedShiftGraph"][{1, {{_} -> 1, {_} -> -1}, {{_} -> {1}, {_} -> {0}}}, {0, 0, 0}, "PruningProbability" -> 0.7]
Out[9]=

Publisher

Pavel Hajek

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 05 December 2025

Source Metadata

Related Resources

License Information