The n-queens problem states that:
Say you have 8 queens on a board. Is there a way to place those 8 queens on an 8x8 board such that none can directly attack each other? More generally, can you place N queens on an NxN board such that no queens attack each other?
You can use backtracking to solve this.
Here are two common algorithm problems:
Backtracking is to constraint satisfaction as branch and bound is to optimization.
type checknode(node v)
if (promising(v))
if (solution(v))
write solution
else
for each child node u of v
checknode(u)
There are three "pseudo-functions" in here:
solution(v)
: checking to see if all constraints are satisfied.promising(v)
: seeing if the node does not break all constraintschecknode(v)
: called only if constraint is satisfied and node is not solution.Let's talk about a recursive solution for this. First, note that there has to be exactly one queen on each row.
When you insert a queen on the first row, you can put it anywhere. Easy. Now, when you put the next queen in the second row, let's say you put it in the first (or second) column. Both of these configurations would not work, since the other queen could attack it. Now, you don't have to search any further configurations with those two queens in those positions. That's the beauty of backtracking.
On eight queens, the search space has 16 million different possibilities. There are 92 solutions to the eight queens problem. Using backtracking, you only have to look at around 15,000 branches.
In summary, backtracking lets us look at every possible state in the tree, but in a smart way: it lets us cut off anything we don't want to consider.
In the TSP, you're a salesperson and looking to sell something in a bunch of cities and come back in the shortest path. We're going to look at branch and bound through the lens of the Traveling Salesperson Problem.
Given a graph , find a Hamiltonian Cycle (a cycle that traverses each node exactly once) with the least weight. No vertex (except first/last) may appear twice.
Branch and bound is the idea of backtracking extended to optimization problems. You are minimizing a function with this useful property:
If the cost of a partial solution is too big, drop this partial solution.
The efficiency of B&B is based on "bounding away" (aka, "pruning") unpromising partial solutions. The earlier you know a solution is not promising, the less time you spend on it. The more accurately you can compute partial costs, the earlier you can bound. Sometimes it's worth spending extra effort to compute better bounds.
It turns out that TSP is an NP-hard problem.