https://github.com/erfaniaa/map-coloring
Map coloring, using four colors
https://github.com/erfaniaa/map-coloring
backtracking graph map-coloring opencv planar-graphs python3
Last synced: about 2 months ago
JSON representation
Map coloring, using four colors
- Host: GitHub
- URL: https://github.com/erfaniaa/map-coloring
- Owner: Erfaniaa
- License: gpl-3.0
- Created: 2019-01-10T23:35:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:28:10.000Z (over 1 year ago)
- Last Synced: 2025-03-18T11:02:59.121Z (2 months ago)
- Topics: backtracking, graph, map-coloring, opencv, planar-graphs, python3
- Language: Python
- Homepage:
- Size: 206 KB
- Stars: 55
- Watchers: 2
- Forks: 19
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://en.wikipedia.org/wiki/Map_coloring)
2. [Four color theorem](https://en.wikipedia.org/wiki/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 corresponding regions have a common border on the map.
3. Using backtracking for [coloring](https://en.wikipedia.org/wiki/Graph_coloring#Vertex_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.