r/programmingchallenges • • Jun 01 '11

Coloring a grid

Make a program to color in the squares of an (n x m) grid (with some number of colors > 2) such that no two same colors touch.

There is a 'brute force' way, and (at least one) more 'elegant' solution as well.

Edit: I didn't specify-- try one that will output a different pattern each time. It's a nicer problem, I think :)

20 Upvotes

38 comments sorted by

View all comments

Show parent comment

3

u/awap Sep 09 '11 edited Sep 09 '11

I don't know about all of the other stuff he said, because frankly this exchange was too long and mean spirited to read through the whole thing. However, it really seemed like he was right about the importance of picking a good search order.

I mean hell, if you just do a checkerboard then fill in the rest you're in a way worse position than the silly case you described, and it's still an under-one-second scan.

This really did not seem right to me. I mean, hell, 2 coloring a grid is like an undergrad algo-class example of why depth-first search can bite you.

And since I was bored and am waiting for a phone call, I implemented the algorithm you described using the checkerboard pattern search. I did it in C++ so there would be no complaints about python causing the slowness or anything. If you have a problem with the implementation you can bring that up. However, it seems pretty clear that, as implemented, this is an exponential time algorithm.

Results:

$ time ./grid_color 2
0 1 
1 0 

real	0m0.036s
user	0m0.001s
sys	0m0.002s
$ time ./grid_color 4
1 0 1 0 
0 1 0 1 
1 0 1 0 
0 1 0 1 

real	0m0.016s
user	0m0.001s
sys	0m0.004s
$ time ./grid_color 6
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 

real	0m0.068s
user	0m0.060s
sys	0m0.004s

The 8x8 one ran for a few minutes, then I decided to kill it and try expanding just one dimension at a time:

$ time ./grid_color 6 6
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 

real	0m0.115s
user	0m0.109s
sys	0m0.003s
$ time ./grid_color 6 8 
1 0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 

real	0m1.847s
user	0m1.810s
sys	0m0.005s

6x10 also took too long. Apparently it is exponential with a really bad constant. I don't really feel like doing the math to figure out what it is, but it's bad.

The 4xC numbers:

4x8:    0.088 s
4x10:   0.304 s
4x12:   4.876 s
4x14: 149.828 s

1

u/StoneCypher Sep 09 '11

Apparently it is exponential with a really bad constant.

May I see your implementation? I suspect a bug.

I should point out that the experiment you performed is not, in fact, his original claim, nor is it my restate of his original claim.

If you're not going to read the whole thing, consider also not taking a position.

2

u/awap Sep 09 '11 edited Sep 09 '11

The implementation is linked from my post.

I wasn't arguing for his original position, because he was making a silly pedantic remark: the obvious iteration order is to go either through them sequentially, either row-major or column major. Asserting that that's not what you meant just seems silly.

What I'm arguing against is the statement you made that iteration order doesn't matter. That statement stands on its own; I don't need to read the whole thread to see what you said. Also, I'm pretty sure it's wrong.

You should take his original suggestion and actually do the complexity analysis instead of just asserting that it must be fast on a gameboy or whatever.

Edit, Here's a hint: do the complexity analysis for a 2xN grid. Ignore the rows, and estimate how many backtracks are needed for a particular column (it's 2c). And every time we backtrack at a column, we have to redo all of the columns below it. It gets bad.

There are 2 obvious improvements to the algorithm. One is to use backjumping instead of ordinary backtracking to go directly to the cell that caused the conflict. The other is to use the current state of the cell to adjust our search order. Both of these might make it violate the uniformly random property that we are looking for. I'm not really sure, and it's time to get back to real work...

1

u/StoneCypher Sep 09 '11

The implementation is linked from my post.

Missed that. Sorry.

Asserting that that's not what you meant just seems silly.

Kay.

What I'm arguing against is the statement you made that iteration order doesn't matter. That statement stands on its own; I don't need to read the whole thread to see what you said. Also, I'm pretty sure it's wrong.

If you do read through, however, you get the context of that comment, and you come to understand its real meaning, instead of the incorrect meaning that is assigned to it by a superficial read out of context.

I said it didn't matter because the worst case was still so tractable.

You should take his original suggestion and actually do the complexity analysis

If you read the rest of the exchange, you'd learn that I did.

instead of just asserting

Oh right, I forgot, when you don't read a thread, it's reasonable for you to tell someone to do something that you imagine they might not have done.

Please stop criticizing your imagination of what I said. Read the rest.