Function Resource

Function Repository Resource:

RandomLabyrinth

Source Notebook

Generate random labyrinths

Contributed by: Anton Antonov

ResourceFunction["RandomLabyrinth"][n]

generates a random labyrinth based on n × n grid graph.

ResourceFunction["RandomLabyrinth"][{m,n}]

generates a random labyrinth based on a grid graph with m rows and n columns.

ResourceFunction["RandomLabyrinth"][size,prop]

gives the specified property prop for the labryinth.

Details and Options

ResourceFunction["RandomLabyrinth"] generates labyrinths based on regular rectangular (default) or hexagonal grid graphs.
RandomLabyrinth supports the option of Graph, along with the option "GridLayout" which can be set to "Hexagonal" to create a hexagonal labyrinth.
The supported properties for prop are: "Walls", "Paths", "Solution", "Start", "End" and All. ResourceFunction["RandomLabyrinth"][size,"Properties"] gives the list of supported properties.

Examples

Basic Examples (1) 

Make a random rectangular grid labyrinth:

In[1]:=
SeedRandom[3];
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{10, 20}]
Out[2]=

Scope (2) 

Make a random hexagonal grid labyrinth:

In[3]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{8, 16}, "GridLayout" -> "Hexagonal"]
Out[3]=

RandomLabyrinth can be given different properties:

In[4]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][10, "Properties"]
Out[4]=

If the properties argument is All, then an association with all properties is returned:

In[5]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][5, All, ImageSize -> Tiny]
Out[5]=

Options (1) 

GridLayout (1) 

The option "GridLayout" specifies the type grid graphs used to make the labyrinth. It takes the values "Rectangular" and "Hexagonal":

In[6]:=
ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][7, "GridLayout" -> #, ImageSize -> {Automatic, 200}] & /@ {"Rectangular", "Hexagonal"}
Out[6]=

Applications (3) 

Make a rectangular grid labyrinth and show it together with a solution:

In[7]:=
res = ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{12, 24}, {"Walls", "Paths", "Solution"}, ImageSize -> 800];
Show[res["Walls"], HighlightGraph[#, #] &@Subgraph[res["Paths"], res["Solution"]]]
Out[8]=

Make a hexagonal grid labyrinth and show it together with a solution:

In[9]:=
res = ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{16, 20}, {"Walls", "Paths", "Solution"}, "GridLayout" -> "Hexagonal", ImageSize -> 900];
Show[res["Walls"], HighlightGraph[#, #] &@Subgraph[res["Paths"], res["Solution"]]]
Out[10]=

Generate 100 labyrinths:

In[11]:=
SeedRandom[55];
AbsoluteTiming[
 labs = Table[ResourceFunction[
CloudObject[
      "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][10, {"Walls", "Paths", "Solution"}, ImageSize -> Small], 100];
 ]
Out[12]=

Show the histogram of the shortest path solution lengths:

In[13]:=
Histogram[Length[#Solution] & /@ labs, Sequence[
 PlotRange -> All, PlotTheme -> "Detailed", PlotLabel -> "Distribution of solution lengths", FrameLabel -> {"solution length", "count"}, ImageSize -> 350]]
Out[13]=

Show the labyrinths with the smallest and largest shortest paths solutions:

In[14]:=
Show[#Walls, HighlightGraph[#, #] &@Subgraph[#Paths, #Solution], ImageSize -> 230] & /@ SortBy[labs, Length[#Solution] &][[{1, -1}]]
Out[14]=

Possible Issues (2) 

For larger size values the labyrinth generation might be slow, especially, for hexagonal labyrinths:

In[15]:=
AbsoluteTiming[ResourceFunction[
CloudObject[
   "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][50, "GridLayout" -> "Hexagonal", EdgeShapeFunction -> ({
Opacity[1], 
AbsoluteThickness[1], 
Line[#]}& )]]
Out[15]=

The making of rectangular labyrinths with same size parameters is faster:

In[16]:=
AbsoluteTiming[ResourceFunction[
CloudObject[
   "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][50, "GridLayout" -> "Rectangular", EdgeShapeFunction -> ({
Opacity[1], 
AbsoluteThickness[1], 
Line[#]}& )]]
Out[16]=

Neat Examples (3) 

Larger rectangular grid maze:

In[17]:=
SeedRandom[44];
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{30, 60}, EdgeShapeFunction -> ({Opacity[1], DarkBlue, AbsoluteThickness[6], Line[#1]} &), ImageSize -> 800]
Out[18]=

A larger hexagonal grid maze with its largest connected components colored:

In[19]:=
SeedRandom[184];
g = ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][{20, 40}, "GridLayout" -> "Hexagonal", EdgeShapeFunction -> Automatic, ImageSize -> 800];
HighlightGraph[g, Map[Subgraph[g, #] &, ConnectedComponents[g][[1 ;; 2]]], EdgeShapeFunction -> ({Opacity[1], AbsoluteThickness[4], Line[#1]} &)]
Out[21]=

A grid of tiny labyrinths:

In[22]:=
SeedRandom[9987];
Multicolumn[Table[ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/antononcube/DeployedResources/Function/RandomLabyrinth"]][RandomInteger[{6, 7}], ImageSize -> Tiny],
   36], 6]
Out[23]=