Science Fair Projects Ideas - Maze generation algorithm

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Maze generation algorithm

There are a number of different maze generation algorithms, that is, automated methods for the creation of mazes.

image:maze.png

The maze shown on the right has been generated by the modified version of Prim's algorithm, below. For the source code for a Java applet that was responsible, click on the maze.

Contents

Theory

A maze is basically a graph which presents a traversal challenge between two points. If the graph is not connected, then there are regions of the graph that are wasted because they do not contribute to the search space. If the graph contains loops, then there may be multiple paths between the chosen paths. Because of this, maze generation is often approached as generating a random spanning tree on a connected graph. Loops which can confound naive maze solvers may be introduced by adding random edges to the result during the course of the algorithm.

Common algorithms are based on search or minimal spanning tree algorithms for connected graphs, with edge weights chosen randomly. Because mazes are often approached from a different paradigm to traditional graph theory, different nomenclature is commonly used: graph edges not included in the resultant spanning tree are called "walls"; edges in the spanning tree are called "passages"; and vertices are typically called "cells" or "rooms". Although maze algorithms are often presented in the context of rectangular or hexagonal arrays, typically they perform equally well on any connected graph.

Depth first search

This algorithm is a randomized version of the depth first search algorithm.

  1. Start at a particular cell and call it the "exit."
  2. Mark the current cell as visited, and get a list of its neighbors. For each neighbor, starting with a randomly selected neighbor:
    1. If that neighbor hasn't been visited, remove the wall between this cell and that neighbor, and then recurse with that neighbor as the current cell.

If your computer architecture has a small stack and cannot effectively use recursion, you can store the backtracking information in the maze itself; this also provides a quick way to display a solution, by starting at any given point and backtracking to the exit.

Mazes generated with a depth-first search have a low branching factor and contain many long corridors, which makes depth-first a good algorithm for generating mazes in video games.

In mazes generated by that algorithm, it will typically be relatively easy to find the way to the square that was first picked at the beginning of the algorithm, since most paths lead to or from there, but hard to find the way out.

Randomized Kruskal's Algorithm

This algorithm is simply a randomized version of Kruskal's algorithm.

  1. Create a list of all walls, and create a set for each cell, each containing just that one cell.
  2. For each wall, in some random order:
    1. If the cells divided by this wall belong to distinct sets:
      1. Remove the current wall.
      2. Join the sets of the formerly divided cells.

There are several data structures that can be used to model the sets of cells. An efficient implementation can perform each union and find operation on two sets in constant amortized time, so the running time of this algorithm is proportional to the number of walls available to the maze.

It matters little whether the list of walls is initially randomized or if a wall is randomly chosen from a nonrandom list, either way is just as easy to code.

Because the effect of this algorithm is to produce a minimal spanning tree from a graph with equally-weighted edges, it tends to produce regular patterns which are fairly easy to solve.

Randomized Prim's Algorithm

This algorithm is a randomized version of Prim's algorithm.

  1. Pick a cell, mark it as part of the maze. Add the walls of the cell to the wall list.
  2. While there are walls in the list:
    1. Pick a random wall from the list, and make it a passage.
    2. Mark the cell on the opposite side as part of the maze.
    3. Add the neighbouring walls of the cell to the wall list.

Like the depth-first algorithm, it will usually be relatively easy to find the way to the starting cell, but hard to find the way anywhere else.

Note that simply running classical Prim's on a graph with random weights would create mazes stylistically identical to Kruskal's, because they are both minimal spanning tree algorithms. Instead, this algorithm introduces stylistic variation because the edges closer to the starting point have a lower effective weight.

Modified version

Although the classical Prim's algorithm keeps a list of edges, for maze generation we could instead maintain a list of adjacent cells. If the randomly chosen cell has multiple edges that connect it to the existing maze, select one of these edges at random. This will tend to branch slightly more than the edge-based version above.

Small-memory algorithm

Other algorithms exist that require only enough memory to store one line of a 2D maze or one plane of a 3D maze; they work by storing which cells in the current line are connected through cells in the previous lines. This algorithm never knocks down a wall between any two cells already connected.

External links

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice