Branch and Bound
Branch and bound solves integer programs exactly by organized search. It splits the problem into smaller ones (branching), and uses LP relaxations to prove that whole subtrees cannot contain a better solution (bounding), so most of the exponentially many integer points are never looked at.
The algorithm
For a minimization problem, keep the best integer solution found so far, the incumbent, with value (initially ), and a list of open subproblems.
- Take a subproblem and solve its LP relaxation, value .
- Prune it if the LP is infeasible, or if (it cannot beat the incumbent).
- If the LP solution is integral, it is a candidate: update the incumbent if better, and prune.
- Otherwise pick a fractional variable and branch: create two subproblems with and .
Every pruned node is a proof that its region contains nothing better. When the list is empty, the incumbent is optimal. At any moment the smallest LP bound among open nodes and the incumbent bracket the optimum: the gap printed by every MIP solver.
import numpy as np
from scipy.optimize import linprog
def branch_and_bound(c, A, b, bounds):
best_val, best_x, nodes = np.inf, None, 0
stack = [bounds]
while stack:
bnds = stack.pop(); nodes += 1
r = linprog(c, A_ub=A, b_ub=b, bounds=bnds, method="highs")
if r.status != 0 or r.fun >= best_val - 1e-9:
continue # infeasible or bounded out
frac = [j for j, v in enumerate(r.x) if abs(v - round(v)) > 1e-6]
if not frac:
best_val, best_x = r.fun, np.round(r.x) + 0.0 # new incumbent
continue
j = frac[0]; v = r.x[j]
lo, hi = bnds[j]
down = list(bnds); down[j] = (lo, np.floor(v))
up = list(bnds); up[j] = (np.ceil(v), hi)
stack += [down, up]
return best_x, best_val, nodes
# maximize 8a + 11b + 6c + 4d s.t. 5a + 7b + 4c + 3d <= 14 and 2a + b + 3c + 2d <= 6, all in {0, 1, 2}
c = np.array([-8.0, -11.0, -6.0, -4.0])
A = np.array([[5.0, 7.0, 4.0, 3.0], [2.0, 1.0, 3.0, 2.0]]); b = np.array([14.0, 6.0])
x, val, nodes = branch_and_bound(c, A, b, [(0, 2)] * 4)
print("optimum:", x, " value:", -val, " nodes explored:", nodes)optimum: [0. 2. 0. 0.] value: 22.0 nodes explored: 7
Bounding is what makes it work
Without pruning, the search would enumerate every integer point. Compare the nodes explored with the size of the box being searched on a 0/1 knapsack:
import numpy as np
from scipy.optimize import linprog
rng = np.random.default_rng(0)
n = 20
v = rng.integers(10, 60, size=n).astype(float); w = rng.integers(5, 40, size=n).astype(float)
cap = w.sum() / 3
nodes, best = 0, -np.inf
stack = [[(0, 1)] * n]
while stack:
bnds = stack.pop(); nodes += 1
r = linprog(-v, A_ub=[w], b_ub=[cap], bounds=bnds, method="highs")
if r.status != 0 or -r.fun <= best + 1e-9:
continue
frac = [j for j, x in enumerate(r.x) if 1e-6 < x < 1 - 1e-6]
if not frac:
best = -r.fun; continue
j = frac[0]
stack += [bnds[:j] + [(0, 0)] + bnds[j + 1:], bnds[:j] + [(1, 1)] + bnds[j + 1:]]
print(f"best value {best:.0f}, nodes explored {nodes}, subsets that brute force would check {2**n:,}")best value 450, nodes explored 149, subsets that brute force would check 1,048,576
Strategy choices
| Choice | Options | Effect |
|---|---|---|
| node selection | depth-first, best-bound, best-estimate | depth-first finds incumbents fast and uses little memory; best-bound improves the lower bound fastest |
| branching variable | most fractional, pseudo-cost, strong branching | strong branching solves trial LPs to choose; pseudo-costs learn from history |
| primal heuristics | rounding, diving, local search (RINS) | good incumbents early prune more |
| relaxation | add cutting planes at nodes ("branch and cut") | tighter bounds, smaller trees |
A child LP differs from its parent by one bound, so solvers re-solve it in a few dual simplex pivots from the parent's basis.
The same idea elsewhere
Branch and bound is a general search strategy: any problem where you can split the search space and compute an optimistic bound on each part. Examples: the travelling salesman problem with 1-tree bounds, global optimization of non-convex functions with interval or convex-relaxation bounds, and alpha-beta pruning in game trees, which is a close cousin.
In software
- All MILP solvers (HiGHS, CBC, SCIP, Gurobi, CPLEX) are branch-and-cut codes; their logs show nodes explored, incumbent, best bound and gap. Setting a relative gap tolerance (
mip_rel_gapin HiGHS) stops early with a guaranteed quality.
Exercises
Exercise 1: Prune or not?
The incumbent has value 40 (minimization). A node's LP relaxation has value 42. Another node's LP has value 38.5 with a fractional solution. What happens to each?
Answer
Prune the first; branch on the second.
Solution
No integer point in the first node can be better than 42 > 40. The second could contain a solution between 38.5 and 40, so it must be explored.
Exercise 2: Integer objective trick
All objective coefficients are integers and all variables integer. The incumbent is 40 and a node's LP bound is 39.2. Can you prune?
Answer
Yes.
Solution
Every integer solution has an integer objective, so the node's best possible value is , which cannot beat the incumbent 40.
Exercise 3: Check with milp (code)
Solve the first example with scipy.optimize.milp and compare.
Answer
The same optimum and value.
Solution
import numpy as np
from scipy.optimize import milp, LinearConstraint, Bounds
c = np.array([-8.0, -11.0, -6.0, -4.0])
A = np.array([[5.0, 7.0, 4.0, 3.0], [2.0, 1.0, 3.0, 2.0]])
r = milp(c, constraints=LinearConstraint(A, ub=[14, 6]), integrality=np.ones(4), bounds=Bounds(0, 2))
print(r.x, -r.fun)[0. 2. 0. 0.] 22.0
Test yourself
In branch and bound for minimization, a node is pruned when…
- its LP bound is at least the incumbent value
- its LP solution is fractional
- it is the deepest node
- the incumbent is infinite
Branching on a fractional creates the subproblems…
- and
- and
- and
The MIP gap reported by a solver is the distance between…
- the incumbent and the best bound
- two LP solutions
- the first and last nodes
- the objective and zero