Skip to content
← Artificial Intelligence

/knowledge/notes/csp-map-colouring

Concept note · Artificial Intelligence

CSPs and Map Colouring

Colouring a map so no neighbours match is the standard first constraint satisfaction problem. Backtracking solves it, and two small ideas make backtracking fast.

Studied
Artificial IntelligenceCOMP30024
When
2022 S1
Applied in
Studied, not used in a project
Read / Refreshed
~5 min read2026-10-21

Colour a map so that no two neighbouring regions share a colour. It sounds like a puzzle for a rainy afternoon, and it is the standard first example of a constraint satisfaction problem, because the same machinery timetables exams, allocates rooms and fills in Sudoku grids. This note follows a backtracking search as it colours a map, and the two small ideas that make it fast.

01

The idea

A constraint satisfaction problem (CSP) has three parts: variables, a domain of possible values for each, and constraints on which combinations are allowed. In map colouring the variables are regions, the domain is the set of colours, and every shared border is a constraint that says the two sides must differ. A solution gives every variable a value without breaking any constraint. The Artificial Intelligence topic places CSPs beside search and logic.

The textbook map is mainland Australia plus Tasmania, with three colours. South Australia borders five others, so it is the hardest region to place. Tasmania borders nobody, so it can take any colour at any time. The usual version leaves out the ACT, and adding it changes nothing, because it only touches New South Wales.

Backtracking search colours one region at a time. When a region has no legal colour left, it undoes the most recent choice and tries that region's next colour. It is depth-first search over partial colourings, and it never builds the full tree of every possible colouring because it abandons a branch at the first clash.

Two ideas make it fast. Variable ordering decides which region to colour next. The minimum remaining values rule, MRV, picks the region with the fewest legal colours left, so a doomed branch fails early instead of after a long detour. Inference looks ahead after each choice. Forward checking strikes the new colour from every uncoloured neighbour, and backs up the moment any neighbour runs out.

Four colours are always enough for a flat map. Appel and Haken proved it in 1976, with a computer checking the cases, and it was the first major theorem to need one. Three are not always enough, and deciding whether three will do is NP-complete, even for flat maps.

02

The maths

Write the map as a graph. Each region is a variable XiX_i, each shared border is an edge in EE, and every variable takes one of kk colours.

X={X1,…,Xn},Di={1,…,k},C={ Xi≠Xj:(i,j)∈E }\mathcal{X} = \{X_1, \dots, X_n\}, \qquad D_i = \{1, \dots, k\}, \qquad \mathcal{C} = \{\, X_i \ne X_j : (i, j) \in E \,\}

There are kⁿ complete colourings. Backtracking checks constraints as it goes, so it visits a small fraction of them on most maps.

Given a partial colouring σ\sigma, a region's legal colours LiL_i are its domain minus the colours already used by its neighbours. MRV colours the region with the smallest LiL_i next, and breaks ties by the most uncoloured neighbours, the degree rule.

Li=Di∖{ σ(Xj):(i,j)∈E, Xj coloured },next=arg⁡min⁡Xi uncoloured∣Li∣L_i = D_i \setminus \{\, \sigma(X_j) : (i, j) \in E,\ X_j \text{ coloured} \,\}, \qquad \text{next} = \arg\min_{X_i \text{ uncoloured}} \lvert L_i \rvert

Haralick and Elliott called this fail-first: if a region is going to fail, find out now.

Forward checking keeps the domains themselves up to date. After a region gets colour cc, every uncoloured neighbour loses cc, and an empty domain means this branch is dead before the search walks into it.

Xi←c  ⟹  Dj←Dj∖{c}  for every uncoloured neighbour Xj,back up if some Dj=∅X_i \leftarrow c \;\Longrightarrow\; D_j \leftarrow D_j \setminus \{c\} \ \text{ for every uncoloured neighbour } X_j, \qquad \text{back up if some } D_j = \varnothing

On backtracking, every domain has to be restored, not only the region that is being undone.

Arc consistency goes one step further. Mackworth's AC-3 keeps removing any value that has no compatible value in some neighbour, until nothing changes. Forward checking is the cheap version that looks only one border away from the region just coloured.

03

Try it

The widget draws a made-up map of up to 16 regions on a grid and colours it by backtracking. The whole search is recorded first, so you can step through it, scrub with the step slider or play it, and the totals compare one setting with another.

16

Colours

Seed 3
ABCDEFGHIJKLMNOP
1 of 869

Colours each region may still take

  • Acoloured red
  • Bmay still take red, ink, grey
  • Cmay still take red, ink, grey
  • Dmay still take red, ink, grey
  • Emay still take red, ink, grey
  • Fmay still take red, ink, grey
  • Gmay still take red, ink, grey
  • Hmay still take red, ink, grey
  • Imay still take red, ink, grey
  • Jmay still take red, ink, grey
  • Kmay still take red, ink, grey
  • Lmay still take red, ink, grey
  • Mmay still take red, ink, grey
  • Nmay still take red, ink, grey
  • Omay still take red, ink, grey
  • Pmay still take red, ink, grey

A struck-through swatch has been ruled out, by a coloured neighbour or by forward checking.

Step 1. A is coloured red. Placements so far: 1. Backtracks so far: 0.

Whole search: 382 placements and 366 backtracks. It found a colouring.

Maps are seeded Voronoi regions on a 24 × 14 grid. Colours are red, ink, grey and dots, so no state relies on colour alone.
  • Start with plain backtracking and three colours. Jump to the end: 382 placements and 366 backtracks before it finds a colouring.
  • Tick MRV. The same map now takes 16 placements and no backtracks at all, one per region.
  • Untick MRV and tick forward checking. It lands in between, at 73 placements and 57 backtracks.
  • Tick both. The count matches MRV alone, because MRV already counts each region's legal colours and spots a region with none left on the very next pick.
  • Set two colours. Plain backtracking needs 46 placements to prove no colouring exists, and MRV needs 4. Then press New map a few times on three colours. Some maps cannot be coloured with three, and plain backtracking sometimes hits the step limit first. With four colours every map works.

04

Where I used it

05

Easy to get wrong

06

Sources