Maze Construction by Using Characteristics of Eulerian Graphs

This paper describes the method to construct a maze for which the solution path goes along with the line of a closed one stroke drawn curve. If the crossing of the curve is considered a vertex and the curve between the two crossings (vertices) an edge, the closed curve becomes an Eulerian graph. Since maze does not allow the solution path to intersect itself, the proposed method constructs the path with no crossing; but it goes through every part of the original curve only once. In the process of the path construction, it makes use of the characteristics of Eulerian Graph. The theory was developed and a number of experiments were successfully conducted in some different situations.


Introduction
Maze generation by computing with square grids has been around for some years. The article [1] showed various algorithms to generate a square grid based maze, usually used, relating with graph theory. Hosokawa [2] presented a number of maze generation algorithms with the relation between path, wall, and coordinates. Xu and Kaplan [3] illustrated complex mazes emphasizing aesthetics for the maze. They also introduced a picture maze [4] called 'Maze-a-pix' by illustrating a sample picture maze, which is provided by Conceptis, Ltd. [5]. However, the method of the picture maze generation was not given. The internet site of the company shows various picture mazes and states a history of computer generated picture maze. Okamoto and Uehara [6] demonstrated an automatic generation algorithm for a picture maze by constructing a Hamiltonian path which fills up the picture region. However, the path is generated on the 2-by-2 extended picture array. The maze is so constructed that the player obtains the solution path by filling up all the path Original Research Article cells on the maze and by painting the picture region. Hamada [7][8] improved the method [6] by enabling to place the start and goal at different positions. Kurokawa developed a more efficient algorithm of construction of Hamiltonian path, with a rigorous proof, to generate a picture maze and introduced a generalized idea for maze path construction [9]. Ikeda and Hashimoto [10] illustrated a picture maze generation method by using simulated annealing. Kurokawa, Mori, and Mizuno [11][12][13] took a different approach and demonstrated the method to generate a picture maze by making use of the picture contours. In this method, all the contours are extracted and are incorporated into the picture maze. It is a very flexible method and can be applicable to a wide range of binary pictures. F. J. Wong and S. Takahashi [14] constructed a hybrid picture maze from two photograph images. This maze is not based on the square grid type mazes but on the special grids computed from the image. Two photo pictures are overlapped in the maze. The maze player depicts an outline of one picture on the other picture background.

Maze Structure
There are a number of different representation structures of a square grid maze. This paper uses one of them and it is explained in this section. A computer constructed maze is usually represented by a two-dimensional array. Such a maze consists of path and wall. A path cell is either a vertex or an edge (of a graph). Fig. 1 is a sample maze array of size 7x7---where '◎' is a vertex; 'e' is an edge, and 'W' and 'w' are walls. A vertex is placed one in every two cells horizontally and vertically. An edge represents the connection between the two vertices. Diagonally adjacent cells to a vertex are wall cells; there are four such cells around a vertex; the vertically or horizontally adjacent cells to a vertex are edge or wall cells. If the cell represents the connection between the two vertices, then it is an edge cell; otherwise it is a wall cell.
The connection between the two vertices is 4-way, vertical or horizontal; and no diagonal connection is allowed. The entire path routes in a maze organize a spanning tree. A vertex and an edge alternately appear along a path. The maze solution of Fig. 1 is the path from the start (S) to the goal (G), which is one of the paths from a vertex (the tree root) to another vertex (a leaf). The maze structure is kept maintained even if the maze is shifted in any direction one or more positions on the array. However, in Fig. 1, vertices are placed on (odd, odd) coordinates.
If the walls are thinned [15], the maze becomes as presented in Fig. 2. This maze, to be presented to a player, actually consists of two kinds of cells: path and wall. However, it also represents the corresponding graph with vertices and edges. There are other maze representations [16]. In short, a maze is said to be a collection of vertices and edges like a graph. However, more restrictions are imposed on a maze: physical positions of vertices and edges; the number of edges to be connected to a vertex and the like.   (Fig. 3). 2. Extract all the contours of the picture (Fig. 3). OpenCV [18] provides software to extract contours for binary pictures. Each contour is represented by a sequence of coordinate addresses. The connectivity of the contours is of 8-way. 3. The 8-way is converted to the 4-way along all contours (Fig. 3). This is to adjust the structure to that of a maze. 4. Each pixel of the contours is considered to be a maze vertex and to be placed one in every two cells vertically and horizontally on a maze array ( Fig. 4 (a)). Dotted contours are organized. 5. Filling all the gaps between the two previously adjacent vertices completes all the contour cycles of the original picture. The cycles are of maze path structure ( Fig. 4 (b)).

Eulerian Graph and One Stroke Closed Drawing
Consider a graph G = (V, E). A closed walk in G is called an Eulerian tour if it contains all the vertices and all the edges exactly once (while vertices can be repeated) in G. And a graph possessing a closed Eulerian tour is called an Eulerian graph [19]. Theorem of Eulerian graph states that a graph G = (V, E) is Eulerian if and only if it is connected and each vertex has an even degree, where V is the set of vertices and E is the set of edges of the graph [19].
Consider the closed line drawing in Fig. 5, which is obtained by Microsoft Paint [20]. Assume that each of the curve intersections is a vertex and that the curve between two intersections is an edge. Then the line drawing can be considered as a graph. Since the drawing is of one stroke, the graph is connected and each vertex has even number of edges. Accordingly, the graph is Eulerian. For more natural situation, each edge between two intersection vertices should be an alternating sequence of an edge and a vertex, starting and ending with an edge like with the maze structure of Sections 2 and 3. So the closed one stroke drawing is Eulerian given the above relation between the line drawing and Eulerian graph. A Lemma of the above theorem of Eulerian graph [19] states that if the graph G has all degrees even, then the edge set E can be partitioned into disjoint subsets E 1 , E 2 , …, E k , where each E i is the edge set of a cycle and = ⋃ . (1)

Contour Extraction and Selection of a Set of Cycles Covering the Entire Drawing
Applying the method of Section 3 to a binary drawn curve like Fig. 5 enables to extract the boundaries of the curve and construct the contour cycles ( Fig. 6) of maze structure [13]. The cycles in Fig. 6 are numbered as given by OpenCV [18]. The cycles cover every region segment surrounded by the original curve. That is, every region surrounded by the original curve is surrounded by one of the contour cycles. This means in turn that every part of the drawn curve is covered doubly by two different contour cycles. That is, every line part (center) of the original curve is put between two adjacent cycles.
Assume an ideal imaginary situation, where the drawn curve has zero width and then every part of the curve is overlapped by the two different cycles, the adjacent cycles. This means that there are two sets of cycles in Fig. 6, each of which constitutes Eulerian graph. What is more, the two cycle sets are different but occupy the same vertices and edges as a whole.
Consider the graph of the vertex set and the edge set such that the vertex set consists of the vertices representing the region surrounded by the drawn curve and the edge set consists of the edges which exist between the two vertices if the two regions (equivalent to the cycles) are adjacent. This graph can be called a region graph or a cycle graph because the region and its surrounding cycle essentially mean the same concept.

Fig. 6. Contour cycles constructed with region (cycle) numbers
Since the original curve graph is Eulerian, the region graph must be bipartite and 2-colorable. The cycles can be separated into two groups based on the adjacency of the cycles. This adjacency can be checked by the distance between the two cycles. Therefore, the bi-partitioning is possible.
In order to explain the adjacency between two cycles, it is necessary to define how the data of a cycle is organized. The data of cycle j is a sequence of coordinates defined as: c is the number of cycles extracted; and is the number of vertices on cycle j.
The adjacency checking can be done by a distance computation between two cycles. Two cycles j and i are considered, here, to be adjacent if there are more than a certain number, say n=5, of vertices on j such that the shortest distance from the vertex l on j to the cycle i is less than the overall average of the shortest distance ( ) multiplied by a certain number a (=2). The shortest distance from the vertex l on cycle j to cycle i, ( , , ), is defined as: If the size of S is larger than n, cycles j and i are considered adjacent.

Merge of Contour Cycles to Construct One Single Cycle
Previous section provides the method to obtain a set of cycles which cover the entire drawing. In Eulerian graph, the set of cycles constitute one single circuit which passes through all the edges, each only once but some vertices with some repetition. However, the actually extracted cycles are usually separate existence as shown in Fig. 6 or Figs. 7 (a) and (b). Therefore, there are usually no common intersection vertices between the actually extracted cycles. However, the intersection vertices are supposed to exist around the positions where the original curve intersects. By finding those places possible to construct one single cycle.
A single cycle can be obtained by merging the cycles in Fig.7 (a) also can be merged in to one single cycle as Fig. 11 (b). B of the original curve. If a part of the cycles is cut and removed then the two ends can be the start and the goal of the solution path of the maze. See partitioning is shown in Figs. 7 (a) partition X and (b) partition Y. Fig. 8 shows the bi lgorithm. Since the separation is based on the adjacency between two cycles, the two groups should occupy the same vertices and edges---looking perfectly alike assuming the original curve is of zero width. It is because that there are exactly same vertices and edges in the cycle sets in the extracted contours . 7 (a) and (b) were obtained from the actual drawing with curve width of 3 pixels. they look somewhat different. The shapes also may look different from the original curve. But the drawing is Eulerian enough so that bi-partitioning works well. If the experiment is done with curve width of 1 pixel, they should look more alike

Merge of Contour Cycles to Construct One Single Cycle
Previous section provides the method to obtain a set of cycles which cover the entire drawing. In Eulerian constitute one single circuit which passes through all the edges, each only once but some vertices with some repetition. However, the actually extracted cycles are usually separate existence as . 7 (a) and (b). Therefore, there are usually no common intersection vertices between the actually extracted cycles. However, the intersection vertices are supposed to exist around the positions where the original curve intersects. By finding those places and connecting the cycles repeatedly, it is possible to construct one single cycle. merging the cycles in Fig.7 (a) as in Fig. 11 (a); the cycles in Fig. 7 (b) also can be merged in to one single cycle as Fig. 11 (b). Both of those merged cycles go through all the parts of the original curve. If a part of the cycles is cut and removed then the two ends can be the start and the goal of the solution path of the maze. See Figs. 12 (a) and (b). The merging algorithm is given in Fig. 13.
; Article no. BJMCS.2015.217 458 . Fig. 8 shows the bilgorithm. Since the separation is based on the adjacency between two cycles, the two groups perfectly alike assuming the original curve is of zero e vertices and edges in the cycle sets in the extracted contours . 7 (a) and (b) were obtained from the actual drawing with curve width of 3 pixels.
original curve. But well. If the experiment is done with curve width of 1 pixel, they should look more alike. The experiments with narrower curves are demonstrated in later . 7 (a) and (b) correspond to the edge set of expression (1).
partitioned graph obtained by examining the adjacency between every two cycles of Fig. 6; 9. If one of these cycle sets are merged into one single cycle, then it can be the solution path of the maze which goes through every part of the originally two such merged cycles. Depending on the Previous section provides the method to obtain a set of cycles which cover the entire drawing. In Eulerian constitute one single circuit which passes through all the edges, each only once but some vertices with some repetition. However, the actually extracted cycles are usually separate existence as . 7 (a) and (b). Therefore, there are usually no common intersection vertices between the actually extracted cycles. However, the intersection vertices are supposed to exist around the positions and connecting the cycles repeatedly, it is 1 (a); the cycles in Fig. 7 (b), oth of those merged cycles go through all the parts of the original curve. If a part of the cycles is cut and removed then the two ends can be the start and the goal in Fig. 13. With a solution path for a maze, it is easy to construct the maze. Adding random dead end branches to the solution path, maze can be constructed. The final maze to be presented to a player is usually the one with walls thinned. Fig. 14

Final Procedure to Finish the Maze
With a solution path for a maze, it is easy to construct the maze. Adding random dead end branches to the solution path, maze can be constructed. The final maze to be presented to a player is usually the one with walls thinned. Fig. 14 illustrates one example (for partition X) of the final maze with walls thinned. The small red and green vertices are the points of the start and goal. Fig. 15 is with the solution path.
: a set of contour cycles extracted : partition sets of the bipartite graph, both sets are empty at the beginning; they will have the resulting vertices of bipartition.
Remove an arbitrary cycle j from R and put it into X.

Region (cycle) adjacency graph
With a solution path for a maze, it is easy to construct the maze. Adding random dead end branches to the solution path, maze can be constructed. The final maze to be presented to a player is usually the one with ) of the final maze with walls thinned. The s of the start and goal. Fig. 15 is with the solution path.
: partition sets of the bipartite graph, both sets are empty at the beginning; they will

Curves with Odd Degree V
Though it is not an ordinary one stroke curve drawing, a drawing with three curve lines extending from a crossing (Fig. 16) was tried. The contour cycles (Fig. 17) were semi-Eulerian), bipartite partitioning was not possible. Kurokawa;BJMCS,9(6)

Vertices
Though it is not an ordinary one stroke curve drawing, a drawing with three curve lines extending from a crossing (Fig. 16) was tried. The contour cycles (Fig. 17)  Find the nearest two points 5.
If the distance between Merge i to m---organizing one single cycle 6.
Remove i from R; 7.

More Curves Crossing at a Point (6 Degree Vertex)
A curve with the crossings of three lines at a point (Fig. 18) was tried. Since it is with all vertices having even degree, bipartite partitioning is expected to be pos which looks more similar to the original drawn curve, the line width was set to 2 pixels. Fig. 19 is the extracted contour cycles. Some cycles overlap, but they constitute independent different cycles is possible to make the separation (bi

More Curves Crossing at a Point (6 Degree Vertex)
A curve with the crossings of three lines at a point (Fig. 18) was tried. Since it is with all vertices having even degree, bipartite partitioning is expected to be possible. In order to try to obtain the maze solution path which looks more similar to the original drawn curve, the line width was set to 2 pixels. Fig. 19 is the extracted contour cycles. Some cycles overlap, but they constitute independent different cycles is possible to make the separation (bi-partitioning). Fig. 20

462
: partition X A curve with the crossings of three lines at a point (Fig. 18) was tried. Since it is with all vertices having sible. In order to try to obtain the maze solution path which looks more similar to the original drawn curve, the line width was set to 2 pixels. Fig. 19 is the extracted contour cycles. Some cycles overlap, but they constitute independent different cycles. Therefore, it partitioning). Fig. 20

More Complex Drawing with
A more complex drawing was tried (Fig. 23).

Drawing Curve with Line
Line drawing with line width of 1 pixel was tried (Fig. 28). Fig. 29 is the extracted contour cycles. Most parts of the cycles overlap. There are actually 8 cycles. Those cycles are independent and overlap. Figs. 30 (a) and (b) are the results of partitioning. There are 5 cycles in Fig  (b). But in those figures, some parts of the cycles overlap. Accordingly, the same merging procedure used in Kurokawa;BJMCS,9(6)

ine Width of 1 Pixel
Line drawing with line width of 1 pixel was tried (Fig. 28). Line drawing with line width of 1 pixel was tried (Fig. 28). Fig. 29 is the extracted contour cycles. Most parts of the cycles overlap. There are actually 8 cycles. Those cycles are independent and separate but . 30 (a) and 3 cycles in (b). But in those figures, some parts of the cycles overlap. Accordingly, the same merging procedure used in the previous subsection is not applicable. An additional procedure (cycle separation: elimination of overlapping) was introduced. Figs. 31 (a) and (b) are the results. Both of Figs. 31 (a) and (b) are sets of complete separate and non-overlapped cycles. Then the previous merge procedure becomes applicable for the merge to produce one single cycles. Figs. 32 (a) and (b) are the resulted single cycles. They look alike. At a glance, both look identical. This is very persuasive to say that there are two Eulerian graphs in the extracted contour cycles if the closed one stroke drawn curve is with zero width, which is virtually a mathematically defined graph. Figs. 33 and 34 are the final mazes, both with start and goal. They also look alike.  Fig. 6 shows the contour cycles which are two parallel paths in most parts. As long as the observation is concerned, the cycle line positions are inside the drawn lines but on the boundary between the drawn curve and the region for the white foreground lines, which are based on the software Open CV [18]. The above fact is true for different curve widths even for the width of one pixel, which is shown in Fig. 28. As long as a boundary exists, contour line is generated. Therefore one curve line always have two contour lines generated on both sides, possibly same positions. When the curve width is one pixel, most parts of the two lines overlap. Two contour cycle lines are presumably generated along the boundary even if its width is zero. They supposedly occupy the same positions. No such boundary actually exists, though. In Section 5, the adjacency between two cycles were checked by the distance between the two cycles. The criteria need not be very strict for most cases. But in the previous experiments, the overall average of shortest distance from a vertex on a cycle to another cycle as (4) is first computed. The values are different depending on the drawn curve especially due to the width of the curve. This distance was used for the criteria in deciding if two cycles are adjacent. The average of shortest distance for Figs. 6, 24 and 29, respectively, is as in Table 1. For the drawn curve line with width of 3 pixel, the boundary lines are two pixel apart on the picture array. When it is converted to the maze structure, the two pixels are apart 4 cells away. For line widths of 2 and 1, they are separate 2 and 0 pixels away, respectively. So the values on Table  1 are considered reasonable. And there is no inconsistency between looking and the computed values. For  Fig. 24, the nearest two pixels along the opposing boundary curves are mostly two cells away. But some may be overlapped. For Fig. 29, the two pixels are mostly overlapped but in some part, they are one or two pixels away. So values in Table 1 are reasonable.

Conclusion
There are two significant remarks for this conclusion. The first is about the theoretical matter and the second is the practical realization of the maze construction using the first.
1) The boundary contours extracted from the closed one stroke drawn curve virtually constitute two Eulerian circuits. They are essentially made up of the identical vertices and edges but constitute different circuits. They constitute two different edge cycle sets, each of which is made of disjoint edge cycle sets of Eulerian graph. The two sets can be separated or bi-partitioned based on the adjacency between the regions surrounded by the cycles. Adjacency is provided by the actually extracted contour cycles. Therefore, the each of the bi-partitioned cycle set can be converted to one single cycle, which is essentially the solution path of the maze. These two maze paths go through all parts of the originally drawn curve. 2) The above theory is demonstrated by a number of experiments---including the bi-partition algorithm and the merge procedure. The thinner the originally drawn curve, the closer looking to the original curve the solution path of the maze becomes.