Branch and Bound
Split the problem into smaller choices, compute how good each branch could possibly get, and throw away every branch that cannot beat the best answer already found.
Branch, Bound, Prune
- Branch – take one undecided choice and create a child node for each value it can take.
- Bound – for each node, compute an optimistic limit on the best answer reachable below it.
- Prune – discard a node whose bound cannot beat the best complete answer found so far.
- Select – choose the next live node to expand: FIFO, LIFO, or least-cost first.
Branch and bound is an exact method for optimisation problems: find the maximum or minimum value over a huge set of candidate solutions. It searches a state-space tree, where the root has no choices made and each level fixes one more decision.
The idea that makes it fast is the bound. If a node's best possible outcome is no better than an answer already in hand, nothing below that node can matter, so the whole subtree is skipped without being generated.
The best complete answer found so far is called the incumbent. Every improvement to the incumbent tightens the test for every remaining node, so a good early answer can prune most of the tree.
- Used for optimisation: maximise or minimise, not just find any answer
- The state-space tree has one level per decision
- Incumbent = best complete answer found so far
- A node is pruned when its bound cannot beat the incumbent
Branch and Bound vs Backtracking
Both methods walk a state-space tree and both cut branches. The difference is why they cut and what order they explore in.
Backtracking cuts a branch that breaks a constraint, such as two queens attacking each other. It usually answers a feasibility question and explores depth-first. Branch and bound also cuts branches that are feasible but cannot be optimal, using a numeric bound, and it may explore in any order that helps.
| Backtracking | Branch and bound | |
|---|---|---|
| Problem type | Decision or enumeration: find one or all solutions | Optimisation: find the best solution |
| Pruning test | Partial solution violates a constraint | Bound cannot beat the incumbent, or infeasible |
| Search order | Depth-first (LIFO) | FIFO, LIFO, or least-cost (best-first) |
| Needs a bound? | No | Yes, an optimistic estimate per node |
| Memory | O(depth), the recursion stack | Up to the whole live list in best-first search |
| Typical examples | N-Queens, Sudoku, subset generation | 0/1 knapsack, TSP, job assignment |
- N-Queens has nothing to maximise, so a bound would add nothing
- Most knapsack branches stay within the weight limit, so only a value bound can cut them
- A backtracking search becomes LIFO branch and bound once a bound test is added
Bounding Functions and the Three Search Strategies
- FIFO branch and bound – live nodes in a queue, so the tree is explored level by level like BFS.
- LIFO branch and bound – live nodes on a stack, so the tree is explored depth-first.
- LC branch and bound – live nodes in a priority queue ordered by bound, so the most promising node is expanded next.
A bound must be optimistic: never worse than the true best answer in the subtree. For a maximisation problem it is an upper bound, for minimisation a lower bound. An optimistic bound guarantees that pruning never throws away the optimum.
It must also be cheap and tight. A bound that costs as much as solving the subtree saves nothing, and a loose bound prunes nothing. For 0/1 knapsack using branch and bound, the standard upper bound is the fractional knapsack value: fill greedily by value per weight and take a fraction of the first item that does not fit. It takes O(n) time and can never be below the true 0/1 optimum.
The live nodes, those generated but not yet expanded, are kept in a list. How that list is ordered gives the three strategies below. Least-cost (LC) search usually finds a strong incumbent early, which is why it prunes the most.
- Maximisation: upper bound. Minimisation: lower bound
- Knapsack bound = fractional knapsack value, O(n)
- Tighter bounds prune more but may cost more to compute
- LC finds good incumbents early; FIFO can use a lot of memory
Terms, operations, and practical uses
Core vocabulary
- State-space treeEach node is a partial solution; each level fixes one more decision.
- Live nodeGenerated but not yet expanded.
- E-nodeThe live node currently being expanded.
- Dead nodePruned, infeasible, or fully expanded.
- IncumbentThe best complete solution found so far.
Bounding rules
- MaximisationPrune when the upper bound ≤ the incumbent's value.
- MinimisationPrune when the lower bound ≥ the incumbent's cost.
- OptimisticA bound never worse than the subtree's true best, so the optimum is never pruned.
Related lessons
- BacktrackingThe constraint-pruning search branch and bound extends.
- Greedy algorithmsThe fractional knapsack greedy that supplies the bound.
- 0/1 knapsackThe same problem solved by dynamic programming.
- Hamiltonian cycleA related NP-complete search over vertex orders.
Code in Python, C++ and Java
import heapq
def knapsack_branch_and_bound(weights, values, capacity):
# Sort by value per unit weight so the greedy bound is as tight as possible.
order = sorted(range(len(weights)), key=lambda i: values[i] / weights[i], reverse=True)
w = [weights[i] for i in order]
v = [values[i] for i in order]
n = len(w)
def upper_bound(level, weight, value):
# Fill greedily, then take a fraction of the first item that does not fit.
bound, room = value, capacity - weight
while level < n and w[level] <= room:
room -= w[level]
bound += v[level]
level += 1
if level < n:
bound += room * v[level] / w[level]
return bound
best_value, best_taken = 0, []
expanded = 0
# Max-heap on the bound: always expand the most promising live node.
live = [(-upper_bound(0, 0, 0), 0, 0, 0, [])]
while live:
neg_bound, level, weight, value, taken = heapq.heappop(live)
if -neg_bound <= best_value or level == n:
continue # this whole subtree cannot beat the best answer
expanded += 1
if weight + w[level] <= capacity:
with_item = taken + [order[level] + 1]
new_value = value + v[level]
if new_value > best_value:
best_value, best_taken = new_value, with_item
bound = upper_bound(level + 1, weight + w[level], new_value)
if bound > best_value:
heapq.heappush(live, (-bound, level + 1, weight + w[level], new_value, with_item))
bound = upper_bound(level + 1, weight, value)
if bound > best_value:
heapq.heappush(live, (-bound, level + 1, weight, value, taken))
return best_value, sorted(best_taken), expanded
value, taken, expanded = knapsack_branch_and_bound([4, 7, 5, 3], [40, 42, 25, 12], 10)
print("best value:", value)
print("items taken:", taken)
print("nodes expanded:", expanded)#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
struct Node {
double bound;
int level, weight, value;
vector<int> taken;
bool operator<(const Node& other) const { return bound < other.bound; }
};
int main() {
vector<int> weights = {4, 7, 5, 3}, values = {40, 42, 25, 12};
const int capacity = 10, n = weights.size();
// Sort by value per unit weight so the greedy bound is as tight as possible.
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int a, int b) {
return values[a] * weights[b] > values[b] * weights[a];
});
vector<int> w(n), v(n);
for (int i = 0; i < n; i++) {
w[i] = weights[order[i]];
v[i] = values[order[i]];
}
// Fill greedily, then take a fraction of the first item that does not fit.
auto upperBound = [&](int level, int weight, int value) {
double bound = value;
int room = capacity - weight;
while (level < n && w[level] <= room) {
room -= w[level];
bound += v[level];
level++;
}
if (level < n) bound += (double)room * v[level] / w[level];
return bound;
};
int bestValue = 0, expanded = 0;
vector<int> bestTaken;
// Max-heap on the bound: always expand the most promising live node.
priority_queue<Node> live;
live.push({upperBound(0, 0, 0), 0, 0, 0, {}});
while (!live.empty()) {
Node node = live.top();
live.pop();
if (node.bound <= bestValue || node.level == n) continue; // cannot beat the best
expanded++;
int i = node.level;
if (node.weight + w[i] <= capacity) {
vector<int> withItem = node.taken;
withItem.push_back(order[i] + 1);
int newValue = node.value + v[i];
if (newValue > bestValue) {
bestValue = newValue;
bestTaken = withItem;
}
double bound = upperBound(i + 1, node.weight + w[i], newValue);
if (bound > bestValue) live.push({bound, i + 1, node.weight + w[i], newValue, withItem});
}
double bound = upperBound(i + 1, node.weight, node.value);
if (bound > bestValue) live.push({bound, i + 1, node.weight, node.value, node.taken});
}
sort(bestTaken.begin(), bestTaken.end());
cout << "best value: " << bestValue << "\n";
cout << "items taken: [";
for (size_t k = 0; k < bestTaken.size(); k++) cout << (k ? ", " : "") << bestTaken[k];
cout << "]\n";
cout << "nodes expanded: " << expanded << "\n";
}import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class Main {
record Node(double bound, int level, int weight, int value, List<Integer> taken) {}
static int[] w, v;
static int capacity, n;
// Fill greedily, then take a fraction of the first item that does not fit.
static double upperBound(int level, int weight, int value) {
double bound = value;
int room = capacity - weight;
while (level < n && w[level] <= room) {
room -= w[level];
bound += v[level];
level++;
}
if (level < n) bound += (double) room * v[level] / w[level];
return bound;
}
public static void main(String[] args) {
int[] weights = {4, 7, 5, 3}, values = {40, 42, 25, 12};
capacity = 10;
n = weights.length;
// Sort by value per unit weight so the greedy bound is as tight as possible.
Integer[] order = {0, 1, 2, 3};
Arrays.sort(order, (a, b) -> values[b] * weights[a] - values[a] * weights[b]);
w = new int[n];
v = new int[n];
for (int i = 0; i < n; i++) {
w[i] = weights[order[i]];
v[i] = values[order[i]];
}
int bestValue = 0, expanded = 0;
List<Integer> bestTaken = new ArrayList<>();
// Max-heap on the bound: always expand the most promising live node.
PriorityQueue<Node> live = new PriorityQueue<>((a, b) -> Double.compare(b.bound(), a.bound()));
live.add(new Node(upperBound(0, 0, 0), 0, 0, 0, List.of()));
while (!live.isEmpty()) {
Node node = live.poll();
if (node.bound() <= bestValue || node.level() == n) continue; // cannot beat the best
expanded++;
int i = node.level();
if (node.weight() + w[i] <= capacity) {
List<Integer> withItem = new ArrayList<>(node.taken());
withItem.add(order[i] + 1);
int newValue = node.value() + v[i];
if (newValue > bestValue) {
bestValue = newValue;
bestTaken = withItem;
}
double bound = upperBound(i + 1, node.weight() + w[i], newValue);
if (bound > bestValue) live.add(new Node(bound, i + 1, node.weight() + w[i], newValue, withItem));
}
double bound = upperBound(i + 1, node.weight(), node.value());
if (bound > bestValue) live.add(new Node(bound, i + 1, node.weight(), node.value(), node.taken()));
}
bestTaken = new ArrayList<>(bestTaken);
Collections.sort(bestTaken);
System.out.println("best value: " + bestValue);
System.out.println("items taken: " + bestTaken);
System.out.println("nodes expanded: " + expanded);
}
}Step through it
Running on weights [4, 7, 5, 3], values [40, 42, 25, 12], capacity 10
Worked Example: Job Assignment
This is the minimisation counterpart of the knapsack tracer, and the version most textbooks use to test the method.
- Workers – A, B and C, each to be given exactly one of jobs J1, J2 and J3.
- Costs – A: 9, 2, 7 · B: 6, 4, 3 · C: 5, 8, 1 (for J1, J2, J3).
- Goal – minimise the total cost, so the bound is a lower bound.
- Lower bound – cost fixed so far + the cheapest still-available job for each unassigned worker.
Root. No one is assigned. Each worker's cheapest job is A 2, B 3, C 1, so lb = 6. The true optimum cannot be below 6, although these picks may clash on a job.
Branch on A. A → J1: 9 + B's cheapest of {J2, J3} = 3 + C's cheapest of {J2, J3} = 1, lb = 13. A → J2: 2 + 3 + 1 = 6. A → J3: 7 + 4 + 5 = 16. The LC rule expands the smallest bound, A → J2.
Branch on B under A → J2. B → J1 leaves C → J3: 2 + 6 + 1 = 9, a complete assignment and the first incumbent. B → J3 leaves C → J1: 2 + 3 + 5 = 10, worse than 9.
Prune. The live nodes A → J1 (13) and A → J3 (16) have lower bounds of at least 9, so neither can produce a cheaper assignment. The search ends: A → J2, B → J1, C → J3, total 9.
| Worker | J1 | J2 | J3 |
|---|---|---|---|
| A | 9 | 2 | 7 |
| B | 6 | 4 | 3 |
| C | 5 | 8 | 1 |
- Root lower bound: the sum of row minimums, 2 + 3 + 1 = 6
- Expand the smallest lower bound first (LC)
- The first complete assignment, 9, becomes the incumbent
- Nodes with lb ≥ 9 are pruned; the answer is 9
Time and Space Complexity
In the worst case the method is no better than exhaustive search: a bound that never prunes leaves all 2ⁿ subsets of a knapsack or all n! assignments to examine. The problems it is used for are usually NP-hard, so no polynomial guarantee is expected. See P, NP, NP-complete and NP-hard.
In practice a tight bound removes most of the tree. In the 4-item knapsack tracer only 4 of the 31 possible nodes are expanded. Industrial integer-programming solvers use branch and bound with linear-programming bounds to solve problems with thousands of variables.
Space depends on the strategy. LIFO needs only O(n) for the current path. FIFO and LC can hold an exponential number of live nodes in the worst case, which is often the real limit on problem size.
- Worst case: exponential, O(2ⁿ) for knapsack, O(n!) for assignment
- Each node costs one bound computation, O(n) for knapsack
- LIFO memory O(n); FIFO and LC can need exponential memory
- The tightness of the bound decides real-world speed
Where It Is Used
It is the standard exact method whenever an optimisation problem has no known polynomial algorithm but instances are small enough, or structured enough, to solve exactly.
To write it up in an exam, state the problem as a tree of decisions, define the bounding function and show why it is optimistic, name the search strategy, then draw the tree with each node's bound and mark every prune with the comparison that caused it.
| Problem | Branch on | Bound |
|---|---|---|
| 0/1 knapsack | Take or leave item i | Fractional knapsack value (upper) |
| Travelling salesman | Next city on the tour | Reduced cost matrix, or half the sum of two cheapest edges per city (lower) |
| Job assignment | Job given to the next worker | Cheapest free job per remaining worker (lower) |
| Integer linear programming | x ≤ ⌊v⌋ or x ≥ ⌈v⌉ for a fractional variable | LP relaxation value |
- Exact answers for NP-hard optimisation problems
- Inside every commercial MIP solver
- Exam answer: tree, bound, strategy, then the pruned tree
- For feasibility-only problems, plain backtracking is enough