BackTracking
CS255
N-Queens
The object is to place queens on a chess board in such as way as no queen can capture another one in a single move
Recall that a queen can move horz, vert, or diagonally an infinite distance
This implies that no two queens can be on the same row, col, or diagonal
We usually want to know how many different placements there are
4-Queens
Lets take a look at the simple problem of placing queens 4 queens on a 4x4 board The brute-force solution is to place the first queen, then the second, third, and forth
After all are placed we determine if they are placed legally
There are 16 spots for the first queen, 15 for the second, etc.
Leading to 16*15*14*13 = 43,680 different combinations
Obviously this isnt a good way to solve the problem
4-Queens
First lets use the fact that no two queens can be in the same col to help us
That means we get to place a queen in each col
So we can place the first queen into the first col, the second into the second, etc. This cuts down on the amount of work
Now there are 4 spots for the first queen, 4 spots for the second, etc.
4*4*4*4 = 256 different combinations
4-Queens
However, we can still do better because as we place each queen we can look at the previous queens we have placed to make sure our new queen is not in the same row or diagonal as a previously place queen Then we could use a Greedy-like strategy to select the next valid position for each col
4-Queens
Q Q x
Q Q
Q x Q x
Q x Q x Q
Q x Q Q x x
4-Queens
Q Q x x Q x x Q Q x x x Q x x
4-Queens
So now what do we do? Well, this is very much like solving a maze
As you walk though the maze you have to make a series of choices If one of your choices leads to a dead end, you need to back up to the last choice you made and take a different route
That is, you need to change one of your earlier selections
Eventually you will find your way out of the maze
4-Queens
Q Q x x x Q x x Q x x Q Q x x Q x x Q x Q x x Q Q x x Q Q x x Q x Q Q x x Q Q Q x x x
4-Queens
Q x Q x Q x Q x x x Q x x Q x Q x x x Q Q x x x Q x Q x x x Q x x Q x Q x Q x x Q x x
Q x
4-Queens
Q x Q x Q Q x Q x x x Q Q x x Q x Q x x x Q
Q Q x x x
4-Queens
Q x Q x x x Q Q Q x Q x Q x x Q x Q x Q Q x x x x Q x
4-Queens
This type of problem is often viewed as a state-space tree
A tree of all the states that the problem can be in
We start with an empty board state at the root and try to work our way down to a leaf node
Leaf nodes are completed boards
4-Queens
1 1 1 2 2 3 3 4 4 1 3 2 4 3 1 4 2 2 3 1 4
Graph Coloring
Graph coloring is the problem of coloring each vertex in a graph such that no two adjacent vertices are the same color Some direct examples:
Map coloring Register assignment
Graph Coloring
The same issues apply as in N-Queens
We dont want to simply pick all subsets
Way too many
We want to prune the state-space tree as soon as we find something that wont work
This implies that we need a sequence of vertices to color As we color the next vertex we need to make sure it doesnt conflict with any of its previously colored neighbors
We may need to backtrack
Graph Coloring
As an example:
The vertices are enumerated in order A-F The colors are given in order: R, G, B
B A F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
A B F
C D
Graph Coloring
C D
Depth-First Search
One can view backtracking as a Depth-First Search through the State-Space
We start our search at the root We end our search when we find a valid leaf node
A Depth-First Search expands the deepest node next
A child children will be expanded before the childs siblings are expanded
This type of search get you to the leaf nodes as fast as you can How do you implement a Depth-First Search?
Backtracking Framework
We will try and build a generic backtracking framework that works with problems States
All it should need to work with is States
State should be generic and not married to any specific problem States should be able to:
Tell us if it has more children Produce the next child Tell us if it is solved Tell us if it is feasible
(In class design of the State, NQueens problem specific, and Backtracking framework classes)
Depth vs. Breadth First Search
A Breadth first search expands all the children at a particular level before advancing to the next level
How can a breadth first search be implemented? Which is better: a depth or breadth first search?
Worst case Big-O?
Optimization Problems
The previous examples simply found all the solutions (Nqueens) or any single solution (Graph Coloring) What if we have a cost associated with each solution and we want to find the optimal solution
As an example take Weighed Graph Coloring
R $2, G $3, B $5, Y $2 A fully connected graph of 3 vertices
Note that some of the connections on the next slide are missing to keep the picture fairly clean
Weighted Graph Coloring
Weighted Graph Coloring
Obviously this approach will take a long time since we need to look at every possible combination Is there any way we can prune even more nodes from the tree?
Besides the non-feasible ones that we already prune
What about a upper or lower bound on a node?
An estimate of the potential cost of expanding the node
Bounding
A bound on a node is a guarantee that any solution obtained from expanding the node will be:
Greater than some number (lower bound) Or less than some number (upper bound)
If we are looking for a minimal optimal, as we are in weighted graph coloring, then we need a lower bound
For example, if the best solution we have found so far has a cost of 12 and the lower bound on a node is 15 then there is no point in expanding the node
The node cannot lead to anything better than a 15
Bounding
We can compute a lower bound for weighted graph color in the following way:
The actual cost of getting to the node Plus a bound on the future cost
Min weight color * number of nodes still to color
That is, the future cost cannot be any better than this
(Interactively draw state space graph on board complete with bounds as given above)
Bounding
Recall that we could either perform a depth-first or a breadth-first search
Without bounding, it didnt matter which one we used because we had to expand the entire tree to find the optimal solution Does it matter with bounding?
Hint: think about when you can prune via bounding
(Interactively draw the breadth-first bounding case on the board and compare it with depth-first)
Bounding
We prune (via bounding) when:
(currentBestSolutionCost <= nodeBound)
This tells us that we get more pruning if:
The currentBestSolution is low And the nodeBound is high
So we want to find a low solution quickly and we want the highest possible lower bound
One has to factor in the extra computation cost of computing higher lower bounds vs. the expected pruning savings
Best-first Search
Depth-first search found a solution quickly Breadth-first search found all solutions at about the same time (but slowly) Best-first search finds a good solution fairly quickly
Not as quickly as depth-first But the solution is often better which will increase the pruning via bounding
Best-first Search
Best-first search expands the node with the best bounds next (Interactively show a best-first state-space tree on weighted graph coloring) How would you implement a best-first search?
Depth-first is a stack Breadth-first is a queue Best-first is a ???
Traveling Salesperson Problem
This is a classic CS problem Given a graph (cities), and weights on the edges (distances) find a minimum weight tour of the cities
Start in a particular city Visit all other cities (exactly once each) Return to the starting city
Cannot be done by brute-force as this is worstcase exponential or worse running time
So we will look to backtracking with pruning to make it run in a reasonable amount of time in most cases
Traveling Salesperson Problem
We will build our state space by:
Having our children be all the potential cities we can go to next Having the depth of the tree be equal to the number of cities in the graph
we need to visit each city exactly once
So given a fully connected set of 5 nodes we have the following state space
only partially completed
Traveling Salesperson Problem
A A A B B C C D D B E E C D E
Traveling Salesperson Problem
Now we need to add bounding to this problem
It is a minimization problem so we need to find a lower bound
We can use:
The current cost of getting to the node plus An underestimate of the future cost of going through the rest of the cities
The obvious choice is to find the minimum weight edge in the graph and multiply that edge weight by the number of remaining nodes to travel through
Traveling Salesperson Problem
As an example assume we have the given adjacency matrix If we started at node A and have just traveled to node B then we need to compute the bound for node B
Cost 14 to get from A to B Minimum weight in matrix is 2 times 4 more legs to go to get back to node A = 8 For a grand total of 14 + 8 = 22
0 14 4 11 18
14 0 5 7 7
4 7 0 9 17
10 8 7 0 4
20 7 16 2 0
Traveling Salesperson Problem
0 14 4 10 20 Recall that if we can make the lower bound higher then we will get more 14 0 7 8 7 pruning Note that in order to complete the tour 4 5 0 7 16 we need to leave node B, C, D, and E
The min edge we can take leaving B is min(14, 7, 8, 7) = 7 Similarly, C=4, D=2, E=4
11 7 9 0 18 7 17 4
2 0
This implies that at best the future underestimate can be 7+4+2+4=17 17 + current cost of 14 = 31
This is much higher than 8 + 14 = 22