Function Repository Resource:

MultiwayDeletionsGraph

Source Notebook

Explore graph traversals while deleting visited components

Contributed by: Bradley Klee

ResourceFunction["MultiwayDeletionsGraph"][g]

returns a directed acyclic graph which maps all possible traversals of graph g while deleting vertices along the way.

ResourceFunction["MultiwayDeletionsGraph"][g,v1]

traverses graph g starting only from initial vertex v1.

ResourceFunction["MultiwayDeletionsGraph"][g,{v1,v2,,vn}]

traverses graph g starting from n initial vertices vi.

ResourceFunction["MultiwayDeletionsGraph"][g,{v1,v2,,vn},method]

allows deleting edges by setting method equals EdgeDelete.

ResourceFunction["MultiwayDeletionsGraph"][g,{v1,v2,,vn},method,max]

introduces a cutoff max for cases where the state space graph g is too large to explore efficiently.

Details

ResourceFunction["MultiwayDeletionsGraph"] takes the same options as Graph.
Vertex names of a ResourceFunction["MultiwayDeletionsGraph"] output depend on a method, either VertexDelete or EdgeDelete:
w1={{},v1}an initial condition for either method, where v1VertexList[g]
wn={Sort[{v1,v2,,vn-1}],vn}with viVertexList[g] and wnVertexList[ResourceFunction["MultiwayDeletionsGraph"][g,v1]]
wn={Sort[{e1,e2,,en-1}],vn]witheiEdgeList[g]andwnVertexList[ResourceFunction["MultiwayDeletionsGraph"][g,v1,EdgeDelete]]
Each vertex records an unordered list of vertices or edges visited as well as a current location.
Two vertices wn and wn-1 are adjacent in the output of ResourceFunction["MultiwayDeletionsGraph"] if and only if the following conditions are met, where edge can be either DirectedEdge or UndirectedEdge:
method=VertexDeleteFirst[wn]==First[wn-1]⋃{vn}&&GraphDistance[g,Last[wn-1],Last[wn]]⩵1
method=EdgeDeleteFirst[wn]==First[wn-1]⋃{edge[vn-1,vn]}&& GraphDistance[g,Last[wn-1],Last[wn]]⩵1
In general, paths through a multiway deletions graph are said to be confluent whenever it is possible to find different valid orderings of the unordered First[wn] which fix the endpoint Last[wn]. Here, "valid" means that, per chosen ordering, each pair of successive vertices wi and wi-1 must satisfy the adjacency criteria.

Examples

Basic Examples (2) 

Plot two pairs of confluent self-avoiding walks on a 2×2 GridGraph:

In[1]:=
ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{2, 2}], {1, 4}]
Out[1]=

Add labels to see which vertices where visited when:

In[2]:=
Row[{GridGraph[{2, 2}, ImageSize -> 50, VertexLabels -> (x_ :> Placed[x, Center]),
   VertexSize -> Large, EdgeStyle -> Darker[Gray, .8],
   VertexStyle -> Directive[White, EdgeForm[LightGray]]], Style[" \[LongRightArrow] ", Gray, Bold, 24], Grid[{WeaklyConnectedGraphComponents@
     ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{2, 2}],
      VertexLabels -> (x_ :> Placed[Last[x], Center]),
      VertexStyle -> Directive[White, EdgeForm[Gray]],
      EdgeStyle -> Gray,
      GraphLayout -> "LayeredDigraphEmbedding",
      VertexSize -> 1/2, ImageSize -> 200, AspectRatio -> 1/2]},
   Frame -> All, FrameStyle -> LightGray, Spacings -> {1, 1}]}]
Out[2]=

Graph self-avoiding walks on a 3×3 grid:

In[3]:=
ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{3, 3}]]
Out[3]=

Only compute the subgraph starting from vertex 5:

In[4]:=
ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{3, 3}], 5]
Out[4]=

Decorate the graph with publication-quality icons:

In[5]:=
With[{StyledDeletionsGraph = Function[{mdg, ing, ivs, vs},
    Graph[mdg, VertexSize -> vs, EdgeStyle -> Gray,
     VertexShapeFunction -> (Inset[HighlightGraph[Graph[ing, VertexStyle -> Directive[EdgeForm[GrayLevel[.4]], GrayLevel[.7]],
           EdgeStyle -> Gray], {Style[First[#2], Hue[.55, 1, 0.9],
            EdgeForm[GrayLevel[0, 0.25]]],
           Style[Last[#2], Red, EdgeForm[GrayLevel[0, 0.25]]]},
          VertexSize -> ivs, ImageSize -> 30], #1, Center, #3] &),
     PerformanceGoal -> "Quality"]]},
 StyledDeletionsGraph[
  ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{3, 3}], {5}],
  GridGraph[{3, 3}], .75, 1.5]]
Out[5]=

Allow walks from vertex 5 to intersect on vertices, but not on edges:

In[6]:=
ResourceFunction["MultiwayDeletionsGraph"][
 GridGraph[{3, 3}], 5, EdgeDelete]
Out[6]=

Again, decorate the graph with publication-quality icons:

In[7]:=
With[{StyledDeletionsGraph = Function[{mdg, ing, ivs, vs},
    Graph[mdg, VertexSize -> vs, EdgeStyle -> Gray,
     VertexShapeFunction -> (Inset[HighlightGraph[Graph[ing, VertexStyle -> Directive[EdgeForm[GrayLevel[.4]], GrayLevel[.7]], EdgeStyle -> Directive[Darker@Gray, Thick]], {Style[
            First[#2], Red,
            EdgeForm[GrayLevel[0, 0.25]]],
           Style[Last[#2], \!\(\*
TagBox[
StyleBox[
RowBox[{"Hue", "[", 
RowBox[{"0.08`", ",", "1", ",", "1"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True,
"NodeID" -> 26],
FullForm]\), EdgeForm[GrayLevel[0, 0.25]]]},
          VertexSize -> ivs, ImageSize -> 30], #1, Center, #3] &),
     PerformanceGoal -> "Quality"]]},
 StyledDeletionsGraph[
  ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{3, 3}], 5, EdgeDelete],
  GridGraph[{3, 3}], .75, 3.5]]
Out[7]=

Scope (1) 

Test graph transformations on random inputs:

In[8]:=
SeedRandom["RandTest"];
With[{ing = First[WeaklyConnectedGraphComponents[RandomGraph[{5, 7}]]]},
 WeaklyConnectedGraphComponents[
    ResourceFunction["MultiwayDeletionsGraph"][ing, {}, #],
    GraphLayout -> "LayeredDigraphEmbedding",
    AspectRatio -> 1/2] & /@ {VertexDelete, EdgeDelete}]
Out[9]=

Properties and Relations (1) 

When inputting a directed acyclic graph, the output is always a TreeGraph:

In[10]:=
With[{g0 = (SeedRandom["DAGtest"];
    DirectedGraph[RandomGraph[{10, 18}], "Acyclic"])},
 Grid[Partition[Labeled[Framed[Graph[#, ImageSize -> {UpTo[100], 100}],
       FrameStyle -> LightGray],
      Style[TreeGraphQ[#], GrayLevel[.4]]
      ] & /@ WeaklyConnectedGraphComponents[
     ResourceFunction["MultiwayDeletionsGraph"][g0]], 5], Spacings -> {1, 2}]]
Out[10]=

Possible Issues (1) 

MultiwayDeletionsGraph automatically deduplicates edges:

In[11]:=
ResourceFunction["MultiwayDeletionsGraph"][
 Graph[Table[UndirectedEdge[1, 2], 5]], {1}, EdgeDelete]
Out[11]=

Neat Examples (2) 

Graph non-self-intersecting walks along the edges of a cube:

In[12]:=
cubeWalks = ResourceFunction["MultiwayDeletionsGraph"][GridGraph[{2, 2, 2}], 1]
Out[12]=

Find the out vertices associated with Hamiltonian cycles:

In[13]:=
hcTerms = Cases[Select[VertexList[cubeWalks], Length[#[[1]]] == 7 &],
  {_, Alternatives @@ VertexOutComponent[
     GridGraph[{2, 2, 2}], 1, {1}]}]
Out[13]=

Count the number of directed Hamiltonian cycles:

In[14]:=
Total[Length@FindPath[cubeWalks, {{}, 1}, #, 8, All] & /@ hcTerms]
Out[14]=

Solve Euler's bridges of Königsberg problem by computing all possible paths:

In[15]:=
With[{gKonigsbergFixed = Graph[{
     UndirectedEdge[n1, n2], UndirectedEdge[s1, s2],
     UndirectedEdge[n1, w], UndirectedEdge[n2, w],
     UndirectedEdge[s1, w], UndirectedEdge[s2, w],
     UndirectedEdge[e, w], UndirectedEdge[n1, e],
     UndirectedEdge[s1, e]}]},
 Grid[Partition[Graph[#,
      ImageSize -> {Automatic, 10 Max[Length /@ VertexList[#][[All, 1]]]}] & /@ WeaklyConnectedGraphComponents[
     ResourceFunction["MultiwayDeletionsGraph"][gKonigsbergFixed, {}, EdgeDelete], GraphLayout -> "LayeredDigraphEmbedding"], 3], Spacings -> {1, 1}]]
Out[15]=

Publisher

Brad Klee

Version History

  • 1.0.0 – 14 June 2022

Related Resources

Author Notes

Help with graphic design from Stephen Wolfram and Jeremy Davis.

License Information