We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm not sure if anyone is interested, but here are some thoughts about this problem that might be useful to someone.
First, it is important to know that for any graph G there is a polynomial P_G(k) which gives the number of ways the graph can be colored with (up to) k colors. This polynomial P_G(k) is called the chromatic polynomial of G, and many of its properties are well-known to mathematicians.
One property of the chromatic polynomial that is relevant in this problem is the fact that its degree is equal to the number of nodes in the graph. For the grid graphs in this problem, this degree will not exceed sixty-four; but we may be asked about a particular grid graph a large number of times. Because a degree MxN polynomial is completely determined by its values for MxN different inputs, we can see that essentially we are being asked to compute the chromatic polynomials for the grid graphs in question.
The second important thing to know is that, as of this writing, there is no known simple, closed-form formula for the chromatic polynomial of an arbitrary grid graph. (If you discover one, you should publish it.) There are well-known algorithms for computing chromatic polynomials of arbitrary graphs, but they are terribly inefficient. So we don't have many options available to us:
We can use a symbolic math package like Mathematica or Maple to find out the chromatic polynomials for all of the graphs we need. This works, but arguably it wastes an opportunity to learn something interesting.
We can write our own program to compute the chromatic polynomial of an arbitrary graph, using the well-known contraction-deletion algorithm. Making this run in a reasonable amount of time is not easy: a naive implementation will end up either contracting or deleting up to 49 edges. A simple recursion through a tree with 2^49 leaf nodes is going to take a while.
We can write our own program to compute the chromatic polynomial of a grid graph, specifically. This is doable, but takes a little bit of thinking. The basic observation is that the number of ways to extend a grid graph by adding one more row depends only on the pattern of colors in the previous row; so there is the possibility here of calculating how many ways the graph can be colored row by row.
A final thought is that, even with a decent idea and a decent implementation, it is very difficult to get an algorithm that will compute the chromatic polynomial of a grid graph without exceeding the time limits for the test cases. So a different approach is to write something that can run in a few minutes, and then hard-code the results into the program that actually passes the test cases.
OK, I'll stop with that. Hopefully these thoughts might be enough to encourage someone to make some progress on this challenge. Good luck.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Coloring Grid
You are viewing a single comment's thread. Return to all comments →
I'm not sure if anyone is interested, but here are some thoughts about this problem that might be useful to someone.
First, it is important to know that for any graph G there is a polynomial
P_G(k)
which gives the number of ways the graph can be colored with (up to) k colors. This polynomialP_G(k)
is called the chromatic polynomial of G, and many of its properties are well-known to mathematicians.One property of the chromatic polynomial that is relevant in this problem is the fact that its degree is equal to the number of nodes in the graph. For the grid graphs in this problem, this degree will not exceed sixty-four; but we may be asked about a particular grid graph a large number of times. Because a degree MxN polynomial is completely determined by its values for MxN different inputs, we can see that essentially we are being asked to compute the chromatic polynomials for the grid graphs in question.
The second important thing to know is that, as of this writing, there is no known simple, closed-form formula for the chromatic polynomial of an arbitrary grid graph. (If you discover one, you should publish it.) There are well-known algorithms for computing chromatic polynomials of arbitrary graphs, but they are terribly inefficient. So we don't have many options available to us:
2^49
leaf nodes is going to take a while.A final thought is that, even with a decent idea and a decent implementation, it is very difficult to get an algorithm that will compute the chromatic polynomial of a grid graph without exceeding the time limits for the test cases. So a different approach is to write something that can run in a few minutes, and then hard-code the results into the program that actually passes the test cases.
OK, I'll stop with that. Hopefully these thoughts might be enough to encourage someone to make some progress on this challenge. Good luck.