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 :)

21 Upvotes

38 comments sorted by

View all comments

Show parent comment

1

u/StoneCypher Sep 08 '11

Right well. I do mathematics

Not enough to know the difference between optimized brute force and never-was-brute-force-in-the-first-place, apparently.

You strike me as one of those people who thinks that everything is a specialized case of random brute force sampling. The equivalent kind of idiot in computer science will point out that essentially everything is turing complete; it's as meaningless a non-observation as yours is.

Let me be perfectly clear with you. You're looking at a simple iteratative algorithm with the shuffle button turned on. THAT IS NOT WHAT BRUTE FORCE MEANS. Brute force means trying every possible end result, not taking one measured step at a time.

and the well known technique to solve this class of problems mathematicians call rejection sampling

Just because there's a technique which is well known to mathematicians doesn't mean you have correctly identified its use.

Here is a case example where you have not.

so sorry to have confused you with this terminology.

That you have made a mistake is not a case of my having gotten confused. You're just making a pile of unjustified assertions then presuming superiority when someone points out the critical logical errors underpinning your claim.

This is not rejection sampling because:

  1. No answers are ever sampled under any circumstances
  2. There is no envelope distribution
  3. There is no instrument distribution
  4. There is no target distribution
  5. There is no unconditional acceptance probability
  6. The set of answers is never generated
  7. No individual complete answer is ever rejected
  8. This is not a monte carlo generation, on which rejection sampling is reliant
  9. The envelope principle makes no sense in this context
  10. U(0,1) is geometrically larger than your grandchildren's computer will be able to handle for real-world maps
  11. The unit interval makes no sense in combinatorics
  12. You pretend to be a mathematician on TV, but actually are operating entirely in the realm of pretentious metaphor, where mathematicians frequently learn that the reason software engineers don't take them seriously is they don't actually understand the software engineering they think they're describing in metaphor but are not

Rejection sampling is where you generate a complete answer at random, then measure it, and either throw it back into the pool or accept it. Its purpose is not to find answers, but rather to see the rate of rejection as a ratio.

You are 100% full of crap.

No need to resort to ad-hominems

Logic: another field you shouldn't be pretending to understand. Ad hominem is not a fancy way to say insult, and I have not made any logic which is reliant on insults. This is simply incorrect.

it just makes one seem stupid

Funny how you complain about insults, then insult in the same sentence. I have a word. It starts with "hypocri" and ends with "e". Can you find the missing letters?

Since the order of iteration is the crucial element of your algorithm

Actually, it isn't. What you may have meant was "since the order of iteration can impact the neighbor workload." The critical (not crucial, get an english book please) element is actually the single-pass scan. Consider reading a book by Dijkstra.

I find this particularly hilarious since you tried to ask me if I could compute the workload of the algorithm, and it's becoming apparent that you cannot.

you should not leave the critical part out of the description.

  1. I did not
  2. Crucial != critical
  3. That you don't know the basics of what the word means, and therefore assume incorrectly that information which is there is not, doesn't affect me any more than if someone talked about the school year and you said "you left out that it starts in September." No, dear, I didn't leave that out; your education did. (Notably, this was discussed in a number of books an actual mathematician would have read, and is a critical part of number theory.)

Okay, I'll show you. If you first iterate over all the even columns, and then all the odd columns, chances are good that your algorithm will take exponential time.

No it won't. Please take a breath and try it, instead of hand-waving about imaginary trap cases you set. In either of those cases there are a worst case of two neighbors; every cell can be filled with zero backtracking.

Maybe you missed that part where I told you the worst case wasn't a problem. Maybe you just are too far from being a mathematician to know how to find the worst case. Maybe for all your talk of what I don't get, you don't actually know how to measure the worst case and figure out how much work it is.

Does it bother you that you're wildly speculating about a well defined, easily bounded thing which earlier you were haughtily challenging others to find, then giving hilariously easy to beat examples which, if you had bothered to think them through, don't actually support you in any way?

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.

You just don't know what you're talking about.

If you don't believe me then try it.

I don't need to. I just found the worst case and tried that, which I told you before this response.

You aren't reading what's being said to you very successfully.

Or iterate over the grid randomly, nearly all iteration orders will result in exponential runtime of your algorithm.

It's funny how you keep saying this, apparently blissfully unaware that it is completely wrong, and that the reason for that was already explained to you.

Since this problem is NP-complete

Looooolooooool.

You think this is SAT?

You aren't even enough of a mathematician to know the difference between the complexity of coloring contiguous maps and coloring arbitrary graphs?

C'mon, Jean-Claude. You said you could easily provide an example. I pulled your card, because you can't.

Instead of giving an example, you gave a link to an unrelated math problem that you think is the same thing, and thought you succeeded.

I'll say it again.

You made the claim that you can give a map on a square grid which produces exponential growth that will take billions of years to complete.

I laughed in your face and told you to do it.

You failed.

Show me one such grid and I'll solve it after work. Hell, show me three.

You don't know what you're talking about, and you made multiple claims of ability which, when challenged, you are backing away from.

If your algorithm really works as well as you think it does, go collect your Turing award and $1M Clay prize.

Randomized depth first search is not my algorithm, and there are no clay prizes it satisfies. The Turing award would never be handed out for an algorithm which was well understood more than two hundred years before Turing was born.

Go on. Show me that billion year grid, since you're so certain you can produce it easily, and held that up as proof of your position.

Holding up an unrelated thing and waiting for the chance to say "it's not unrelated, you just don't get it" is not actually a case of your having shown that billion year grid you claimed you could make so easily.

For all your talk, you still have no idea what you're talking about, and you can't produce the things you claim you can.

Not A Real Mathematician (tm).

2

u/julesjacobs Sep 08 '11

This is not rejection sampling because: [snip] Rejection sampling is where you generate a complete answer at random, then measure it, and either throw it back into the pool or accept it. Its purpose is not to find answers, but rather to see the rate of rejection as a ratio.

Nice cutting and pasting words from wikipedia. I think you missed the word "sampling" in rejection sampling. What you're describing is generally called "inference", and indeed you can use rejection sampling as a subroutine to do approximate inference.

No it won't. Please take a breath and try it, instead of hand-waving about imaginary trap cases you set. In either of those cases there are a worst case of two neighbors; every cell can be filled with zero backtracking.

Maybe you missed that part where I told you the worst case wasn't a problem. Maybe you just are too far from being a mathematician to know how to find the worst case. Maybe for all your talk of what I don't get, you don't actually know how to measure the worst case and figure out how much work it is.

You claiming that the worst case isn't a problem doesn't change the facts. Fact is it is a problem. With 2 colors and filling in even columns first then odd columns can lead to exponential backtracking. For example, if the algorithm happens to select the first cell in the top row as "red" and the third cell in the top row as "blue". Then your algorithm with happily continue filling in the grid until it has to fill in the second cell in the first row. At that point it will backtrack an exponential number of times all the way back up to the third cell in the first row.

You aren't even enough of a mathematician to know the difference between the complexity of coloring contiguous maps and coloring arbitrary graphs?

You said arbitrary map, and it would be most logical to assume that you meant arbitrary map not just a map on a grid. A map on a grid is obviously a trivial problem since it is strictly simpler than a full grid, and I dismissed the possibility that you'd make such a trivial claim.

I'm still confused about whether you're an educated troll or not.

0

u/StoneCypher Sep 08 '11

Nice cutting and pasting words from wikipedia.

Cute, really, how if someone gives a long list of things that undermine you, instead of to show how any of them are incorrect or to address any of the problems, you just make an (incorrect) guess about where the material came from, then proceed to ignore the long list of errors you apparently can't actually address in legitimate terms.

I think you missed the word "sampling" in rejection sampling

No, I didn't. That's why the very first thing I pointed out is that no sampling is occurring. For sampling to occur, you'd need a way to either select from a pre-calculated list of all possible colorings, or a mechanism which generates a complete coloring.

Since neither of those are in play here, which I pointed out in that text you didn't read very well again, ... qed.

What you're describing is generally called "inference"

I didn't say anything about inference. This is a complete and total non sequitur.

You probably meant I was committing inference, but that is also false. I have made zero assumptions about truth.

You might however actually mean

and indeed you can use rejection sampling as a subroutine to do approximate inference.

And that's even funnier, because Approximate Inference is a machine learning technique by guiding from sample sets to rule-based decision making. Since there are no over-arching structures keeping track of how successful each coloring was, anyone who knows what you're trying to talk about actually is is sadly giggling right now.

Pearl's propogation algorithm is an approximate inference topic. This is just randomized depth first search to perform a coloring.

I love how you just keep picking random things in math, saying "this is what you're doing," 100% failing to describe in any way how your assertion makes any sense, then when you have a list of errors pointed out, you first assert your claim to be correct, then claim someone else is engaging in inference, which you later swap with approximate inference, even though they're fundamentally unrelated.

The best part? It's not clear which one is less correct.

You claiming that the worst case isn't a problem doesn't change the facts. Fact is it is a problem.

Your random assertions are not facts. No mathematician would ever abuse that word that way.

You claimed to be able to produce an example board easily, yet now you've gone through two replies going through backflips to avoid doing so, yet asserting as hard as your little fake mathematician hands can that such boards exist, because you say it's a fact.

And I'm laughing at you because I already calculated the worst case, something which if you knew how to do you would have by now, and it's still less than a second of work on a Gameboy Advance.

But just keep saying problem, fact and exponential, while avoiding giving the examples you earlier tried to shame me by pretending you could give.

Truly, you're fooling everyone. (Granted this only holds in a universe where you're the only thing that matters, since nobody but you is fooled, but this appears to be the value system in place.)

I'll say it again.

The calculated worst case is less than a second, and you're claiming you can easily produce an example of a billion year result.

After you make that claim, nothing you say gets taken seriously until you do what you said you could easily do.

You and I both know you actually cannot.

With 2 colors and filling in even columns first then odd columns can lead to exponential backtracking

Yes, this is the hilariously obviously false claim you already made. You appear to be allergic to giving an actual example, since you know perfectly well that any example you give will be refuted in far less than a billion years.

For example

You seem quite confused about what an example actually is.

Then your algorithm with happily continue filling in the grid until it has to fill in the second cell in the first row. At that point it will backtrack an exponential number of times all the way back up to the third cell in the first row.

And with this, every programmer reading breathes a sigh of relief that you're in mathematics, where your lack of ability to follow simple algorithms cannot hurt them.

You said arbitrary map

No, you made this claim when we were talking about a grid.

Funny how you need to change your claim suddenly.

I'll take an arbitrary map, though, since a map isn't an abstract graph, and that qualified thing you gave earlier doesn't apply here.

Go on, give me a map I can't color. You can't do what you pretended you could do, and you can't do what context obviously was, but if you're this desperate, just do what you said you could do, and show me the actual example you claimed you could easily make.

When you keep talking about it, after your bluff has been called, you look like a liar.

A map on a grid is obviously a trivial problem since it is strictly simpler than a full grid, and I dismissed the possibility that you'd make such a trivial claim.

I didn't make the claim, though. You did. The article is about a grid. Grids are not arbitrary maps. I said that a grid with even hundreds of thousands of cells would be trivial. You said "I could easily give you one that'd take billions of years."

Now you're lying through your teeth, trying to pretend I made the claims, trying to pretend you thought it was going to be some other kind of map, et cetera.

Your new claim is no less false.

Stop trying to avoid it, and give the example you said you could so easily give.

Honestly, the only thing that's going to take a billion years is getting you to live up to the claims you make of yourself.

I'm still confused about whether you're an educated troll or not.

Of course you are.

When are you going to do what you claimed you could easily do to prove me wrong, and stop hiding behind personal attacks as a way of avoiding that the thing you claimed you could easily do to prove me wrong is actually something that you can't do?

Come on, Mr. Mathematician. You said this was easy. Quit hiding. Show me your easy example. I'll even let you change the rules to any arbitrary map; it doesn't have to be the thing that the article was about which you claimed was wrong, which you're now pretending is me having made a claim.

Go on, show me a map that takes a billion years to color.

It's like you just don't understand how pathetic it is that you keep avoiding what you claimed you could so easily do.

There's a simple Pascal's Observation here.

If you really can do it, and you aren't doing it, then "you're undermining yourself because not following through on your claims makes you look like you're posing pretentiously, and don't know what you're talking about."

If you really can't do it ("if," ha), then stop pretending you can; no amount of pretending is going to make it look like you really can.

Seriously, dude, in neither case is what you're currently doing, by putting on a show about what you know and claiming it's easy to produce examples, then going to great lengths to avoid your own claims, actually helping you.

Repeating:

I'm still confused about whether you're an educated troll or not.

This is just you laying emotional prerequisites so that later, when you run out of excuses, you can claim you've known I was a troll all along, and that you're "just giving up," coincidentally right before the claims you made were really going to come true this time.

I'm not a Republican, so I don't fall for garbage like that.

Last chance, then I'm walking away bored, since you're struggling to keep up with topics that are highschool freshman computer science issues by talking about math which is way over your head.

You want to do what you said you could easily do to prove me wrong, or do you want to keep up with this math word salad ego stroking game?

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.