First of all, thanks for all answers and comments. Unfortunetaly, the existing tools were not fully compatible with the latest versions of QGIS and ArcGIS. Therefore I made my own solution using the tool indicated by @polygeo, the QGIS plugin from @Alexandre and the name of the algorithm (four color map) from @Jens.
Here is my code for those interested (for ArcGIS but the second part could be used in QGIS as well).
arcpy.MakeFeatureLayer_management(fc, fc[:-4]+ "_lyr" )
try:
arcpy.AddField_management(fc[:-4] + "_lyr", "color", "SHORT")
except:
print "field alread exists"
arcpy.CalculateField_management(fc[:-4] + "_lyr", "color", "10" , "PYTHON")
arcpy.PolygonNeighbors_analysis(fc[:-4] + "_lyr", fc[:-4] + "_tb.dbf" )
graph = []
cursor=arcpy.da.SearchCursor( fc[:-4] + "_tb.dbf" , ("src_FID","nbr_FID") )
for row in cursor:
graph.append(row)
pols = arcpy.da.UpdateCursor(fc[:-4] + "_lyr", ("OID@","color"))
colored = []
for pol in pols:
nbrs = [ second for first, second in graph if first == pol[0]]
usedcolors = []
for nbr in nbrs:
usedcolors += [second for first, second in colored if first == nbr]
pol[1]=[color for color in range(10) if color not in usedcolors][0]
colored.append(pol)
pols.updateRow(pol)
Note that the algorithm does not guarantee that only 4 colors are used: though it has been proven that the solution exists, the "brute force" is necessary to achieve it. In my case, I got 7 colors which is small enough. The script could have an additional loop until the solution is found, but I need to do it for hundreds of maps and 7 colors is OK.
R
code) and an optimal solution (which will use three or even two colors if they can be found to work) on Mathematica. That solution is recursive; the reply to my post gives a linear programming solution. Manifold GIS has long had a five-color algorithm built in. (Four-coloring is hard to do; five-coloring is relatively simple to achieve.) – whuber♦ Mar 11 '13 at 17:02