CS 2 (Winter 2025) Lab 05: Mazes

This lab focuses on implementing both a recursive and an iterative depth-first search to solve mazes.

Setup

Register for the lab using grinch: https://register.caltech.codes and clone the repository as explained in the setup instructions.

Goals and Outcomes

By the end of this lab, you will…

Depth-First Search

Depth-First Search (DFS) is a powerful algorithm for traversing (or searching) through deep layered data structures. Essentially, the idea of the algorithm is this:

This search is “Depth-First” because it travels as deep as it can before it backtracks at all; you keep exploring deeper into the data structure until you can`t explore any deeper. This is the same idea that was brought up in lecture when we wrote a program to find the set of all words that could be created by mutating one letter of an input word at a time.

In this lab we will be implementing both a recursive and an iterative DFS to solve mazes!

Maze Interface

We can think of each point in a maze as a node, where traversable paths are represented by connections between nodes. These connections are represented by the “parent” and “children” of each node. The parent represents which node the current node was reached from and the children represent which nodes can be reached from the current node. If a node has no children other than its parent, we know we’ve reached a dead end in the maze. In this lab, we will be working with a simple maze and point interface and implementing DFS solvers to complete mazes. You can find the full implementations in Maze.java and Point.java, but here`s what you really need to know:

getChildren Helper Method

Let’s start by writing a helper method that interacts directly with the maze interface. It would be very convenient to have a method that returns an array of all of the children of a given Point (i.e. all of the points that we can move to from a given point).

public static Point[] getChildren(Point point)

Returns a Point array containing all children of a given point.


You will need to interact with the north[][], east[][], south[][], and west[][] arrays to decide where you can move from the given point. Also, make sure to set each new point’s parent to the current point!

Recursive Depth-First Search

Now that we have some helper methods, we’re ready to write our first DFS! It is easier to write a Depth-First Search recursively, so we will start with that. Before we get started, here’s a few things you should know:

Iterative Depth-First Search

It is also possible to implement the DFS algorithm iteratively! The stack data structure fits perfectly for this task; we can iteratively implement DFS by continuously pushing all children of the current node on to the stack, then popping off the top of the stack to be the new current node. Because the next level of children gets pushed on top of the other children from the previous level, we explore deeper and deeper into the maze before we backtrack to other children! Here are a few quick notes:

Checking the Results on gitlab