# Backtracking Algorithms

Topics: Backtracking, Minotaur, Algorithm Pages: 6 (1719 words) Published: June 4, 2012
Chapter 7 Backtracking Algorithms
Truth is not discovered by proofs but by exploration. It is always experimental. — Simone Weil, The New York Notebook, 1942

Objectives • • • • • •
To appreciate how backtracking can be used as a solution strategy. To recognize the problem domains for which backtracking strategies are appropriate. To understand how recursion applies to backtracking problems. To be able to implement recursive solutions to problems involving backtracking. To comprehend the minimax strategy as it applies to two-player games. To appreciate the importance of developing abstract solutions that can be applied to many different problem domains.

Backtracking Algorithms

– 238 –

For many real-world problems, the solution process consists of working your way through a sequence of decision points in which each choice leads you further along some path. If you make the correct set of choices, you end up at the solution. On the other hand, if you reach a dead end or otherwise discover that you have made an incorrect choice somewhere along the way, you have to backtrack to a previous decision point and try a different path. Algorithms that use this approach are called backtracking algorithms. If you think about a backtracking algorithm as the process of repeatedly exploring paths until you encounter the solution, the process appears to have an iterative character. As it happens, however, most problems of this form are easier to solve recursively. The fundamental recursive insight is simply this: a backtracking problem has a solution if and only if at least one of the smaller backtracking problems that results from making each possible initial choice has a solution. The examples in this chapter are designed to illustrate this process and demonstrate the power of recursion in this domain.

7.1 Solving a maze by recursive backtracking
Once upon a time, in the days of Greek mythology, the Mediterranean island of Crete was ruled by a tyrannical king named Minos. From time to time, Minos demanded tribute from the city of Athens in the form of young men and women, whom he would sacrifice to the Minotaur—a fearsome beast with the head of a bull and the body of a man. To house this deadly creature, Minos forced his servant Daedelus (the engineering genius who later escaped the island by constructing a set of wings) to build a vast underground labyrinth at Knossos. The young sacrifices from Athens would be led into the labyrinth, where they would be eaten by the Minotaur before they could find their way out. This tragedy continued until young Theseus of Athens volunteered to be one of the sacrifices. Following the advice of Minos’s daughter Ariadne, Theseus entered the labyrinth with a sword and a ball of string. After slaying the monster, Theseus was able to find his way back to the exit by unwinding the string as he went along. The right-hand rule Theseus’s strategy represents an algorithm for escaping from a maze, but not everyone in such a predicament is lucky enough to have a ball of string or an accomplice clever enough to suggest such an effective approach. Fortunately, there are other strategies for escaping from a maze. Of these strategies, the best known is called the right-hand rule, which can be expressed in the following pseudocode form: Put your right hand against a wall. while (you have not yet escaped from the maze) { Walk forward keeping your right hand on a wall. }

As you walk, the requirement that you keep your right hand touching the wall may force you to turn corners and occasionally retrace your steps. Even so, following the righthand rule guarantees that you will always be able to find an opening to the outside of any maze. To visualize the operation of the right-hand rule, imagine that Theseus has successfully dispatched the Minotaur and is now standing in the position marked by the first character in Theseus’s name, the Greek letter theta (Θ):

Backtracking Algorithms

– 239 –

Θ...