Four-colouring (somewhat belated)
Occasionally something diary-
In mid-
The simplest algorithm that does the job is a recursive one. Given a map, pick an uncoloured region, iterate through all the colours for that region which don't already conflict with something else, and for each possibility recurse into the remaining regions, stopping as soon as an answer appears. You can speed this up hugely by choosing, at every stage of the recursion, the currently most constrained region to iterate over next; this prunes the search tree early and often, and although the worst-
But I've never been a fan of algorithms which have a hideous worst case but are OK in practice. So during my initial research for this puzzle, I googled around to see if I could find a four-
The algorithm was a by-
So I mailed one of the authors of the paper and asked if he could clarify some details for me. He replied, in what felt like a rather patronising tone, that he didn't think his algorithm would be much use to me because it was hard to implement.
Well, I'm not scared by that. I'm pretty good at implementing strange algorithms, even algorithms which I've been warned not to try because they're too hard. For example, when I was doing my DipCS, the Algorithms lecturer warned an entire roomful of Cambridge students that they should on no account attempt to implement a B-
So I persisted, and we discussed the details of the algorithm, and gradually –
A basic sketch of the proof of the four-
- Here is a ginormous appendix listing about six hundred small fragments of graphs.
- Given any planar graph (which has had some simple preprocessing done on it), you can guaranteeably find one of those fragments somewhere in it. Proof by exhaustive computer search.
- Given a graph which contains one of those fragments, you can reduce it to a slightly smaller graph, in such a way that if you are then presented with a four-
colouring of the reduced graph you can transform it back into a four- colouring of the original graph. Details of exactly how to do this for each of the 600 fragments are given in the ginormous appendix. - All sufficiently small planar graphs can be four-
coloured, simply by virtue of having at most four vertices. - Hence, essentially by an induction-
like argument, any planar graph can be four- coloured.
The four-
Unfortunately, this also leads to some rather undesirable properties of the algorithm:
- It has quadratic space cost as well as quadratic time. Each recursive step has to construct a slightly reduced version of its input graph, and as best I could tell it pretty much has to construct it in entirely separate memory rather than subtly modifying the original graph data structures. Quadratic space cost is really hideous; I can't think of any algorithm in common use which has it.
- Its linear-
time recursive step has an extremely large time constant. It has to search the graph for one of 600- odd small graph fragments, which is going to take 600 times however long it takes to find one fragment. And before that it has to compute a quantity called ‘charge’ for each vertex of the graph, which is done by another nasty graph- pattern- matching step, and which is vital to knowing whether you've found your fragment or not. Meanwhile, even finding one fragment is painfully slow: you have to essentially iterate over all edges of your input graph and ask ‘Suppose this edge of G corresponded to a given edge of my fragment F; can I then match up the rest of the edges and vertices of F without finding a mismatch?’. - Perhaps worst of all, it isn't sensibly testable. The set of 600 fragments has been proved to be sufficient to guarantee that one can be found in any graph, but hasn't been proved to be necessary. The guy I corresponded with, in fact, was of the opinion that it might easily be that only 20-
50 of them were actually required; he just hadn't managed to prove it. So you end up with 600 special cases in the algorithm code, but you can't construct a set of test inputs which between them exercise more than a small fraction of those special cases. This is a recipe for unreliability.
Once that lot became clear, I abandoned the idea of implementing the algorithm. I still think my original self-
So the current version of Map still uses the simple early-
no subject
OTOH my memory of other attempts to prove the theorem suggest to me that as long as your graph doesn't have any vertices which have 5 edges meeting at it then a good algorithm might exist. This stems from the fact it is far easier to prove the 4 colour theorem for all maps without pentagonal countries :).
There exists somewhere in my house a passable (if overly verbose) pop-sci book about the theorem which neatly sets out a) how you can reduce the problem to {2,3,4,5}-agonal countries and b) how {2,3,4}-agonal countries aren't too much of a problem. I believe it is called 'Four colours suffice' or something similar.
no subject
…except that I just bet the constants in the O(N2) are so huge you need to be colouring a much larger map than the ones in your puzzle before they're amortised.
For the avoidance of doubt, when you say that each step has to construct a reduced version of its input graph, I assume the previous version must be retained in its entirety?
no subject
As I said in the post, "as best I could tell". I didn't see any way to avoid having to do so, although it's possible that a way might have existed and I failed to spot it.
no subject
I noted your saying that a graph couldn't be reduced in situ; I was just wanting confirmation that you can't delete graphn once you've made graphn+1, i.e. that you have to retain the entire sequence of graphs in order to reconstruct the colouring once all the reductions have been done.
Incidentally, if — as I suspect — the algorithm works by creating a reducing sequence of graphs then reconstructing a colouring back up the sequence towards the original, you can probably transform the algorithm from O(N2) time O(N2) space into one that's O(N3) time and O(N) space, simply by making sure the reduction sequence is deterministic then replaying it at each step in the reconstruction instead of using retained copies of the graphs.
no subject
Do you think there could be any short cut solution? Something that in practice works more like the recursive algorithm, but that you can put some really large theoretical limit on by proving it the bad cases are ruled out by the proof??
Or you could implement this algorithm, but for graphs with N smaller than [BIGNUM] reduce to the recursive algorithm, instead. That's linear, and leaving out the quadratic algorithm is a bug that will never show up :)
Or you could release it untested, and check the answer, and if it fails automatically email a submission of the core dump to a maths journal of your choice ;)
no subject
no subject
no subject
Another thing along the same lines would be to try to find a way of dividing a one-region one-coloured graph recursively with recolouring until you were bored. That would be a general approach (as an arbitrary graph could be assembled into such a hierarchy) if you could find out a recolour step, but trying to develop your own analytic algorithm for four-colouring is not something to do in your spare time, ;).
Fwiw, I implemented a four-colouring algorithm for work a couple of years ago and used the obvious recursive approach, but N was small.
no subject
That you need four mathematicians for an efficient proof of the four-colour theorem sounds like a corollary of Conway's Law...
no subject