# Algorithms Third Edition in C++ Part 5. Graph Algorithms (2006)

### CHAPTER EIGHTEEN

Graph Search

### 18.1 Exploring a Maze

It is instructive to think about the process of searching through a graph in terms of an equivalent problem that has a long and distinguished history (*see reference section*)—finding our way through a maze that consists of passages connected by intersections. This section presents a detailed study of a basic method for exploring every passage in any given maze. Some mazes can be handled with a simple rule, but most mazes require a more sophisticated strategy (see *Figure 18.1*). Using the terminology *maze* instead of *graph*,*passage* instead of *edge*, and *intersection* instead of*vertex* is making mere semantic distinctions, but, for the moment, doing so will help to give us an intuitive feel for the problem.

One trick for exploring a maze without getting lost that has been known since antiquity (dating back at least to the legend of Theseus and the Minotaur) is to unroll a ball of string behind us. The string guarantees that we can always find a way out, but we are also interested in being sure that we have explored every part of the maze, and we do not want to retrace our steps unless we have to. To accomplish these goals, we need some way to mark places that we have been. We could use the string for this purpose as well, but we use an alternative approach that models a computer implementation more closely.

We assume that there are lights, initially off, in every intersection, and doors, initially closed, at both ends of every passage. We further assume that the doors have windows and that the lights are sufficiently strong and the passages sufficiently straight that we can determine, by opening the door at one end of a passage, whether or not the intersection at the other end is lit (even if the door at the other end is closed). Our goals are to turn on all the lights and to open all the doors. To reach them, we need a set of rules to follow, systematically. The following maze-exploration strategy, which we refer to as *Trémaux* *exploration*, has been known at least since the nineteenth century (*see reference section*):

(*i*) If there are no closed doors at the current intersection, go to step (*iii*). Otherwise, open any closed door to any passage leading out of the current intersection (and leave it open).

(*ii*) If you can see that the intersection at the other end of that passage is already lighted, try another door at the current intersection (step (*i*)). Otherwise (if you can see that the intersection at the other end of the passage is dark), follow the passage to that intersection, unrolling the string as you go, turn on the light, and go to step (*i*).

(*iii*) If all the doors at the current intersection are open, check whether you are back at the start point. If so, stop. If not, use the string to go back down the passage that brought you to this intersection for the first time, rolling the string back up as you go, and look for another closed door there (that is, return to step (*i*)).

*Figures 18.2* and *18.3* depict a traversal of a sample graph and show that, indeed, every light is lit and every door is opened for that example. The figures depict just one of many possible outcomes of the exploration, because we are free to open the doors in any order at each intersection. Convincing ourselves that this method is always effective is an interesting exercise in mathematical induction.

**Figure 18.2 Trémaux maze exploration example**

In this diagram, places that we have not visited are shaded (dark) and places that we have visited are white (light). We assume that there are lights in the intersections, and that, when we have opened doors into lighted intersections on both ends of a passage, the passage is lighted. To explore the maze, we begin at 0 and take the passage to 2 (left, top). Then we proceed to 6, 4, 3, and 5, opening the doors to the passages, lighting the intersections as we proceed through them, and leaving a string trailing behind us (left). When we open the door that leads to 0 from 5, we can see that 0 is lighted, so we skip that passage (top right). Similarly, we skip the passage from 5 to 4 (right, second from top), leaving us with nowhere to go from 5 except back to 3 and then back to 4, rolling up our ball of string. When we open the doorway from the passage from 4 to 5, we can see that 5 is lighted through the open door at the other end, and we therefore skip that passage (right, bottom). We never walked down the passage connecting 4 and 5, but we lighted it by opening the doors at both ends.

**Figure 18.3 Trémaux maze exploration example (continued)**

Next, we proceed to 7 (top left), open the door to see that 0 is lighted (left, second from top), and then proceed to 1 (left, third from top). At this point, most of the maze is traversed, and we use our string to take us back to the beginning, moving from 1 to 7 to 4 to 6 to 2 to 0. Back at 0, we complete our exploration by checking the passages to 5 (right, second from bottom) and 7 (bottom right), leaving all passages and intersections lighted. Again, the passages connecting 0 to 5 and 0 to 7 are both lighted because we opened the doors at both ends, but we did not walk through them.

**Figure 18.4 Decomposing a maze**

To prove by induction that Trémaux exploration takes us everywhere in a maze (top), we break it into two smaller pieces, by removing all edges connecting the first intersection with any intersection that can be reached from the first passage without going back through the first intersection (bottom).

**Property 18.1** *When we use Trémaux* *maze exploration, we light all lights and open all doors in the maze and end up back where we started.*

*Proof*: To prove this assertion by induction, we first note that it holds, trivially, for a maze that contains one intersection and no passages—we just turn on the light. For any maze that contains more than one intersection, we assume the property to be true for all mazes with fewer intersections. It suffices to show that we visit all intersections, since we open all the doors at every intersection that we visit. Now, consider the first passage that we take from the first intersection, and divide the intersections into two subsets: (*i*) those that we can reach by taking that passage without returning to the start, and (*ii*) those that we *cannot* reach from that passage without returning to the start. Applying the inductive hypothesis, we know that we visit all intersections in (*i*) (ignoring any passages back to the start intersection, which is lit) and end up back at the start intersection. Then, applying the the inductive hypothesis again, we know that we visit all intersections in (*ii*) (ignoring the passages from the start to intersections in (*i*), which are lit). •

From the detailed example in *Figures 18.2* and *18.3*, we see that there are four different possible situations that arise for each passage that we consider taking:

(*i*) The passage is dark, so we take it.

(*ii*) The passage is the one that we used to enter (it has our string in it), so we use it to exit (and we roll up the string).

(*iii*) The door at the other end of the passage is closed (but the intersection is lit), so we skip the passage.

(*iv*) The door at the other end of the passage is open (and the intersection is lit), so we skip it.

The first and second situations apply to any passage that we traverse, first at one end and then at the other end. The third and fourth situations apply to any passage that we skip, first at one end and then at the other end. Next, we see how this perspective on maze exploration translates directly to graph search.

**Exercises**

• **18.1** Assume that intersections 6 and 7 (and all the hallways connected to them) are removed from the maze in *Figures 18.2* and *18.3*, and a hallway is added that connects 1 and 2. Show a Trémaux exploration of the resulting maze, in the style of *Figures 18.2* and *18.3*.

• **18.2** Which of the following could *not* be the order in which lights are turned on at the intersections during a Trémaux exploration of the maze depicted in *Figures 18.2* and *18.3*?

0-7-4-5-3-1-6-2

0-2-6-4-3-7-1-5

0-5-3-4-7-1-6-2

0-7-4-6-2-1-3-5

• **18.3** How many different ways are there to traverse the maze depicted in *Figures 18.2* and *18.3* with a Trémaux exploration?