Tutorial Backtracking, CMPT 115 Created by Svetlana Slavova, July by efr19747


									                                Tutorial “Backtracking”, CMPT 115
                               Created by Svetlana Slavova, July 2006

1. Basic information

●   Backtracking is an algorithmic technique that uses recursion to try all the possibilities
    in order to find the correct solution. Essentially, the idea is to try each possibility until
    you get the right one. During the search, if you try an alternative that doesn't work,
    you backtrack to the choice point, the place which presented you with different
    alternatives, and you try the next alternative. When you have exhausted the
    alternatives, you return to the previous choice point and try the next alternative there.
    If there are no more choice points, the search fails.
●   Conceptually, a backtracking algorithm does a depth-first search of a tree of possible
    (partial) solutions. Each choice is a node in the tree.

2. Solving a maze

                                     Fig 1 : Sample Maze

Consider a maze, shown in the diagram above, where a player has to decide how to get
from room A to room B. The player can move from room to room through the corridors
provided, but has no way of telling how many corridors a room is connected to until he
reaches that room. The following is a sample session showing how the player solves the

          The player moves from room A to room 2.
     1.   From here he can move either to rooms 1 or 3. He chooses 1.
     2.   Dead End. He returns to room 2, then goes on to room 3.
     3.   He has no choice but to move to room 4.
     4.   Given a choice between rooms 5 and 6, he chooses 6.
     5.   Another choice between 7 and 8, he chooses 7.
     6.   Dead End. He returns to 6 and chooses 8.
     7.   Dead End again. He returns to 6 and sees that he has exhausted his choices.
     8.   He goes back to room 4, and chooses room 5.
     9.   He sees room B, moves to it, and solves the maze.

                             Tutorial “Backtracking”, CMPT 115
                            Created by Svetlana Slavova, July 2006

The following is some pseudocode which implements the algorithm described above:
function CheckRoom(RoomNumber)
            if RoomNumber has been visited, return.
             for each room connected to this room
                   if ConnectedRoomNumber == B, the maze has been solved
                   else CheckRoom(ConnectedRoomNumber)
             end for
       end function

3. How backtracking works

                               Fig 2 : The maze as a tree

The maze can be represented as a tree, with room A at the root of the tree. The
backtracking algorithm discussed above is a tree-spanning algorithm. When it reaches a
node like room 2, where it has more than one path to choose from, it picks one path (to
room 1) which is a leaf node, or Dead End. Finding that it can proceed no more, it
unwinds to its previous execution, (back to room 2) and takes the other path, to room 3.
Such an algorithm is known as a depth-first spanning algorithm, because it tends to
follow a path right down to the bottom of the tree, before it gives up and unwinds to a
previous execution in order to choose a different path. Manually apply the CheckRoom
function above to the tree to get a feel of how the algorithm works.
Backtracking procedures are characterized by multiple recursive calls, typically within a
loop (see the CheckRoom function above). Unlike the simple recursion examples, the
backtracking procedure goes through multiple winding and unwinding stages as it loops
through the choices it has to make at every node. One of the most common applications
of backtracking is in the evaluation of moves for strategy games like tic-tac-toe or chess.

Example of a maze is given here: http://math.hws.edu/xJava/other/maze.html


To top