∇ProvenDecisions
Ctrl K

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.

Answer. The smallest objective value is 3, at x1_1 = 1, x1_2 = 0, x1_3 = 0, x2_1 = 0, x2_2 = 0, x2_3 = 1, x3_1 = 0, x3_2 = 1, x3_3 = 0, x4_1 = 1, x4_2 = 0, x4_3 = 0, x5_1 = 0, x5_2 = 1, x5_3 = 0, y1 = 1, y2 = 1, y3 = 1. All 20 constraints are satisfied exactly. Optimal within gap tolerance.

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:

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.

VariableTypeMeaning
x1_1binary (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_2binary (0 or 1)see x1_1
x1_3binary (0 or 1)see x1_1
x2_1binary (0 or 1)see x1_1
x2_2binary (0 or 1)see x1_1
x2_3binary (0 or 1)see x1_1
x3_1binary (0 or 1)see x1_1
x3_2binary (0 or 1)see x1_1
x3_3binary (0 or 1)see x1_1
x4_1binary (0 or 1)see x1_1
x4_2binary (0 or 1)see x1_1
x4_3binary (0 or 1)see x1_1
x5_1binary (0 or 1)see x1_1
x5_2binary (0 or 1)see x1_1
x5_3binary (0 or 1)see x1_1
y1binary (0 or 1)1 if colour 1 is used (yes/no)
y2binary (0 or 1)1 if colour 2 is used (yes/no)
y3binary (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.

x1_11x1_20x1_30x2_10x2_20x2_31x3_10x3_21x3_30x4_11x4_20x4_30x5_10x5_21x5_30y11y21y31

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.

#ConstraintValueLimitSlackBinding
1x1_1 + x1_2 + x1_3 = 1110yes
2x2_1 + x2_2 + x2_3 = 1110yes
3x3_1 + x3_2 + x3_3 = 1110yes
4x4_1 + x4_2 + x4_3 = 1110yes
5x5_1 + x5_2 + x5_3 = 1110yes
6x1_1 + x2_1 <= y1110yes
7x1_2 + x2_2 <= y2011
8x1_3 + x2_3 <= y3110yes
9x2_1 + x3_1 <= y1011
10x2_2 + x3_2 <= y2110yes
11x2_3 + x3_3 <= y3110yes
12x3_1 + x4_1 <= y1110yes
13x3_2 + x4_2 <= y2110yes
14x3_3 + x4_3 <= y3011
15x4_1 + x5_1 <= y1110yes
16x4_2 + x5_2 <= y2110yes
17x4_3 + x5_3 <= y3011
18x1_1 + x5_1 <= y1110yes
19x1_2 + x5_2 <= y2110yes
20x1_3 + x5_3 <= y3011

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 variablePricesValue
y1price of constraint 'x1_1 + x1_2 + x1_3 = 1' (objective change per extra unit of its right-hand side)1
y2price of constraint 'x2_1 + x2_2 + x2_3 = 1' (objective change per extra unit of its right-hand side)1
y3price of constraint 'x3_1 + x3_2 + x3_3 = 1' (objective change per extra unit of its right-hand side)0
y4price of constraint 'x4_1 + x4_2 + x4_3 = 1' (objective change per extra unit of its right-hand side)0
y5price of constraint 'x5_1 + x5_2 + x5_3 = 1' (objective change per extra unit of its right-hand side)0
y6price of constraint 'x1_1 + x2_1 <= y1' (objective change per extra unit of its right-hand side)-1
y7price of constraint 'x1_2 + x2_2 <= y2' (objective change per extra unit of its right-hand side)-1
y8price of constraint 'x1_3 + x2_3 <= y3' (objective change per extra unit of its right-hand side)-1
y9price of constraint 'x2_1 + x3_1 <= y1' (objective change per extra unit of its right-hand side)0
y10price of constraint 'x2_2 + x3_2 <= y2' (objective change per extra unit of its right-hand side)0
y11price of constraint 'x2_3 + x3_3 <= y3' (objective change per extra unit of its right-hand side)0
y12price of constraint 'x3_1 + x4_1 <= y1' (objective change per extra unit of its right-hand side)0
y13price of constraint 'x3_2 + x4_2 <= y2' (objective change per extra unit of its right-hand side)0
y14price of constraint 'x3_3 + x4_3 <= y3' (objective change per extra unit of its right-hand side)0
y15price of constraint 'x4_1 + x5_1 <= y1' (objective change per extra unit of its right-hand side)0
y16price of constraint 'x4_2 + x5_2 <= y2' (objective change per extra unit of its right-hand side)0
y17price of constraint 'x4_3 + x5_3 <= y3' (objective change per extra unit of its right-hand side)0
y18price of constraint 'x1_1 + x5_1 <= y1' (objective change per extra unit of its right-hand side)0
y19price of constraint 'x1_2 + x5_2 <= y2' (objective change per extra unit of its right-hand side)0
y20price of constraint 'x1_3 + x5_3 <= y3' (objective change per extra unit of its right-hand side)0
y21price of constraint 'x1_1 <= 1' (objective change per extra unit of its right-hand side)0
y22price of constraint 'x1_2 <= 1' (objective change per extra unit of its right-hand side)0
y23price of constraint 'x1_3 <= 1' (objective change per extra unit of its right-hand side)0
y24price of constraint 'x2_1 <= 1' (objective change per extra unit of its right-hand side)0
y25price of constraint 'x2_2 <= 1' (objective change per extra unit of its right-hand side)0
y26price of constraint 'x2_3 <= 1' (objective change per extra unit of its right-hand side)0
y27price of constraint 'x3_1 <= 1' (objective change per extra unit of its right-hand side)0
y28price of constraint 'x3_2 <= 1' (objective change per extra unit of its right-hand side)0
y29price of constraint 'x3_3 <= 1' (objective change per extra unit of its right-hand side)0
y30price of constraint 'x4_1 <= 1' (objective change per extra unit of its right-hand side)0
y31price of constraint 'x4_2 <= 1' (objective change per extra unit of its right-hand side)0
y32price of constraint 'x4_3 <= 1' (objective change per extra unit of its right-hand side)0
y33price of constraint 'x5_1 <= 1' (objective change per extra unit of its right-hand side)0
y34price of constraint 'x5_2 <= 1' (objective change per extra unit of its right-hand side)0
y35price of constraint 'x5_3 <= 1' (objective change per extra unit of its right-hand side)0
y36price of constraint 'y1 <= 1' (objective change per extra unit of its right-hand side)0
y37price of constraint 'y2 <= 1' (objective change per extra unit of its right-hand side)0
y38price 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)

Change the numbers and solve it

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

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: