Will a greedy algorithm solve Tatham's Flood? - Puzzling Stack Exchange most recent 30 from puzzling.stackexchange.com 2022-01-17T01:16:46Z https://puzzling.stackexchange.com/feeds/question/46644 https://creativecommons.org/licenses/by-sa/4.0/rdf https://puzzling.stackexchange.com/q/46644 32 Will a greedy algorithm solve Tatham's Flood? Rand al'Thor https://puzzling.stackexchange.com/users/5373 2016-12-12T23:51:52Z 2016-12-16T04:31:19Z <p>I was just investigating some of the puzzles on <a href="http://www.chiark.greenend.org.uk/~sgtatham/puzzles/" rel="noreferrer">Simon Tatham's website</a>, and came across <a href="http://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/flood.html" rel="noreferrer">Flood</a>, in which we start with an $n\times n$ grid of cells each of which is filled with one of $k$ predetermined colours, and (quoting the game instructions):</p> <blockquote> <p>Try to get the whole grid to be the same colour within the given number of moves, by repeatedly flood-filling the top left corner in different colours.</p> <p>Click in a square to flood-fill the top left corner with that square's colour.</p> </blockquote> <p>My strategy for doing this is to use a simple 'greedy algorithm'. At each stage:</p> <ul> <li>consider the single-colour block which starts from the top left corner</li> <li>count the number of cells of each colour which are adjacent to this block (i.e. which will become part of the block if we flood-fill with that colour)</li> <li>pick the colour such that this number is maximal, and flood-fill with that colour.</li> </ul> <p>For example, in a position such as this:</p> <p><a href="https://i.stack.imgur.com/fOxEkm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fOxEkm.png" alt="example"></a></p> <p>... flood-filling blue would gain us 4 cells, purple would gain us 5, red 3, yellow 2, and orange 2, so we choose purple. <hr> <strong>Is this algorithm always optimal?</strong> If so, is there a nice proof of its optimality? If not, what <em>is</em> the optimal algorithm for making the whole grid monochromatic as fast as possible?</p> <p><sub><strong>Disclaimer:</strong> I don't know the answer to this question. Also, I'm not completely confident that I've explained the algorithm well enough - please leave a comment if it's not clear, and I'll try to improve it.</sub></p> https://puzzling.stackexchange.com/questions/46644/-/46645#46645 40 Answer by TheGreatEscaper for Will a greedy algorithm solve Tatham's Flood? TheGreatEscaper https://puzzling.stackexchange.com/users/32596 2016-12-13T00:07:31Z 2016-12-13T11:41:33Z <p>It's definitely not optimal! Here's a counterexample:</p> <p><a href="https://i.stack.imgur.com/pYzro.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pYzro.png" alt="enter image description here"></a></p> <p>Start from the white area. Greedy algorithm would make you do all the colours on the top right (5 moves: yellow, green, dark green, blue, dark blue), then the red (1) and then all the other colours (6 moves), making a total of 12 moves.</p> <p>There is an obviously more efficient method: orange, yellow, green, dark green, blue, dark blue, red, needing only 7 moves.</p> <p>I don't think there's a simple algorithm to solve flood apart from a breadth first search which will bash out every possible case, since too much optimization relies on evaluating the entire grid. </p> <hr> <p>EDIT: I think I should clarify what a breadth first search is, as it appears it's a programmer-centric term.</p> <p>Breadth first search works through the following ways: - Only try colours that will increase your area - Store every possible state after n moves - Throw away states that are strictly inefficient - If you're a clever programmer, you can throw away some more states :).</p> <p>The states stored for n = 2 are (Y,g), (Y,O), (Y,R), (O,Y), (O,R), (R,O), (R,Y).<br> The area (Y,O) covers is a strict subset of (O,Y) so it can be removed. (O,R) Identical to (R,O).<br> So we're left with (Y,g), (Y,R), (O,Y), (O,R), (R,Y).<br> An optional clever shortcut is to throw out (Y,R) by comparing it to (R,Y) topologically, and how any solution with Y,R can be beaten by R,Y, but that requires a lot more programming work. It'll naturally die out later down the search.<br> So a good breadth first search will only store (Y,g), (O,Y), (O,R), and (R,Y), for 2 move possibilities.</p> <p>Basically, the first time a set of n moves turns the board into one colour, the breadth first search is done.<br> With a little bit of programming knowledge, it's the easiest way to 'solve' a game like flood.</p> https://puzzling.stackexchange.com/questions/46644/-/46655#46655 5 Answer by Glen O for Will a greedy algorithm solve Tatham's Flood? Glen O https://puzzling.stackexchange.com/users/9289 2016-12-13T07:29:16Z 2016-12-13T07:29:16Z <p>A normal greedy algorithm, as TheGreatEscaper has demonstrated, can be quite inefficient for some arrangements.</p> <p>But there may be value in a greedy algorithm which values percentage of a colour removed, rather than merely number of cells removed. For example, in TheGreatEscaper's example, you have three choices - red, yellow, or orange. Choosing red will clear ~17% of the red on the board. Choosing yellow will clear ~50% of the yellow on the board. But choosing orange will clear 100% of the orange on the board. Therefore, there is no value in delaying the choice of orange.</p> <p>I doubt even this approach would end up being guaranteed to be optimal. But I would expect it to perform better than the naive cell-count approach.</p> <p>Even better would be if a multi-step greedy algorithm were applied - a greedy algorithm that looks more than one step at a time.</p> <p>For instance, with a two-step algorithm, you'd look at the next two colours. So for example, using TheGreatEscaper's example, if you look only one step (using a cell-count approach), you get one cleared for each of red and orange, and two for yellow. With a two-step approach, the sequence orange-yellow, red-yellow, and yellow-Lgreen all clear 5 cells, whereas other choice pairs give fewer cells.</p> <p>Using this, along with percentages, would stabilise the sequence, avoiding many of the pitfalls that the naive approach gives, although it probably won't be completely optimal.</p> <p>Imagine TheGreatEscaper's example, but with orange replacing the dark blue at the far edges.</p> <p>In this case, the sequence using the naive approach will go yellow (2), Lgreen (3), Dgreen (4), blue (5), orange (7), red (6), blue (5), Dgreen (4), Lgreen (3), yellow (2) - 10 steps.</p> <p>If using a simple percentage approach (with a tie broken by count), it will go yellow (50%), Lgreen (50%), Dgreen (50%, 4), red (~67%), blue (100%, 10), orange (100%, 13), Lgreen (100%, 3), yellow (100%, 2), red (100%, 2) - 9 steps.</p> <p>If using a percentage approach with two-step greediness, it will go red (~117%), yellow (200%), Lgreen (200%), Dgreen (200%), blue(200%), orange(200%), red - 7 steps.</p> https://puzzling.stackexchange.com/questions/46644/-/46656#46656 2 Answer by Shirkam for Will a greedy algorithm solve Tatham's Flood? Shirkam https://puzzling.stackexchange.com/users/32641 2016-12-13T07:32:51Z 2016-12-13T07:32:51Z <p>Using a greedy algorithm usually gets you to a nearly optimal solution, or even optimal, but it's no ensured. If you want to find the solution that gets you to solve the puzzle in minimum moves (that is, the optimal solution), I would recommend you, if you're willing to learn it, of course, getting started with a bactracking algorithm. It will compare all possible solutions and return you the best. Once you have it, it's easy to modify it to obtain a branch and bound algorithm, wich will solve the problem more efficiently, ensuring you always have the optimal solution. </p> <p>In case you only want a solution, yeah, a greedy algorithm usually does your work. </p> <p>Here is a good starting point about backtracking: <a href="http://algorithms.tutorialhorizon.com/introduction-to-backtracking-programming/" rel="nofollow noreferrer">http://algorithms.tutorialhorizon.com/introduction-to-backtracking-programming/</a></p> https://puzzling.stackexchange.com/questions/46644/-/46658#46658 5 Answer by EmFi for Will a greedy algorithm solve Tatham's Flood? EmFi https://puzzling.stackexchange.com/users/32637 2016-12-13T08:14:26Z 2016-12-13T08:14:26Z <p>If you redefine the metric of greediness, you might find a more efficient algorithm.</p> <p>I've been playing around with the following greedy algorithm and had consistent success with random harder puzzles. I've only tried it for a handful of puzzles but it usually results in fewer moves than the autosolver provided by the site. It's not optimal, but it can probably be proven to be better or equivalent to picking the option that clears the most cells.</p> <p>Maximizing the number of cells unlocked by your move is the greedy strategy. It's still not optimal, because it fails to provide the optimal solution in the case provided by TheGreatEscaper.</p> <p>At each step make the choice that maximizes the reach of the flood. So choosing the option that exposes the most new cells to flood. Options should be weighted to those exposing clusters (which would likely expose the reach even further on a following move). I found this could be achieved by adding each cell in a cluster that would have an exposed side after choosing your colour. </p> <p>Put into other words, maximize the total number of cells that could be cleared by all choices for the following move.</p> <p>Tie breaker should reevaluate the state after each tied option</p> <p>It's not optimal but it would up solving TheGreatEscaper's scenario in one move more than the optimal solution.</p> <p>In the given example, you would choose purple, not because it clears 5 squares, but because it opens up 10. Orange would open up 6, blue opens 5, yellow opens 3, and red only opens 1.</p> https://puzzling.stackexchange.com/questions/46644/-/46671#46671 39 Answer by dtldarek for Will a greedy algorithm solve Tatham's Flood? dtldarek https://puzzling.stackexchange.com/users/21989 2016-12-13T09:39:19Z 2016-12-14T09:38:58Z <p>That problem is NP-hard, so an efficient strategy to calculate the optimal moves would be a major breakthrough in computer science. Of course, there might be a greedy strategy, but not an efficient one, e.g. that works in exponential time.</p> <p>To prove that it really is NP-hard, we will reduce <a href="https://en.wikipedia.org/wiki/Vertex_cover" rel="noreferrer">vertex cover</a> to your problem.</p> <p>Let $G$ be the input graph. We will use $|V|+1$ colors, one for each vertex, and one "border color" (diagram below uses gray). First, for each edge, put $|V|$ squares in a row using the colors of the vertices, and then surround it with a border. Then surround it again, each half of the perimeter with the colors of incident vertices.</p> <p><a href="https://i.stack.imgur.com/lMXWP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lMXWP.png" alt="example diagram"></a></p> <p>To explain the intuition behind the construction, let us make three observations:</p> <ul> <li>for any valid flooding sequence it is sufficient to perform only one border-color flood, namely the one that removes the last tile of border-color;</li> <li>after the border-color flood, we will still have to flood all the colors of other vertices, so it is suboptimal to flood any "unnecessary" vertices colors before all the border-color tiles has been removed;</li> <li>by the construction of puzzle instance, exactly the colors of vertices corresponding to a vertex cover are necessary, that is, for any edge at least one of incident vertices is in the cover.</li> </ul> <p>To complete the proof we have to show that an optimal puzzle solution corresponds to an optimal solution of minimum vertex cover. In other words, we show that any flooding sequence can be transformed into valid vertex cover in $G$, and that for any vertex cover in $G$ there is an appropriate puzzle flooding sequence.</p> <p>Given a flooding sequence we take vertex $v$ into the cover if its color has been flooded before the last tile of border color has been removed. The size of the cover is at most $|C| \leq k-|V|-1$ where $k$ is the length of the sequence. Observe that to remove border color tiles of each "edge" we had to flood at least one of its outer colors, in other words, each edge will have at least one incident vertex in the cover, as required.</p> <p>Given a valid vertex cover, the corresponding flooding strategy is to first flood the cover-colors, then border, and then again all the vertex colors. It's size is $k = |C|+|V|+1$ where $C$ is the vertex cover used. Note that this number matches the bound from the previous paragraph, that is, strictly shorter flooding sequence implies strictly smaller vertex cover.</p> <p>I hope this helps $\ddot\smile$</p> https://puzzling.stackexchange.com/questions/46644/-/46737#46737 3 Answer by Max Hoffman for Will a greedy algorithm solve Tatham's Flood? Max Hoffman https://puzzling.stackexchange.com/users/32698 2016-12-14T17:41:41Z 2016-12-16T04:31:19Z <p>It seems like a possible optimal strategy would be:</p> <ul> <li><p>If an entire color scheme can be eliminated with the next flood choice, choose that color</p></li> <li><p>Else: select the color that increases the surface area/border length between the current $\left( f_n \right )$ and next step $\left( f_{n+1} \right)$ </p></li> </ul> <p>In this case, surface area would be defined in a more practical sense than just the unit length of the total edges of the flood. It would be a count of the total number of blocks touching the flood, which prevents double counting and ignores the edges of the playing board. </p> <p>This is different from the greedy algorithm in that counting the touching blocks maximizes only the increase in the flood's size this turn, whereas the surface area I defined would increase the flood's ability to grow in subsequent turns, optimizing growth throughout the game.</p> <p>This method will not always be optimum, also. There are cases where sacrificing turns for a more strategic and rewarding flood in the future would be optimum. If we ran thousands of simulations to compare the a) average time of computation and b) number of turns until success for i) this simple algorithm and ii) a machine learning model or backtracking model, we could compare the tradeoff between the two. I think that if the board's starting points are generated randomly, this quick algorithm would still work pretty well.</p>