Why are some games np-complete? - Computer Science Stack Exchange most recent 30 from cs.stackexchange.com 2019-08-22T07:30:29Z https://cs.stackexchange.com/feeds/question/69936 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://cs.stackexchange.com/q/69936 49 Why are some games np-complete? racc44 https://cs.stackexchange.com/users/64065 2017-02-06T18:14:37Z 2017-02-12T20:07:42Z <p>I read the Wikipedia entry about "<a href="https://en.wikipedia.org/wiki/List_of_NP-complete_problems">List of NP-complete problems</a>" and found that games like super mario, pokemon, tetris or candy crush saga are np-complete. How can I imagine np-completeness of a game? Answers don't need to be too precise. I just want to get an overview what it means that games can be np-complete. </p> https://cs.stackexchange.com/questions/69936/-/69942#69942 20 Answer by quicksort for Why are some games np-complete? quicksort https://cs.stackexchange.com/users/62447 2017-02-06T19:07:00Z 2017-02-06T19:07:00Z <p>I honestly don't know exactly what kind of model is used by the people making those claims; however, what seems reasonable to me would be talking about the $\mathcal{NP}$-completeness of deciding something about a game situation.</p> <p>Let's take as an example Tetris, since it's the only one from those you quote that I understand enough to talk about. Tetris has a rule called "perfect clear", that gives the player a large bonus if a piece drop clears the board entirely. One might wonder if, given an ordered sequence of pieces $\{P_i\}$ and an integer $k$, there exists a legal sequence of moves for the pieces $P$ that achieves at least $k$ perfect clears. Problem statements such as those are sufficiently abstract that can be modeled with the tools of complexity theory.</p> <p>Long story short, "$\mathcal{NP}$-complete" means one thing and one thing only, fancy claims such as "Super Mario is $\mathcal{NP}$-complete" have to be translated into a formal statement before they make any actual sense.</p> https://cs.stackexchange.com/questions/69936/-/69945#69945 72 Answer by Craig Gidney for Why are some games np-complete? Craig Gidney https://cs.stackexchange.com/users/535 2017-02-06T19:25:43Z 2017-02-06T19:25:43Z <p>It just means that you can create levels or puzzles within these games that encode NP-Hard problems. You can take a graph coloring problem, create an associated Super Mario Bros. level, and that level is beatable if and only if the graph is 3-colorable.</p> <p>If you want to see the <em>specific</em> way the NP-Complete problems are translated into the games, I recommend the paper <a href="https://arxiv.org/abs/1203.1895">"Classic Nintendo Games are (Computationally) Hard"</a>. It's well written and easy to follow.</p> <p>An important caveat to keep in mind is that the NP-hardness requires generalizing the games in "obvious" ways. For example, Tetris normally has a fixed size board but the hardness proof requires the game to allow arbitrarily large boards. Another example is off-screen enemies in Super Mario Bros: the proof is for a variant of the game where off-screen enemies continue moving as if they were onscreen, instead of ceasing to exist and being reset to their starting position when Mario comes back.</p> https://cs.stackexchange.com/questions/69936/-/70015#70015 1 Answer by einpoklum for Why are some games np-complete? einpoklum https://cs.stackexchange.com/users/11796 2017-02-08T00:10:50Z 2017-02-08T18:40:15Z <p>Here's a simplistic hand-waving explanation:</p> <p>Such games are <em>in NP</em> because "running" a player's behavior over the course of a game and checking whether s/he wins or loses can be done efficiently (we need it to be in polynomial time in the length of the game, but it's probably linear or $O(n \log(n))$-ish).</p> <p>Such games are <em>NP-hard</em> because the player's behavior is very expressive. While at any given point a player may only have a limited, even a fixed, number of possible actions, that's enough to create a space of behaviors or strategies exponential in the length of the game; and while you may be able to provide a simple condition or logical formula on the validity/benefit/correctness of a player's actions locally, globally you get a similar effect as with a large combinatorial circuit, or a k-CNF formula.</p> <p>Hopefully this makes some intuitive sense and also rings enough CS theory bells.</p> <p>PS - Some games are much more (computationally) complex than that. For example, the boardgames <a href="https://en.wikipedia.org/wiki/Hex_(board_game)" rel="nofollow noreferrer">Hex</a>, <a href="https://en.wikipedia.org/wiki/Hex_(board_game)" rel="nofollow noreferrer">Go</a> and and <a href="https://en.wikipedia.org/wiki/Reversi" rel="nofollow noreferrer">Reversi</a> are PSPACE-complete. That's essentially because the formula you need to satisfy for a winning strategy is a repeatedly-alternating-quantifier formula: The exists a move by player 1, such that for every move by player 2, there exists a move by player 1 etc. etc. such that with all those moves having been played, either some of player 2's moves are invalid or we have a valid sequence player 1 has won. With NP games, it's typically just one player's behavior/strategy/choice of moves.</p> https://cs.stackexchange.com/questions/69936/-/70034#70034 0 Answer by David for Why are some games np-complete? David https://cs.stackexchange.com/users/65156 2017-02-08T10:34:02Z 2017-02-08T10:34:02Z <p>Well, it's certainly in NP, because a possible solution is just a finite number of inputs (in each input frame you can select any of the k buttons, we represent each selection of the buttons for each frame with a letter) that brings you to the win-screen. We know that this game has been beaten before, so we know, that there exists a solution. An NTM goes over it's tape and guesses magically a correct certificate of length n. Then it simulates Super Mario with the input and verifies it. The verification can be done in polynomial time (linear time actually, if the solution is correct, it will take exactly n frames to win).</p> <p>To show NP-completeness, we could reduce 3-SAT to it by building a 3-Sat checker with the level generator (which is built through arbitrary code execution <a href="https://www.youtube.com/watch?v=IOsvuEA2h4w" rel="nofollow noreferrer">https://www.youtube.com/watch?v=IOsvuEA2h4w</a>).</p> <p>So we have a 3-SAT CNF input, that we check for correct formatting first. If it's badly formatted, we just translate it into one 'jump'-input (it's not possible to to beat Super Mario within one frame by doing a jump).</p> <p>We call the length of the 3-CNF input n.</p> <p>If it's correctly formatted, we translate it into a number of inputs, that builds the 3-CNF checker for us (always the same code of length k), translates the 3-CNF into a String of inputs, that builds the specific 3-CNF in the checker (in O(n)),and checks all possible solutions by brute-force. It idles and does nothing, if after going thru all solutions, none is found. It restarts the game and uses a known solution for Super Mario to beat the game (the code to do that is has the length j). Our transformation is thus in O(n), so it is within polynomial time.</p> <p>If the CNF is badly formatted, we don't win (by definition our input is not winning, if we haven't won one frame after the execution of it). If the CNF is not satisfiable, we don't win (you can't win by idling for one frame in the level generator, we ensured that in our code). If the CNF is satisfiable, the checker finds a solution restarts and wins the game. Thus the polynomial reduction of 3-Sat to Super Mario is complete and we have proven, that Super Mario is NP-complete.</p> <p>(Hope I haven't messed this up somewhere. We do run into a storage problem, if the 3-CNF is too long, but limited storage is usually ignored in these contexts, I believe)</p> https://cs.stackexchange.com/questions/69936/-/70057#70057 1 Answer by gnasher729 for Why are some games np-complete? gnasher729 https://cs.stackexchange.com/users/17408 2017-02-08T20:12:31Z 2017-02-08T20:12:31Z <p>For single player games, you can always ask the question "is there a winning strategy for the player", and that question often has a "YES" answer that can be verified in polynomial time, and may very well be NP complete. </p> <p>For two-player games, the answer can very often not be verified in polynomial time, because to verify that a move for A is a winning move, you must demonstrate that for every response of B there would be again a winning move for A and so on. </p> https://cs.stackexchange.com/questions/69936/-/70184#70184 0 Answer by MaxW for Why are some games np-complete? MaxW https://cs.stackexchange.com/users/42267 2017-02-11T17:45:32Z 2017-02-12T20:07:42Z <p><em>I rewrote this answer to try to address some comments on a previous version.</em></p> <p>I assume that you've read the Wikipedia definition for <a href="https://en.wikipedia.org/wiki/NP-completeness" rel="nofollow noreferrer">NP-completeness</a> which really doesn't focus on games. I'll water down the exact meaning of NP-completeness and game theory just a bit and explain the essence of a NP-Complete game. </p> <p>Let's consider a 2-player game with alternate moves, more restrictively this is essentially about <a href="https://en.wikipedia.org/wiki/Combinatorial_game_theory" rel="nofollow noreferrer">Combinatorial games</a>. Basically a game which the you have some number of moves that can be made and you must chose one of them. You would like to play "perfectly" which means that you'd never make a "bad" move. So of the allowable moves you'd like to select the best one. (Of course your opponent has the same goal...)</p> <blockquote> <p>Note that perfect play doesn't mean that you'll always win. The rules of the game can be such that the first or the second player should win. Also some games like Tic-Tac-Toe should end in a draw. Thus what "perfect play" means in this discussion is:<br /> (1) That you'll never be in a winning position and then lose the game because you made a "bad" move<br /> (2) You'll never miss an opportunity to get into the winning position if such an opportunity arises.</p> </blockquote> <p>Given the current state of the game what you'd like is to be able to use an "efficient algorithm" to calculate the best move. On the other hand let's note that an algorithm that has to search through the whole game tree is an "inefficient algorithm." </p> <p>Now lets define "efficiency" a bit more formally. I'm going to simplify this a bit but the essence is correct. Consider the number of calculations, $C$, that must be done to chose the next move, that one the average each move has $B$ possibilities (the <a href="https://en.wikipedia.org/wiki/Branching_factor" rel="nofollow noreferrer">branching factor</a>) and that there are $n$ moves left in the game. The notion is also that each calculation takes the same time so that the effort can be translated into <a href="https://en.wikipedia.org/wiki/Time_complexity" rel="nofollow noreferrer">time complexity</a>, $T$, instead of raw calculations. </p> <ul> <li>An "efficient algorithm" will have:<br /> $T \varpropto aB^a + bB^{\alpha-1} + cB^{\alpha-2} + ... + hB^0$<br /> where $\alpha$ is a "small integer," and a-h are some real numbers. Thus the efficient algorithm executes in <em>polynomial time</em> since this is a polynomial expression. </li> <li>An "inefficient algorithm" will have:<br /> $T \varpropto aB^n$<br /> and this algorithm executes in <em>exponential time</em> (i.e. nonpolynominal time). The point here is that as $n$ gets larger a combinatorial explosion results.</li> </ul> <p>Now the important point is that it is <em>impossible</em> to have an efficient algorithm, polynomial time, which plays perfectly for a game which NP-complete. To play perfectly a NP-complete problem must, by definition, be solved by an inefficient algorithm that runs in nonpolynomial time. </p> <blockquote> <p>Note that the running time is about the intrinsic number of calculations, not the response time perceived by a human. For a small game like Tic-Tac-Toe the computer could play all possible future moves and still respond quickly as perceived by a human. </p> </blockquote> <p>For <a href="https://en.wikipedia.org/wiki/Nim" rel="nofollow noreferrer">Nim</a> it is possible to create an polynomial time algorithm. At any point in the game the algorithm can calculate which player has a winning move and what that move should be. </p> <p>On the other hand let's take the game of <a href="https://en.wikipedia.org/wiki/3D_tic-tac-toe" rel="nofollow noreferrer">Qubic</a>. (You're trying to make a line of 4 in a 3D grid. So it is essentially tic-tac-toe on a 4x4x4 grid.) Qubic is NP-complete thus there is no polynomial time algorithm to calculate the next perfect move. The only way to know if you currently have a winning move is to try all the possible moves of both players to verify that a particular move is a winner, or at least not a loser. </p> <blockquote> <p>Truthfully the whole game tree for Qubic is small enough so that it can be encoded into a computer program which can play perfectly. What encoding means is that the whole game tree has been explored and all the moves worked out in advance. So the program can essentially do a quick database call using the current board state and get back the best move for that board state without having to do the tree search each time a move is to be made. This is really a "cheat" for our purposes here. </p> </blockquote> <p>Now let's discuss <a href="https://en.wikipedia.org/wiki/Computer_chess" rel="nofollow noreferrer">chess</a> to discuss the <em><a href="https://en.wikipedia.org/wiki/Evaluation_function" rel="nofollow noreferrer">evaluation function</a></em> ignoring some of the other features of chess playing programs. Chess is still an <a href="https://en.wikipedia.org/wiki/Solved_game" rel="nofollow noreferrer">unsolved game</a>. It is unknown if the first or second player should win. It is not possible to be given any board position and predict with certainty who will win. In fact chess has such a large game tree that it is just impossible to search the whole game tree. You'd need computers which are not just 10 or 100 times faster but billions of billions of time faster than any current computer. (There is the hope that <a href="https://en.wikipedia.org/wiki/Quantum_computing" rel="nofollow noreferrer">quantum computing</a> could cut through this Gordian knot.)</p> <p>Think of the chess evaluation function as giving each possible next move a probability of being the best move. What a chess program does is to combine look aheads with the evaluation function. Thus the program looks at all possible future moves until it gets to a point where a "good" score can be given to the board position. The computer evaluates all possible paths through the tree this way and then chooses the path with the best score. Since the search never got to the end of the game for all the paths being evaluated, all chess programs ultimately use an imperfect evaluation function. (If you're near the end of the game then the computer might be able to look at all possible future moves.) That means that might be possible to beat the program even if the program had a winning position at some point. </p>