Towards an optimal solution for Open Flood

A couple months ago I came by an interesting problem that my friends were working on for their Algorithms course homework. The problem statement (linked here) was:

(This question was inspired by the game Open Flood [available as an app on Android].)

You are given a directed graph $\text{G}$ with $n$ vertices and $m$ edges (here $m \geq n$). Every edge $e \in \text{E(G)}$ has a color $c(\text{e})$ associated with it. The colors are taken from a set $C = \{1, \ldots, \xi\}$ (assume $\xi \leq n$), and every color $c \in C$ has price $p(c) > 0$.

Given a start vertex $s_0 = s$, and a sequence $\Pi = \langle c_1, \ldots, c_\ell \rangle$ of colors, a compliant walk, at the $i$th time, either stays where it is (i.e., $s_i = s_{i-1}$), or alternatively travels a sequence of edges of color $c_i$ that starts at $s_i$. Formally, if there is a path

$$\sigma \equiv (u_1, u_2), (u_2, u_3), \ldots, (u_{\tau-1}, u_\tau) \in \text{E(G)}$$

such that $c((u_j, u_{j+1})) = c_i$ for all $j$, and $u_1 = s_i$, then one can set $s_{i+1} = u_\tau$. The price of $\Pi$ is $p(\Pi) = \sum_{i=1}^\ell p(c_i)$.

Describe an algorithm, as fast as possible, that computes the cheapest sequence of colors for which there is a compliant walk in $\text{G}$ from a vertex $s$ to a vertex $t$.

For full credit, your algorithm should run in $O(m \log m)$ time (be suspicious if you get faster running time). Correct solutions providing polynomial running time would get $50\%$ of the points.

Some hard to read math in there, but let us break it down by first looking at the game. It is available on the Google Play Store, and since it is open source, you can browse the source on GunshipPenguin/open_flood.

Open Flood on Android
Open Flood on Android.

The idea of the game is simple: you start at the top left cell. In each turn, all adjacent cells with the same color are flooded with the selected color. Below is the Jupyter notebook exploring a few heuristics for solving it:

Loading notebook…

Try it yourself

Play a 12×12 board below, or click Simulate greedy to watch the immediate-maximum heuristic from the notebook solve it step by step.

← Back to writing