I want to get an estimate for the worst-case scenario of an algorithm where the number of iterations depends on how many pixels in an image have neighbours that differ from themselves. Assuming the image to be in grayscale, I’m looking for a way to generate an M x N matrix filled with random values in the range [0, 255] where no two pixels with the same value are contiguous (including “corner” neighbours, i.e. this is for the 8-neighbour case).
Edit: should’ve made this clearer in the original posting. I’m looking to get a relatively uniform sampling of all values in the range [0, 255].
How would one do this? The efficiency of the solution doesn’t really matter, I’m looking for something that’s easy to reason about.
etc.
More generally, the dumbest way to try to colour a graph is to visit each vertex in turn (in any order you like), and give it a colour that hasn’t already been used for a vertex that’s adjacent to it. If (as in this case) you have more colours than the greatest possible number of adjacent vertices, you can’t lose.
The grid above is the result of visiting the matrix from left-to-right along each row, and using the smallest available value. You can instead choose one at random from the available values: