Branch and Bound: Classic Problems
This lesson applies the general scheme of branch and bound to several problems. In each one, the work consists of answering the same three questions:
- What is a state, and what are its children?
- What is the heuristic (the lower bound, for minimization) that orders the live nodes and decides what to prune?
- What is the initial pruning limit?
- How to define states, optimistic bounds, and initial limits for six classic problems: task assignment, 0/1 knapsack, sliding puzzles, rectangle placement, N-queens, and the traveling salesman.
- Why a tight bound (such as the fractional greedy bound for 0/1 knapsack) can cut the number of expanded nodes by orders of magnitude.
- How admissible heuristics (, Manhattan distance) and parity reachability apply to the 8/15-puzzle.
- How to mirror minimization bounds into upper bounds for maximization problems.
| Problem | Type | Heuristic | What it illustrates |
|---|---|---|---|
| Task assignment | Minimization | Cost so far + cheapest remaining cost of each column | A complete trace; initial limit |
| 0/1 knapsack | Maximization | Value so far + fractional (greedy) completion | Mirrored bounds; the power of a tight bound |
| Sliding puzzles | Minimum number of moves | Moves made + Manhattan distance | Reachability test; repeated states; |
| Rectangle placement | Minimization | Area of the bounding box so far | Geometric states |
| N-queens | First solution | Queens left to place, then diagonal length | B&B used to find one solution fast |
| Traveling salesman | Minimization | Length so far + cheapest exit of each city still to leave | A bound for a classic NP-hard problem |
Task assignment
Problem. There are agents and tasks. Each agent performs exactly one task and each task is performed by exactly one agent. The cost of agent performing task is . Find the assignment with minimum total cost. (This problem was solved by backtracking in the previous lesson; now we solve it with branch and bound.)
We use this cost matrix, with agents and tasks 1 to 4:
| Agent \ Task | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| a | 11 | 12 | 18 | 40 |
| b | 14 | 15 | 13 | 22 |
| c | 11 | 17 | 19 | 23 |
| d | 17 | 14 | 20 | 28 |
Design
- State: the tasks assigned to the first agents. Level decides the task of agent , so each node has one child per task still free. When only one agent is left, its task is forced, so the last two agents are decided together.
- Heuristic (lower bound): the sum of the costs already assigned, plus, for each task still free, the minimum cost in its column among the agents still free. Each remaining task will be done by someone, and at best by its cheapest remaining agent, so this never overestimates. (It may be too optimistic: the same agent could be the cheapest for two tasks.)
- Initial pruning limit: the cheaper of the two diagonals of the matrix. Each diagonal is a valid assignment (agent gets task , or task ), so the optimum cannot be worse:
For example, the state "a gets task 1" has heuristic (assigned) (column 2) (column 3) (column 4) .
Trace
The live nodes are kept in a priority queue ordered by heuristic. The search proceeds as follows (the limit is shown after each step):
| Step | Node expanded | Children generated (heuristic) | Limit |
|---|---|---|---|
| 1 | root | a1 (60), a2 (58), a3 (65), a4 (78, pruned: 78 ≥ 73) | 73 |
| 2 | a2 (58) | b1 (68), b3 (59), b4 (64) | 73 |
| 3 | a2 b3 (59) | c1 d4: solution, cost 64; c4 d1: solution, cost 65 | 64 |
| 4 | a1 (60) | b2 (68, pruned), b3 (61), b4 (66, pruned) | 64 |
| 5 | a1 b3 (61) | c2 d4: solution, cost 69; c4 d2: solution, cost 61 | 61 |
| 6 | The best live node is a2 b4 (64) ≥ 61: stop | 61 |
The optimal solution is (a, 1), (b, 3), (c, 4), (d, 2), with cost .
Only 4 complete assignments were generated (6 if we count the two diagonals used for the initial limit), instead of the that exhaustive backtracking examines. Nodes a3, a4, a2 b1, a1 b2 and a1 b4 were never expanded.
At scale
On random matrices with costs between 1 and 99, we compared this B&B with the backtracking algorithm of the previous chapter (which prunes only when the cost so far reaches the best cost):
| Backtracking: recursive calls | B&B: nodes expanded | B&B: nodes generated | |
|---|---|---|---|
| 10 | 12,739 | 218 | 1,420 |
| 12 | 77,412 | 69 | 565 |
| 14 | 194,179 | 41 | 332 |
Both return the same optimum. The difference comes from the estimate of the future cost: backtracking only knows what has already been spent, while the B&B bound also accounts for what must still be spent, so it detects hopeless branches much earlier. For a walkthrough of the same technique, see Assignment Problem using Branch and Bound.
ProofThe bounds used in the examples are valid0/1 knapsack
Problem. We have objects with weights and values , and a knapsack with capacity . Choose objects (each one entirely or not at all) to maximize the total value without exceeding the capacity. We solved it with dynamic programming in pseudo-polynomial time; branch and bound solves it without depending on the size of .
Design
- State: decisions taken for the first objects (take it or leave it), with the weight and value accumulated. Each node has at most two children.
- This is a maximization problem, so everything is mirrored: the bound is an optimistic upper bound on the value reachable from the node, the benchmark is the best value found so far, and a node is pruned when its bound is less than or equal to that value.
- Bound: sort the objects by value per unit of weight , decreasing. From the current state, keep adding the remaining objects in that order while they fit, and fill the remaining capacity with a fraction of the next one. This is the greedy solution of the fractional knapsack, which is optimal for the relaxed problem, so it can never be worse than the true best completion.
Example
Use the instance from the dynamic programming lesson: , and three objects with weights and values (ratios , , , already sorted).
- Root: take object 1 (weight 6, value 8), 4 units of capacity left, take of object 2 (value 4): bound .
- Expand the root:
- "take 1": value 8, bound . The best value so far becomes 8.
- "leave 1": objects 2 and 3 fit: bound .
- Expand "take 1" (12). Object 2 does not fit. Leaving it gives bound ; expanding that, object 3 does not fit either, and the complete solution has value 8.
- Expand "leave 1" (its bound, 10, is greater than 8). "Take 2": value 5, bound 10. "Leave 2": bound 5 ≤ 8, pruned.
- Expand "take 2" (10). "Take 3": value 10, which becomes the best value. "Leave 3": bound 5, pruned.
- No live node can beat 10: stop. The optimum is objects 2 and 3, value 10.
The example also shows that the first branch explored (the one with the most attractive bound) does not always contain the optimum: the fractional bound of "take 1" was 12, but its true best completion is only 8. The bound was optimistic, as required, and the search corrected itself.
🧠 Checkpoint: Compute the bound
Question: A knapsack with capacity and three objects with weights 4, 5, 3 and values 12, 10, 3 (ratios 3, 2, 1, already sorted). What is the bound of the root? What is the optimum?
Answer: Root: take object 1 (weight 4, value 12), 4 units left, and of object 2 (value 8): bound 20. The optimum is objects 1 and 3 (weight 7), value 15; objects 1 and 2 do not fit together. The bound is optimistic, as it must be.
The power of a tight bound
On 30 random objects (weights between 10 and 99, capacity half of the total weight):
| Algorithm | Nodes or calls | Time |
|---|---|---|
| Backtracking, pruning only objects that do not fit | about 1,207 million calls | 2.3 s |
| Backtracking + fractional bound | 251 calls | 0.2 ms |
| Best-first B&B + fractional bound | 122 nodes expanded | 1.6 ms |
All three return the same value. The bound is responsible for almost all of the improvement; the best-first order then halves the number of nodes, at the price of creating node objects and managing a priority queue. On instances with 40 and 50 objects, the bounded versions still need only a few hundred nodes.
ProofThe bounds used in the examples are validSliding puzzles
Problem. The 15-puzzle is played on a board with 15 numbered tiles and one empty cell. A move slides a tile adjacent to the empty cell into it (equivalently, the empty cell moves to one of its up to four neighbors). Pieces cannot be lifted. Starting from a scrambled board, reach the goal state with the tiles in order and the empty cell in the last position, using as few moves as possible. The 8-puzzle is the same game on a board.
The state space
The board can be in configurations. Only half of them can be reached from a given initial state, but that is still about states: building and exploring the whole tree is unacceptable. Before searching, we should know whether the goal is reachable at all.
Reachability test
Number the cells from 1 to 16, row by row, and treat the empty cell as tile 16. Define:
- : the cell where tile is in the initial state;
- : the number of tiles such that (smaller tiles that appear after tile );
- if the empty cell is in one of the shaded cells of a checkerboard pattern (cells 2, 4, 5, 7, 10, 12, 13 and 15), and otherwise.
Then the goal state is reachable if and only if
The reason is a parity argument: every move changes the parity of the number of inversions (horizontal moves by exactly one inversion, vertical moves by an odd number) and also changes the color of the square where the empty cell is, so the parity of the total never changes. The goal has sum 0 and .
Example 1. For the board
the values are , , , , and , all the others 0. The sum is 37, and the empty cell is in cell 6, which is not shaded (). The total, 37, is odd: the goal cannot be reached, and the search should not even start.
Example 2. For the board
and , so the sum is 8, with (cell 11 is not shaded). The total is even: the goal is reachable (in fact, with 2 moves). The initial state shown at the beginning of this section is also reachable (sum 64, ).
For the 8-puzzle (odd board width), the rule is simpler: the goal is reachable if and only if the number of inversions among the 8 tiles, ignoring the empty cell, is even.
ProofThe parity condition of the 15-puzzle🧠 Checkpoint: Reachable or not?
Question: Is the goal reachable from the 8-puzzle board 1 2 3 / 4 5 6 / 8 7 _ (the empty cell last)?
Answer: No. Ignoring the empty cell, the tiles read 1 2 3 4 5 6 8 7, with a single inversion (8 before 7). The number is odd, so the goal is unreachable: no search is needed to know it.
Heuristics
Two classic estimates of how far a board is from the goal:
- Misplaced tiles: the number of tiles that are not in their final cell.
- Manhattan distance: the sum, over all tiles, of the number of rows plus columns that separate each tile from its final cell.
Both are admissible: each misplaced tile needs at least one move, and each move changes the Manhattan distance of only one tile by one. The empty cell must not be counted, or the estimate may exceed the true number of moves. Manhattan distance is always at least as large as the number of misplaced tiles, so it is the tighter (better) of the two.
Ordering the live nodes:
To find the solution with the fewest moves, the priority of a board must be
where is the number of moves already made from the initial state and an admissible heuristic. This is the A* algorithm described in the previous lesson. The B&B must also discard repeated states: otherwise it would keep generating boards it has already seen, moving tiles back and forth.
We solved 200 random solvable 8-puzzles with four strategies:
| Strategy | Average moves in the solution | Average boards expanded |
|---|---|---|
| Breadth-first (FIFO queue) | 22.3 (optimal) | 83,701 |
| Best-first, + misplaced tiles | 22.3 (optimal) | 15,682 |
| Best-first, + Manhattan distance | 22.3 (optimal) | 1,266 |
| Best-first, Manhattan distance only (no ) | 64.7 | 292 |
The three strategies that include always find the shortest solution, and the better the heuristic, the fewer boards they expand. The last one expands fewer boards still, but its solutions are almost three times longer than necessary: in 194 of the 200 puzzles it returned a non-optimal solution, in one case with 142 extra moves. Forgetting turns branch and bound into a greedy search.
Rectangle placement
Problem. We have flat rectangular pieces , where piece measures . We want to place them on a flat board, without overlapping, so that the area of the rectangle that encloses all of them is minimum. A real application is arranging boxes in a warehouse with robotic arms, or cutting pieces from a sheet of material with minimum waste.
For example, with , and , different arrangements need enclosing boxes of , , or . The last one is optimal: the pieces have a total area of , so they fill the square without gaps (the piece in a corner, the piece standing next to it, and the piece along the remaining side).
Design
- State: a matrix of integers representing the board, where 0 marks a free cell and a value marks a cell occupied by piece . At step the next piece not yet placed is added.
- Children: one child for each valid position of the next piece, both horizontal and vertical. A position is valid if it does not overlap the pieces already placed and is adjacent to at least one of them (placing a piece far away can only make the box bigger).
- Solution: a node at level (all pieces placed). The problem always has a solution if the board is large enough.
- Bound: the area of the enclosing box of the pieces placed so far can only grow when more pieces are added, so it is a valid lower bound. A node is pruned when that area already reaches the area of the best solution found.
A stronger bound uses the fact that the final box must also contain the area of the pieces not yet placed: .
N-queens
Branch and bound is not only for optimization. Here it is used to find one placement of non-attacking queens (the problem solved by backtracking) as fast as possible. Only states in which no queen attacks another are generated.
Branching heuristic
- Develop first the state with more queens placed: it is closer to a solution.
- In case of a tie, prefer the state whose last queen is on a cell whose longest diagonal is the shortest. A queen on a short diagonal attacks fewer cells, so it leaves more room for the remaining queens.
Let be the length of the longest diagonal passing through cell . For a board:
| 5 | 4 | 3 | 4 | 5 |
| 4 | 5 | 4 | 5 | 4 |
| 3 | 4 | 5 | 4 | 3 |
| 4 | 5 | 4 | 5 | 4 |
| 5 | 4 | 3 | 4 | 5 |
To merge both criteria into one number (lower is better), the first criterion is multiplied by 10 so that it always dominates the second:
For , the five children of the root (one queen in the first column) get the values 45, 44, 43, 44 and 45, so the search starts with the queen in the middle row of the first column, on the short diagonal.
Effect
We counted the states expanded to find the first solution, compared with the recursive calls of first-solution backtracking:
| Backtracking (calls) | B&B (states expanded) | |
|---|---|---|
| 8 | 114 | 31 |
| 16 | 10,053 | 17 |
| 20 | 199,636 | 59 |
| 24 | 411,609 | 253 |
| 28 | 3,006,299 | 92,956 |
The heuristic guides the search towards a solution far more directly. It is not perfect (for it needs many more states than for ), which is a reminder that heuristics improve typical behavior, not the worst case.
The traveling salesman
Problem. A salesman must visit cities, each exactly once, and return to the city where he started. The distance between every pair of cities is known. Find the shortest tour. It is the most famous NP-hard problem, and branch and bound is the basis of the exact methods used to solve it.
Design
- State: a partial tour that starts at city A, with the cities visited so far in order, and its length. Each child adds one unvisited city at the end.
- Solution: a state that has visited all cities; its cost includes the return to A.
- Lower bound: in the final tour, every city that has not been left yet will have to be left exactly once. So a valid bound is the length so far, plus the cheapest edge from the current city to an unvisited city, plus, for each unvisited city, its cheapest edge to another unvisited city or back to A. Each term is a minimum over the edges that the tour could still use, so the sum never exceeds the length of any completion.
An example
Five cities, A to E, with these symmetric distances:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 12 | 10 | 19 | 8 | |
| B | 12 | 3 | 7 | 2 | |
| C | 10 | 3 | 6 | 20 | |
| D | 19 | 7 | 6 | 4 | |
| E | 8 | 2 | 20 | 4 |
At the root nothing has been decided, and every city must still be left once: the bound is the sum of the smallest distance of each row, . The best-first search expanded 13 nodes and generated 31, and found the optimal tour A → C → B → D → E → A, of length . There are only 24 tours starting at A here, so the saving is modest; it grows quickly with .
At scale
On random instances with symmetric distances between 10 and 99, we compared this branch and bound with a backtracking search that prunes only when the length so far reaches the best tour found:
| Cities | Backtracking: calls | B&B: nodes expanded | B&B: nodes generated |
|---|---|---|---|
| 10 | 115,173 | 454 | 2,118 |
| 12 | 1,187,758 | 644 | 4,129 |
| 13 | 12,072,124 | 4,230 | 26,759 |
Both find the same optimum. As in the assignment problem, the difference comes from the bound, which accounts for the length still to travel. Real solvers such as Concorde use much stronger bounds (based on spanning trees and linear programming) and solve instances with tens of thousands of cities.
- Bounding the 0/1 knapsack with an integer greedy completion. It is not an upper bound; use the fractional one.
- Searching a sliding puzzle that has no solution. Check first whether the configuration is reachable.
- Ordering the priority queue in the wrong direction. Minimize costs, maximize values: the most promising node must come out first.
Summary
| Problem | State | Heuristic / bound | Initial limit |
|---|---|---|---|
| Task assignment | Tasks of the first agents | Cost so far + minimum of each free column | Cheaper diagonal |
| 0/1 knapsack | Decisions on the first objects | Value so far + fractional greedy completion (upper bound) | Value of the greedy solution, or 0 |
| Sliding puzzles | Board configuration | (moves made) + Manhattan distance | Infinite (stop at the first goal extracted) |
| Rectangle placement | Board matrix with the pieces placed | Area of the enclosing box so far | Infinite, or any valid arrangement |
| N-queens | Queens placed so far, without attacks | Not needed (first solution) | |
| Traveling salesman | Partial tour from A | Length so far + cheapest exit of the current and of each unvisited city | Infinite, or the length of a greedy tour |
Practice
- ⚖️ Loose vs. tight bound. On a 0/1 knapsack instance with 20 items, compare the number of nodes expanded using the fractional greedy upper bound versus the trivial bound (value so far + sum of all remaining values).
- 🧮 Initial limit. In the task assignment problem (), compare starting with an infinite initial limit versus starting with the cost of the main diagonal or a greedy assignment.
- 🧩 Reachability first. Given two 8-puzzle start states (one solvable and one with two tiles swapped), verify their inversion parity before running A* and observe what happens if the check is skipped.
- 🗺️ TSP at scale. Measure how many nodes branch and bound expands on random symmetric TSP instances for and note both execution time and peak heap size.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.