Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another - MathOverflow most recent 30 from http://mathoverflow.net 2013-05-23T20:51:00Z http://mathoverflow.net/feeds/question/121871 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Alexander Chervov 2013-02-15T07:03:37Z 2013-02-20T22:30:37Z <p>The question is close to the <a href="http://en.wikipedia.org/wiki/Sokoban" rel="nofollow">Sokoban</a> game (thanks to Dima Pasechnik !), but a little different in details.</p> <p>Consider a directed graph (multi-graph). Consider some set of marked chips (chip1, chipe2,..., chipM). Put chips on some set of vertices 'Init1','Init2','Init3'... And consider some other set of vertices 'Final1','Final2',..., 'FinalM'.</p> <p><strong>Question</strong> Propose an "efficient" algorithm which will determine is it possible to "MOVE" chips from positions "InitNN" to positions 'FinalNN'.</p> <p>Where we are allowed to "MOVE" chip from a vertex to an outgoing edge and from incoming edge to corresponding vertex. With the CONSTRAINT that two chips are NOT allowed to be at the same place. One move - moves only ONE chip. ChipK should go to position FinalK - same "K".</p> <p><strong>Question</strong> There can be many approaches to solve the problem, I am interested in analysis their complexity. Any ideas are welcome. For example if graph is "random" in certain sense what can be the algorithm the least average complexity ? </p> <p>Where complexity is counted in number of operations (write a C-code (I actually wrote a Matlab code), compile to and calculate the number of cycles - this is well-defined complexity measure, different compilers and CPU will give approximately same result). </p> <p><strong>Example of algorithm</strong> It seems the simplest way to solve a problem is the following. Essentially it can be reduced to determining where two vertices are connected in some bigger graph, which in turn can be solved by "breadth-first search" ("wave algorithm" in Russian) (I mean let us enumerate all possible chip configurations - it will give vertices of the "new graph". Let us connect two vertices (configurations) if there is a "MOVE" which goes form one to another.) By "breadth-first search" ("wave algorithm" in Russian) I mean the following - take an initial vertex and find all connected to it; next step find all vertices connected to vertices found on the previous step; and so on....</p> <p><strong>Question</strong> What about efficiency of this algorithm ? Can one propose better ?</p> http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti/121893#121893 Answer by Dima Pasechnik for Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Dima Pasechnik 2013-02-15T11:56:57Z 2013-02-15T11:56:57Z <p>Not an answer, but an improved version of the question:</p> <p>Consider a directed graph $G=(V,A)$, and a subset $F\subset V$ of <em>final</em> nodes. Let $m=|F|$ and consider the following game: one is given a configuration of $m$ chips on $V$, with not more than one chip per node (or, more formally, an injective function $c:[1,2,\dots, m]\to V$), and the following moves are allowed:</p> <ul> <li>choose an arc $vu\in A$ with a chip on $v$ no chip on $u$;</li> <li>move the chip from $v$ onto $u$.</li> </ul> <p>(i.e., with the formalism of functions, we modify $c$ so that $u$ enters $c^{-1}(V)$, and $v$ leaves $c^{-1}(V)$.)</p> <p>The <strong>goal</strong>: cover $F$ with chips. (i.e., achieve $c([1,2,\dots, m])=F$).</p> <p><strong>Question</strong>: what is the complexity of deciding whether the goal can be reached.</p> http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti/121898#121898 Answer by Dima Pasechnik for Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Dima Pasechnik 2013-02-15T13:24:19Z 2013-02-16T09:44:24Z <p><strong>Edit</strong>: Sokoban is a <em>harder</em> problem than this one!</p> <p>It is not hard to see that <a href="http://en.wikipedia.org/wiki/Sokoban" rel="nofollow">Sokoban</a> is a particular case of this problem (the graphs arising in Sokoban are undirected and planar, of degree at most 4). Sokoban <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.50.585" rel="nofollow">is known to be NP-complete</a>. (thanks to my wife, who is a complexity theorist by training, and used to play Sokoban :-)).</p> <p>Thus, in general, assuming that P is not equal to NP, any procedure to solve this problem will run in time exponential in $m$.</p> <p>On the other hand, for any fixed $m$, this problem is solvable by repeated application of the shortest path algorithm, which need to iterate over all the possible assignments of chips in the initial configuration to the (ordered) elements of $F$ (i.e. the final nodes). If for no such an assignment the process of moving the chips completes, there is no solution. On the other hand, the procedure runs in polynomial time for $m$ fixed.</p> http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti/121942#121942 Answer by Günter Rote for Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Günter Rote 2013-02-15T20:46:47Z 2013-02-17T01:34:07Z <p>Here is a polynomial-time algorithm. I assume that the chips are identical, as in Dima's reformulation and in Sokoban. (Another version would be that the chip from Init$_i$ has to go to Final$_i$, for every $i$.)</p> <ol> <li>Find out <em>which</em> chips should go to <em>which</em> target positions: <ol> <li>Set up a bipartite graph, with an arc from Init$_i$ to Final$_j$ if Final$_j$ is reachable from Init$_i$ in the graph (ignoring the chips).</li> <li>If some Init$_i$ and some Final$_j$ coincide, it means that that some chip can be regarded as lying already on its target position. We take such pairs of vertices out of the graph.</li> <li>Find a perfect matching. If there is none, abort. The problem has no solution.</li> <li>The matching gives a 1-1 assignment between initial and final positions.</li> </ol></li> <li>Now we realize these movements one by one. Say, we want to move chip $A$ from Init$_i$ to Final$_j$, which is not occupied. Find any path from Init$_i$ to Final$_j$ in the graph (ignoring the other chips). Try to push the chip along this path. It may happen that some other chip $B$ blocks the path. In this case, move $B$ one step along the path, and then let $A$ take the previous place of $B$. Since the chips are identical, the effect is the same as if $A$ had jumped over $B$. The same trick can "jump" over several adjacent chips that block the path.</li> </ol> http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti/122049#122049 Answer by Roland Bacher for Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Roland Bacher 2013-02-17T08:01:01Z 2013-02-17T08:01:01Z <p>Not an answer but a reformulation of Sokoban in terms of oriented graphs: A Sokoban-problem of order $k$ on a finite directed graph with vertices $V$ and directed edges $E$ (with $(s,t)$ denoting an edge from $s$ to $t$) consists of two (perhaps intersecting) subsets $V_I,V_F$ of $k$ vertices (defining initial and final positions of the $k$ boxes), of a marked vertex $*$ of $V\setminus V_I$ and of a set $P=\cup_{e\in E}P_e$ of "possible pushes" where $P_e$ is a subset of edges starting at the final vertex $t$ of the oriented edge $e=(s,t)$.</p> <p>A Sokoban-problem is solved if $V_F=V_I$.</p> <p>A move consists either by a choice of a new marked vertex $*'\in V\setminus V_I$ adjacent to $<em>$ by an edge <code>$(*,*')$</code> or by a choice of a new marked vertex $</em>'\in V_i$ adjacent to $*$ by an edge <code>$(*,*')$</code> together with a new set $V'_I$ of initial vertices of the form <code>$V'_I=(V_I\setminus\lbrace *'\rbrace)\cup \lbrace v\rbrace$</code> with $v\in V\setminus V_I$ such that <code>$(*',v)$</code> is an element of the set <code>$P_{(*,*')}$</code> of possible pushes associated to the oriented edge <code>$(*,*')$</code>. (The sets $V_F$ of final positions and $P$ of possible pushes remain the same.)</p> <p>The Sokoban-graph $(V,E,V_F,P)$ with possible moves $P$ and $k$ final positions $V_F$ is the directed graph with vertices given by all ${\sharp(V)\choose k}(\sharp(V)-k)$ possible choices of $k$ initial vertices $V_I$ and of a marked vertex $*$ in $V\setminus V_I$. Moves define oriented edges of the Sokoban-graph. A solution of a Sokoban problem $(V,E,V_I,V_F,*,P)$ is a path in the Sokoban-graph $(V,E,V_F,P)$ starting at the vertex <code>$(V,E,V_I,V_F,*,P)$</code> and ending at one of the $(\sharp(V)-k)$ solutions <code>$(V,E,V_F,V_I=V_F,*,P)$</code> (with <code>$*\in V\setminus V_F$</code>).</p> <p>The Sokoban graph $(V,E,V_F,P)$ has thus $&lt;\sharp(V)^{k+1}$ vertices and it is of degree $\leq m^2$ where $m$ is the maximum over all in-degrees and out-degrees of $(V,E)$. I have the impression that this implies that a given concrete Sokoban problem can be solved in polynomial time (since $m\leq 4$ for Sokoban).</p> http://mathoverflow.net/questions/121871/algorithm-to-solve-sokoban-like-game-on-graphs-move-chips-from-one-set-of-verti/122350#122350 Answer by Günter Rote for Algorithm to solve Sokoban-like game on graphs - move chips from one set of vertices to another Günter Rote 2013-02-19T21:27:24Z 2013-02-20T22:30:37Z <p>The better analogy when the markers are distinct is not Sokoban, but the <a href="http://en.wikipedia.org/wiki/15_puzzle" rel="nofollow">15-puzzle</a>. It is even on an <em>undirected graph</em>.</p> <p>All my remarks below are about the undirected version. <strong>ADDITION:</strong> <em>At the end there is a remark about the application to the directed case</em>. (My brief literature search turn up only one paper with directed graphs, for a <em>single</em> pebble (robot) with obstacles whose final position is irrelevant.) The upshot is:</p> <blockquote> <p>On undirected graphs, the decision version ("Does a solution exist?") is solvable in polynomial time, but minimizing the number of moves is NP-hard.</p> </blockquote> <p>There is a short paper by Oded Goldreich, dating back to 1984 but <a href="http://dx.doi.org/10.1007/978-3-642-22670-0_1" rel="nofollow">published</a> only in 2011, "<a href="http://www.wisdom.weizmann.ac.il/~oded/COL/puzzle.pdf" rel="nofollow">Finding the shortest move-sequence in the graph-generalized 15-puzzle is NP-hard</a>". Ratner and Warmuth showed (<a href="http://dx.doi.org/10.1016/S0747-7171%2808%2980001-6" rel="nofollow">Journal of Symbolic Computation, 1990</a>) that this is true even for the extension of the 15-puzzle to larger squares.</p> <p>Richard Wilson has <a href="http://dx.doi.org/10.1016/0095-8956%2874%2990098-7" rel="nofollow">characterized</a> in 1974 the cases when a solution is possible, in the case when there are $n-1$ tokens on a general biconnected $n$-vertex graph, like in the 15-puzzle. According to a <a href="http://www.aaai.org/ocs/index.php/SOCS/SOCS12/paper/viewFile/5407/5197" rel="nofollow">recent paper by Gabriele Röger and Malte Helmert</a>, Kornhauser, Miller, and Spirakis (<a href="http://dx.doi.org/10.1109/SFCS.1984.715921%20" rel="nofollow">"Coordinating pebble motion on graphs, the diameter of permutation groups, and applications", 1984</a>) extended these results to the case when fewer vertices are occupied and to more general graphs, and showed that there is a solution with $O(n^3)$ moves if there is a solution at all. (I haven't looked at this paper. Anyway, Röger and Helmert recommend to find more details in the <a href="http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-320.pdf" rel="nofollow">tech-report</a>, which contains Daniel Kornhauser's Master's theses.)</p> <p>For <strong>directed graphs</strong> which are <em>biconnected and strongly connected</em>, one can apply the characterizations of the undirected graph case, because a "backward move" can always be simulated: Let $ab$ be an arc and suppose we would like to move a pebble $X$ from $b$ to $a$. Find a directed cycle through $ab$ and push the vertices on this cycle through, but don't make the last move of pebble $X$ from $a$ to $b$. This realizes a backward move by $O(n^2)$ forward moves. This yields a linear-time decision algorithm and an $O(n^5)$ upper bound on the number of moves (when a solution exists) for this digraph class.</p>