An interactive column using Java applets
by Alex Bogomolny
Mazes
October 2003
Long time ago at a carpenter shop in Iowa City I purchased an unfinished desk for my first computer -- a grant from a local IBM office. I wondered aloud at the price, $35. "Cost of the material aside, labor alone should have cost more than that." The carpenter replied with pride, "Not if you know how to go about cutting and assembling the pieces, and you do not." I always remember this episode when a problem I try to resolve has a simple solution I keep missing.
For a long time I wanted to write a computer program to draw mazes. I even made a few attempts, too. But none was either satisfactory or satisfying. As it finally came out, there exist well established techniques for creating and solving mazes, known probably to every undergraduate computer science major. Indeed, the web is inundated with well documented examples by students who made their homework available on the Internet. But of course... Graphs and mazes are one and the same thing -- in some sense at least. The caveat is that the apparent complexity of a maze should not be judged by a naked eye, but rather with the mind's eye. A good example is furnished by what one may call a Hilbert maze. (To see the example in all its glory, check "Maze" and keep clicking on the applet below.)
As a graph, Hilbert's maze is trivial. It's just a two vertexgraph connected by a single edge, the latter being the canonical approximation to the famous plane filling curve. A similar observation applies to Peano's mazes, although there are 272 of them. Curiously, many of the historic mazes [Dudeney, Gardner, Rouse Ball] are exactly of this kind. The path meanders in a small area making the journey from the entrance to the exit long, but no fear -- there is no way to get lost in the hedges. (However, concerning Peano and Hilbert's mazes, one might contemplate the properties of the limiting maze. The path is still there, but the passageway is too narrow to squeeze by. If one could, it would make a long and a fearful journey indeed. A situation worth Zeno's attention.)
Kruskal's algorithm starts with the set of all vertices and an empty set of edges. On each iteration an edge is added to the collection. The only restriction in this process is that the addition of an edge should not create a circuit, a path that starts and ends with the same vertex. (Simple structures and algorithms have been devised to speed up verification, see [Levitin, pp. 314-317].)
One of the standard ways to solve a maze is by the depth first search [Levitin, pp. 163-165] technique: follow a path from a node until a dead end is reached. Retreat up the path till the first opportunity to check another node. Continue from there. Repeat the process until all the nodes have been visited.
The applet bellow has four tabs - Define grid, Define shape, Create maze, Solve maze. The starting point is a rectangular grid whose dimensions range from 2 to 50. The shape of the grid could be changed by removing some of the vertices and also by putting some of the removed ones back, and by selection of the Start (blue) and End (red) vertices. In any event, the vertices must be clicked on. As described, to create a maze, you are given a choice of Prim's and Kruskal's algorithm. You can watch them work by clicking the "Step By Step" button, or perform one step at a time with the "One Step" button. You can also watch a maze solved or proceed one step (of the depth first algorithm) at a time.
The English word "maze", that only in the 14th century became associated with the network of paths [Schwartzman] is related to a longer form "amaze." In old times it is said [Dudeney, p. 127] to be amazed meant to be "lost in thought" which may happen naturally once in a maze. Nowadays, the word "maze" still comes to mind sometimes when we are confronted with a quandary. The word "amaze" has now a different connotation. With a reference to a later, now customary meaning of the word, it is surely amazing how things become quite simple once "you know how."
On every step of the algorithm we have a set of vertices connected by edges such that the graph they form is a tree.
 
On every step of the algorithm we have a set of edges such that the graph they form is circuit-free.
Let V0 consist of a single vertex (Start), and E0 be empty.
 
Let V0 = V and E0 be empty.
Since minimum spanning trees exist, <V0, E0> is an acceptable pair.
 
Since minimum spanning trees exist, <V, E0> is an acceptable pair.
Assume that the pair <Vk, Ek> produced on the kth step is acceptable. To complete the proof, we'll show that the pair <Vk+1, Ek+1> is also acceptable.
 
Assume that the pair <V, Ek> produced on the kth step is acceptable. To complete the proof, we'll show that the pair <V, Ek+1> is also acceptable.
Assume to the contrary that <Vk+1, Ek+1> is not acceptable.
 
Assume to the contrary that <V, Ek+1> is not acceptable.
By assumption, there exists a minimum spanning tree <V, F> with Ek a subset of F. Assume ek+1 is the edge selected by Prim's algorithm on the (k+1)st step. Adjoining ek+1 to F is bound to produce a circuit.
 
By assumption, there exists a minimum spanning tree <V, F> with Ek a subset of F. Assume ek+1 is the edge selected by Kruskal's algorithm on the (k+1)st step. Adjoining ek+1 to F is bound to produce a circuit.
This means that there exists another edge e with weight at least that of ek+1 whose removal from F will leave a tree <V, F-{e}+{ek+1}>. The weight of the latter can't exceed that of the minimum spanning tree <V, F>. So it's, too, is a minimum spanning tree, but in contradiction with our assumption, its set of edges contains Ek+1.
 
This means that there exists another edge e with weight at least that of ek+1 whose removal from F will leave a tree <V, F-{e}+{ek+1}>. The weight of the latter can't exceed that of the minimum spanning tree <V, F>. So it's, too, is a minimum spanning tree, but in contradiction with our assumption, its set of edges contains Ek+1.