Skip to content
Map coloring, using four colors
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
README.md
iran.jpg
map_coloring.py Check image dimensions Jun 22, 2019
requirements.txt
tehran_province.jpg
usa.png Add samples Jan 14, 2019

README.md

map-coloring

Map coloring, using four colors

This program gets a map image as an input and produces all possible valid colorings of that map using backtracking.

The input image background and borders should be white.

Some Basic Stuff to Know

  1. Map coloring
  2. Four color theorem

Algorithm

  1. Detecting all non-white regions (eg. provinces or states).
  2. Converting the input map to a simple planar graph: There will be a node for each region. Two nodes will be adjacent, if and only if their correspondent region have a common border on the map.
  3. Using backtracking for coloring that graph (it's a recursive function that produces all valid colorings).
  4. Displaying all produced colorings on the given map.

Dependencies

Install numpy, matplotlib and opencv using pip.

pip install -r requirements.txt

Run

python3 map_coloring.py map_image_file_name

Samples

python3 map_coloring.py iran.jpg

The original image:

A part of the program output:

python3 map_coloring.py tehran_province.jpg

The original image:

A part of the program output:

python3 map_coloring.py usa.png

The original image:

A part of the program output:

Notes

It runs slowly on large images. It can be improved by changing the second part of its algorithm (about setting the graph edges). Some computational geometry knowledge about polygons may be needed for this part.

Any contributions are welcomed.

You can’t perform that action at this time.