Back Tracking
Back Tracking
Back Tracking
By
Riaz Ahmad
COMSATS WAH CANTT
Backtracking (animation)
dead end
?
dead end
start
dead end
dead end
dead end
?
success!
Backtracking
Solving a maze
Backtracking Algorithm
4.
Consider a maze
Solving a maze
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 approach the player uses to solve the maze could be like this:
The player moves from room A to room 2.
From here he can move either to rooms 1 or 3. He chooses 1.
Dead End. He returns to room 2, then goes on to room 3.
He has no choice but to move to room 4.
Given a choice between rooms 5 and 6, he chooses 6.
Another choice between 7 and 8, he chooses 7.
Dead End. He returns to 6 and chooses 8.
Dead End again. He returns to 6 and sees that he has exhausted his choices.
He goes back to room 4, and chooses room 5.
He sees room B, moves to it, and solves the maze.
10
The sample maze, like all mazes, is essentially 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 it's 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).
a 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 chess.
We will look at a much simpler game, tic-tac-toe, and see how a backtracking strategy
can be used to ensure that we can never lose the game.
12
13
Coloring a map
14
Return failure
15
16
Data structures
int map[][];
void createMap() {
map = new int[7][];
map[0] = new int[] {
map[1] = new int[] {
map[2] = new int[] {
map[3] = new int[] {
map[4] = new int[] {
map[5] = new int[] {
map[6] = new int[] {
}
1
4
2 3
5
1,
0,
0,
2,
0,
2,
2,
4,
4,
4,
4,
1,
6,
3,
2, 5 };
6, 5 };
3, 6, 5 };
6 };
6, 3, 2 };
1, 0 };
4, 1, 5 };
18
final
final
final
final
final
int
int
int
int
int
NONE = 0;
RED = 1;
YELLOW = 2;
GREEN = 3;
BLUE = 4;
19
20
22
23
Solving a puzzle
24
What is searching?
If you want to go from Point A to Point B, you are employing
some kind of search. For a direction finder, going from Point A to
Point B literally means finding a path between where you are
now and your intended destination.
For a chess game, Point A to Point B might be two points
between its current position and its position 5 moves from now.
Searching falls under Artificial Intelligence (AI). A major goal of
AI is to give computers the ability to think, or in other words,
mimic human behavior. The problem is, unfortunately, computers
don't function in the same way our minds do. They require a
series of well-reasoned out steps before finding a solution. Your
goal, then, is to take a complicated task and convert it into
simpler steps that your computer can handle.
25
Search representation
First, we need a representation of how our search problem will
exist. The following is an example of our search tree
In our above graph, the path connections are not two-way. All
paths go only from top to bottom
26
27
If the removed node is our destination, break out of the loop, add the
node to our Closed list, and return the value of our Closed list.
If the removed node is not our destination, continue the loop (go to Step
c).
28
Step 0
Let's start with our root/goal node:
I will be using two lists to keep track of what we are
doing - an Open list and a Closed List. An Open list
keeps track of what you need to do, and the Closed List
keeps track of what you have already done.
Open List: A
Closed List: <empty>
29
Step 1
Steps 2
Our Open list contains two items. For depth first search and
breadth first search, you always explore the first item from our
Open list. The first item in our Open list is the B node. B is not
our destination, so let's explore its neighbors:
31
Step3
32
Step 4
We now expand the E node from our Open list. E is not our destination, so we
explore its neighbors and find out that it contains the neighbors F and G.
Remember, F is our target, but we don't stop here though. Despite F being on
our path, we only end when we are about to expand our target Node - F in this
case:
Our Open list will have the E node removed and the F and G nodes added. The
removed E node will be added to our Closed List:
Open List: F, G, C
Closed List: A, B, D, E
33
Step 5
We remove F from our Open list and add it to our Closed List. Since we are at
our destination, there is no need to expand F in order to find its neighbors. Our
final Open and Closed Lists contain the following data:
Open List: G, C
Closed List: A, B, D, E, F
The final path taken by our depth first search method is what the final value of
our Closed List is: A, B, D, E, F
34
35
If the removed node is our destination, break out of the loop, add the node to
our Closed list, and return the value of our Closed list.
If the removed node is not our destination, continue the loop (go to Step c).
36