Data Structures and Algorithms

Backtracking Algorithms with Search Space Pruning; TSP and Branch and Bound

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.

Review: Backtracking vs. Branch and Bound

Here are two common algorithm problems:

Constraint Satisfaction

  • Can we satisfy all given constraints?
  • If yes, how do we satisfy them? Need a specific solution
  • May have more than one solution
  • Can stop early

Optimization

  • "If we have multiple solutions, find the best one!"
  • Optimize some objective function subject to the constraints
  • Cannot stop early

Backtracking is to constraint satisfaction as branch and bound is to optimization.

Backtracking: General Form

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:

  • First, there is solution(v): checking to see if all constraints are satisfied.
  • There is promising(v): seeing if the node does not break all constraints
  • checknode(v): called only if constraint is satisfied and node is not solution.

N Queens

  • When N = 1, 1 queen can be placed on a 1x1 board so that it doesn't threaten another.
  • When N = 2, 2 queens cannot be placed on a 2x2 board because all configurations would put both in danger.
  • When N = 3, this is more complicated. You still can't do it though.
  • When N = 4, this is possible. You put each queen on an edge, one space away from each corner.

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.

Branch and Bound and TSP

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

Branch and bound is the idea of backtracking extended to optimization problems. You are minimizing a function with this useful property:

  • A partial solution is pruned if its cost >= cost of best known complete solution.

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.