Graph colouring as an integer program: an integer programming example, solved and checked
The graph colouring as an integer program is a classic integer programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Colour the vertices of a five-cycle (edges 1-2, 2-3, 3-4, 4-5, 5-1) so adjacent vertices differ, using as few colours as possible.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- xic = 1 if vertex i gets colour c; yc = 1 if colour c is used.
- x11 + x12 + x13 = 1: vertex 1 gets exactly one colour.
- x11 + x21 ≤ y1: along the edge 1-2, at most one end has colour 1, and only if colour 1 is used.
The model
Each line has a plain-language note on the right.
| make this as small as possible | |
| limit 1: left side must equal 1 | |
| limit 2: left side must equal 1 | |
| limit 3: left side must equal 1 | |
| limit 4: left side must equal 1 | |
| limit 5: left side must equal 1 | |
| limit 6: left side cannot exceed the right side | |
| limit 7: left side cannot exceed the right side | |
| limit 8: left side cannot exceed the right side | |
| limit 9: left side cannot exceed the right side | |
| limit 10: left side cannot exceed the right side | |
| limit 11: left side cannot exceed the right side | |
| limit 12: left side cannot exceed the right side | |
| limit 13: left side cannot exceed the right side | |
| limit 14: left side cannot exceed the right side | |
| limit 15: left side cannot exceed the right side | |
| limit 16: left side cannot exceed the right side | |
| limit 17: left side cannot exceed the right side | |
| limit 18: left side cannot exceed the right side | |
| limit 19: left side cannot exceed the right side | |
| limit 20: left side cannot exceed the right side | |
| yes (1) or no (0) |
What is being decided
There are 18 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x1_1 | binary (0 or 1) | 1 if vertex 1 gets colour 1 (yes/no); the other xi_c variables follow the same pattern for vertex i and colour c |
| x1_2 | binary (0 or 1) | see x1_1 |
| x1_3 | binary (0 or 1) | see x1_1 |
| x2_1 | binary (0 or 1) | see x1_1 |
| x2_2 | binary (0 or 1) | see x1_1 |
| x2_3 | binary (0 or 1) | see x1_1 |
| x3_1 | binary (0 or 1) | see x1_1 |
| x3_2 | binary (0 or 1) | see x1_1 |
| x3_3 | binary (0 or 1) | see x1_1 |
| x4_1 | binary (0 or 1) | see x1_1 |
| x4_2 | binary (0 or 1) | see x1_1 |
| x4_3 | binary (0 or 1) | see x1_1 |
| x5_1 | binary (0 or 1) | see x1_1 |
| x5_2 | binary (0 or 1) | see x1_1 |
| x5_3 | binary (0 or 1) | see x1_1 |
| y1 | binary (0 or 1) | 1 if colour 1 is used (yes/no) |
| y2 | binary (0 or 1) | 1 if colour 2 is used (yes/no) |
| y3 | binary (0 or 1) | 1 if colour 3 is used (yes/no) |
Goal and limits
Minimize the objective subject to 20 constraints.
Problem type
ILP, complexity level 5: integer variables with linear objective and constraints. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): constraints 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 15, 16, 18, 19 (see the table). Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x1_1 + x1_2 + x1_3 = 1 | 1 | 1 | 0 | yes |
| 2 | x2_1 + x2_2 + x2_3 = 1 | 1 | 1 | 0 | yes |
| 3 | x3_1 + x3_2 + x3_3 = 1 | 1 | 1 | 0 | yes |
| 4 | x4_1 + x4_2 + x4_3 = 1 | 1 | 1 | 0 | yes |
| 5 | x5_1 + x5_2 + x5_3 = 1 | 1 | 1 | 0 | yes |
| 6 | x1_1 + x2_1 <= y1 | 1 | 1 | 0 | yes |
| 7 | x1_2 + x2_2 <= y2 | 0 | 1 | 1 | |
| 8 | x1_3 + x2_3 <= y3 | 1 | 1 | 0 | yes |
| 9 | x2_1 + x3_1 <= y1 | 0 | 1 | 1 | |
| 10 | x2_2 + x3_2 <= y2 | 1 | 1 | 0 | yes |
| 11 | x2_3 + x3_3 <= y3 | 1 | 1 | 0 | yes |
| 12 | x3_1 + x4_1 <= y1 | 1 | 1 | 0 | yes |
| 13 | x3_2 + x4_2 <= y2 | 1 | 1 | 0 | yes |
| 14 | x3_3 + x4_3 <= y3 | 0 | 1 | 1 | |
| 15 | x4_1 + x5_1 <= y1 | 1 | 1 | 0 | yes |
| 16 | x4_2 + x5_2 <= y2 | 1 | 1 | 0 | yes |
| 17 | x4_3 + x5_3 <= y3 | 0 | 1 | 1 | |
| 18 | x1_1 + x5_1 <= y1 | 1 | 1 | 0 | yes |
| 19 | x1_2 + x5_2 <= y2 | 1 | 1 | 0 | yes |
| 20 | x1_3 + x5_3 <= y3 | 0 | 1 | 1 |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
x1_1 = cp.Variable(name='x1_1', boolean=True) # 1 if vertex 1 gets colour 1 (yes/no); the other xi_c variables follow the same pattern for vertex i and colour c
x1_2 = cp.Variable(name='x1_2', boolean=True) # see x1_1
x1_3 = cp.Variable(name='x1_3', boolean=True) # see x1_1
x2_1 = cp.Variable(name='x2_1', boolean=True) # see x1_1
x2_2 = cp.Variable(name='x2_2', boolean=True) # see x1_1
x2_3 = cp.Variable(name='x2_3', boolean=True) # see x1_1
x3_1 = cp.Variable(name='x3_1', boolean=True) # see x1_1
x3_2 = cp.Variable(name='x3_2', boolean=True) # see x1_1
x3_3 = cp.Variable(name='x3_3', boolean=True) # see x1_1
x4_1 = cp.Variable(name='x4_1', boolean=True) # see x1_1
x4_2 = cp.Variable(name='x4_2', boolean=True) # see x1_1
x4_3 = cp.Variable(name='x4_3', boolean=True) # see x1_1
x5_1 = cp.Variable(name='x5_1', boolean=True) # see x1_1
x5_2 = cp.Variable(name='x5_2', boolean=True) # see x1_1
x5_3 = cp.Variable(name='x5_3', boolean=True) # see x1_1
y1 = cp.Variable(name='y1', boolean=True) # 1 if colour 1 is used (yes/no)
y2 = cp.Variable(name='y2', boolean=True) # 1 if colour 2 is used (yes/no)
y3 = cp.Variable(name='y3', boolean=True) # 1 if colour 3 is used (yes/no)
objective = cp.Minimize(y1 + y2 + y3)
constraints = [
x1_1 + x1_2 + x1_3 == 1,
x2_1 + x2_2 + x2_3 == 1,
x3_1 + x3_2 + x3_3 == 1,
x4_1 + x4_2 + x4_3 == 1,
x5_1 + x5_2 + x5_3 == 1,
x1_1 + x2_1 <= y1,
x1_2 + x2_2 <= y2,
x1_3 + x2_3 <= y3,
x2_1 + x3_1 <= y1,
x2_2 + x3_2 <= y2,
x2_3 + x3_3 <= y3,
x3_1 + x4_1 <= y1,
x3_2 + x4_2 <= y2,
x3_3 + x4_3 <= y3,
x4_1 + x5_1 <= y1,
x4_2 + x5_2 <= y2,
x4_3 + x5_3 <= y3,
x1_1 + x5_1 <= y1,
x1_2 + x5_2 <= y2,
x1_3 + x5_3 <= y3,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x1_1 =', x1_1.value)
print('x1_2 =', x1_2.value)
print('x1_3 =', x1_3.value)
print('x2_1 =', x2_1.value)
print('x2_2 =', x2_2.value)
print('x2_3 =', x2_3.value)
print('x3_1 =', x3_1.value)
print('x3_2 =', x3_2.value)
print('x3_3 =', x3_3.value)
print('x4_1 =', x4_1.value)
print('x4_2 =', x4_2.value)
print('x4_3 =', x4_3.value)
print('x5_1 =', x5_1.value)
print('x5_2 =', x5_2.value)
print('x5_3 =', x5_3.value)
print('y1 =', y1.value)
print('y2 =', y2.value)
print('y3 =', y3.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Integer variables are relaxed: this is the dual of the continuous (LP) relaxation, which gives a bound on the integer optimum, not its exact value. Only weak duality: the dual of the continuous relaxation gives an lower bound of 2 on the integer optimum 3, a duality gap of 1. Integer problems generally have a gap.
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'x1_1 + x1_2 + x1_3 = 1' (objective change per extra unit of its right-hand side) | 1 |
| y2 | price of constraint 'x2_1 + x2_2 + x2_3 = 1' (objective change per extra unit of its right-hand side) | 1 |
| y3 | price of constraint 'x3_1 + x3_2 + x3_3 = 1' (objective change per extra unit of its right-hand side) | 0 |
| y4 | price of constraint 'x4_1 + x4_2 + x4_3 = 1' (objective change per extra unit of its right-hand side) | 0 |
| y5 | price of constraint 'x5_1 + x5_2 + x5_3 = 1' (objective change per extra unit of its right-hand side) | 0 |
| y6 | price of constraint 'x1_1 + x2_1 <= y1' (objective change per extra unit of its right-hand side) | -1 |
| y7 | price of constraint 'x1_2 + x2_2 <= y2' (objective change per extra unit of its right-hand side) | -1 |
| y8 | price of constraint 'x1_3 + x2_3 <= y3' (objective change per extra unit of its right-hand side) | -1 |
| y9 | price of constraint 'x2_1 + x3_1 <= y1' (objective change per extra unit of its right-hand side) | 0 |
| y10 | price of constraint 'x2_2 + x3_2 <= y2' (objective change per extra unit of its right-hand side) | 0 |
| y11 | price of constraint 'x2_3 + x3_3 <= y3' (objective change per extra unit of its right-hand side) | 0 |
| y12 | price of constraint 'x3_1 + x4_1 <= y1' (objective change per extra unit of its right-hand side) | 0 |
| y13 | price of constraint 'x3_2 + x4_2 <= y2' (objective change per extra unit of its right-hand side) | 0 |
| y14 | price of constraint 'x3_3 + x4_3 <= y3' (objective change per extra unit of its right-hand side) | 0 |
| y15 | price of constraint 'x4_1 + x5_1 <= y1' (objective change per extra unit of its right-hand side) | 0 |
| y16 | price of constraint 'x4_2 + x5_2 <= y2' (objective change per extra unit of its right-hand side) | 0 |
| y17 | price of constraint 'x4_3 + x5_3 <= y3' (objective change per extra unit of its right-hand side) | 0 |
| y18 | price of constraint 'x1_1 + x5_1 <= y1' (objective change per extra unit of its right-hand side) | 0 |
| y19 | price of constraint 'x1_2 + x5_2 <= y2' (objective change per extra unit of its right-hand side) | 0 |
| y20 | price of constraint 'x1_3 + x5_3 <= y3' (objective change per extra unit of its right-hand side) | 0 |
| y21 | price of constraint 'x1_1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y22 | price of constraint 'x1_2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y23 | price of constraint 'x1_3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y24 | price of constraint 'x2_1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y25 | price of constraint 'x2_2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y26 | price of constraint 'x2_3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y27 | price of constraint 'x3_1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y28 | price of constraint 'x3_2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y29 | price of constraint 'x3_3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y30 | price of constraint 'x4_1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y31 | price of constraint 'x4_2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y32 | price of constraint 'x4_3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y33 | price of constraint 'x5_1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y34 | price of constraint 'x5_2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y35 | price of constraint 'x5_3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y36 | price of constraint 'y1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y37 | price of constraint 'y2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y38 | price of constraint 'y3 <= 1' (objective change per extra unit of its right-hand side) | 0 |
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
minimize y1 + y2 + y3
subject to
x1_1 + x1_2 + x1_3 = 1
x2_1 + x2_2 + x2_3 = 1
x3_1 + x3_2 + x3_3 = 1
x4_1 + x4_2 + x4_3 = 1
x5_1 + x5_2 + x5_3 = 1
x1_1 + x2_1 <= y1
x1_2 + x2_2 <= y2
x1_3 + x2_3 <= y3
x2_1 + x3_1 <= y1
x2_2 + x3_2 <= y2
x2_3 + x3_3 <= y3
x3_1 + x4_1 <= y1
x3_2 + x4_2 <= y2
x3_3 + x4_3 <= y3
x4_1 + x5_1 <= y1
x4_2 + x5_2 <= y2
x4_3 + x5_3 <= y3
x5_1 + x1_1 <= y1
x5_2 + x1_2 <= y2
x5_3 + x1_3 <= y3
binary: x1_1, x1_2, x1_3, x2_1, x2_2, x2_3, x3_1, x3_2, x3_3, x4_1, x4_2, x4_3, x5_1, x5_2, x5_3, y1, y2, y3
# x1_1 = 1 if vertex 1 gets colour 1 (yes/no); the other xi_c variables follow the same pattern for vertex i and colour c
# y1 = 1 if colour 1 is used (yes/no)
# y2 = 1 if colour 2 is used (yes/no)
# y3 = 1 if colour 3 is used (yes/no)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, SCIP) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.