Nim game/SG function (Game Theory)

Source: Internet
Author: User
Nim game is one of the most classic models in game theory ?), It has simple rules and the conclusion that there is no beauty
Nim games are a kind of combinatorial games. Specifically, they belong to impartial combinatorial games (ICG ). An ICG game meets the following conditions (which may not be rigorous): 1. Two players are involved; 2. Two players move the game alternately, players can choose one of the limited legal mobile sets to move; 3. Any possible situation of the game, A valid movement set only depends on the situation. It does not depend on the operator of the player in turn, any previous operation, number of dice, or other factors. 4. If it is the move of a player, the valid mobile set in this situation is empty (that is to say, it cannot be moved at this time. According to this definition, many daily games are not ICG. For example, Chinese chess does not meet Condition 3, because the Red party can only move the red child, and the black party can only move the black child. The valid moving set depends on the player's turn.
Normally Definition of Nim gamesYes: There are several piles of stones, and the number of each pile of stones is limited. The legal move is to "select a pile of stones and take several stones away (you can't help but take them )", if it is the turn of a person, all the stone heaps have been taken empty, it will be negative (because he does not have any legal movement at the moment ).
This game looks a little complicated. Let's start with the simple situation. If it is your turn, there will be only one pile of stones left, then the winning strategy at this time will definitely be to finish all the stones without leaving them to the opponent, and then the opponent will lose. If there are two piles of stones that are not equal to each other, the winning strategy is to take a pile of stones to make them equal. If the opponent takes several stones in a pile, you can get the same number in the other heap until the victory. If you are dealing with two equal stones, you do not have any winning strategy at this time. Instead, your opponent can follow the above strategy to ensure that you will win. If it's three piles of stones ...... It seems very difficult to analyze. It seems that we have to use some other useful (preferably stylized) analysis methods, or, we 'd better design an algorithm that can find a winning strategy when there is a winning strategy.
   Define p-position and N-PositionWhere p represents previous, and N represents next. Intuitively, when the person who moved the previous move had a winning strategy, it was p-position, that is, "the latter hand can be sure to win" or "the first hand is defeated ", now it is the turn of the move to win the strategy is n-position, that is, "first hand can ensure that the victory ". More rigorous definition: 1. in a situation where no movement can be performed (that is, terminal position) is P-position; 2. the position that can be moved to P-position is n-position; 3. p-position is the case where all moves lead to n-position.
According to this definition, if the situation cannot be reproduced, or the positions set can be sorted by topology, then each position, p-position, or N-position can be calculated by definition.
Take the NIM game as an example. For example, I just said that when there are only two piles of stones and the number of the two piles is equal, the latter hand has a winning strategy, that is, this is a P-position. The following is a proof of concept (3, 3) is a p, is a P, is a P-position. First, the sub-situation (that is, the situation that can be caused by legal Movement) (apparently, the position of the stone heap does not affect its nature, so we can think of (x, y) and (Y, x) as the same situation. We only need to calculate the nature of these three situations. () Sub-situations include (), where () is obviously p-position, so) is n-position (as long as you find a sub-situation that is P-position, it can be indicated that it is n-position ). () Is followed by P-position (because () is the only sub-situation () is n-position), SO () is n-position. It can also be proved that (2, 3) is n-position. So all sub-situations in (3, 3) are n-position, which is P-position. Through a simple mathematical induction, We can strictly prove that "when there are two piles of stones, the situation is P-position when and only when the number of these two piles of stones is equal ".
Based on the above process, we can get a recursive algorithm-for the current situation, recursively calculate the nature of all its sub-situations. If there is a sub-situation that is P-position, moving to this sub-situation is a winning strategy. Of course, you may be keenly aware that there are a large number of overlapping sub-problems, so you can use the DP or memorizing search method to improve efficiency. But the problem is that using this algorithm, for a nim game situation (A1, A2 ,..., an) for judging its nature and finding a winning strategy, we need to calculate O (A1 * A2 *... * An) the nature of a situation, regardless of the memory, cannot reduce the time complexity. Therefore, we need a more efficient way to determine the nature of the NIM game situation.
Let's just draw a conclusion. (Bouton's theorem) for a nim game (A1, A2 ,..., an), it is P-position when and only when A1 ^ A2 ^... ^ An = 0, where ^ indicates an exclusive or (XOR) operation. How are you doing? When I saw it, I also thought it was amazing that there was no reason to relate the exclusive or operation. However, the proof of this theorem is not complex, basically based on the proof of two positions.
According to the definition, to prove the correctness of a method to judge the nature of position, we only need to prove three propositions: 1. This judgment judges all terminal positions as p-position; 2. When the condition is determined as N-position, it can certainly be moved to a certain position. 3. The condition that is determined as p-position cannot be moved to a certain position.
The first proposition clearly has only one terminal position, that is, all 0, exclusive or still 0.
The second proposition, for a situation (A1, A2,..., an), if A1 ^ A2 ^... ^! = 0, there must be a legitimate movement. After AI is changed to 'ai', A1 ^ A2 ^... ^ ai' ^... ^ An = 0. Set A1 ^ A2 ^... ^ An = K, then there must be an AI. Its binary value indicates that the highest bit of K is 1 (otherwise, how does one obtain the highest bit of K ). At this time, AI ^ k <AI must be established. Then we can change AI to AI '= ai ^ K. At this time, A1 ^ A2 ^... ^ AI '^... ^ An = A1 ^ A2 ^... ^ an ^ K = 0.
The third proposition, for a certain situation (A1, A2 ,..., an), if A1 ^ A2 ^... ^ An = 0, there must be no valid movement. After AI is changed to AI, A1 ^ A2 ^... ^ AI '^... ^ An = 0. Because the exclusive or operation satisfies the elimination rate, it is determined by a1 ^ A2 ^... ^ An = A1 ^ A2 ^... ^ AI '^... ^ An can get Ai = AI '. Changing AI to AI is not a legal move. Pass.
According to this theorem, we can judge the nature of a nim situation in O (n) time. If it is n-position, it can also be in O (N) find all winning strategies in time. The Nim problem is basically perfectly solved. ----------------------- The SG function provides a directed acyclic graph and a pawnpiece on the starting vertex. The two players move the pawnpiece along the directed edge alternately, it cannot be set to negative by the initiator. In fact, this game can be considered as an abstract model of all impartial combinatorial games. That is to say, any ICG can regard each situation as a vertex, and abstract each situation and its sub-situation into a directed graph game by a directed edge ". Next we define the SD-garundy function on the vertex of the directed acyclic graph. First define the Mex (minimal excludant) operation, which is applied to a set operation, indicating the smallest non-negative integer that does not belong to this set. For example, Mex {0, 1, 2, 4} = 3, Mex {2, 3, 5} = 0, and Mex {} = 0.
For a given directed acyclic graph, define SD-garundy Function g is as follows: g (x) = Mex {G (y) | Y is the successor of x }.
Let's take a look at the nature of SG functions. First, all the vertex corresponding to the terminal position, that is, the vertex without an outbound edge, and its SG value is 0, because its successor set is an empty set. Then for a vertex x whose g (x) = 0, all its successor y satisfies g (y )! = 0. For a G (x )! = 0 vertex, there must be a successor y to meet G (y) = 0.
The above three statements show that the postion represented by vertex x is P-position when and only when g (x) = 0 (exactly corresponds to the three sentences defined by P-positioin/n-position ). By calculating the SG value of each vertex in a directed acyclic graph, we can find a winning strategy for each situation. However, the usage of SG functions is far less simple. If a directed graph game becomes more complex, for example, a directed graph does not have only one piece, but has n pieces. You can choose one to move each time, how can we find a winning strategy?
Let's take another look at the significance of the SG value of the vertex. When g (x) = K, it indicates that for any 0 <= I <K, a successor y of X satisfies g (y) = I. That is to say, when the SG value of a piece is K, we can change it to 0, to 1 ,...... , Into the K-1, but never keep K unchanged. I don't know if you can think of the NIM game based on this. The rule of the NIM game is: each time you select a pile of K stones, you can change it to 0, to 1 ,...... , Into the K-1, but never keep K unchanged. This indicates that if the SG value of the top point where n pawns are located is regarded as the corresponding number of stones in N heaps, then each winning strategy of this Nim game corresponds to the winning strategy of the original n pieces!
For N pawns, set the SG values of the corresponding vertex to (A1, A2 ,..., An), reset the situation (A1, A2 ,..., An) when a winning strategy of the NIM game is to turn AI into K, a winning strategy of the original game is to move the I-th piece to the vertex where the SG value is K. This sounds a little amazing-how can I go back to the NIM game after a lap.
In fact, we only need to prove that the digraph game of multiple pawns is P-position. Only when the SG function where all pawns are located is different or 0. This proves that it is almost identical to the Bouton's theorem in the previous section. You just need to change a few nouns as appropriate.
Just now, in order to make the problem look easier, I thought that N pieces were moving on a directed graph. However, if it is not on a directed graph, but on a directed graph, each piece can be moved by a piece (that is, a directed graph), which will not bring any changes to the conclusion.
So we can define sum of graph games: Set G1, G2 ,...... And GN are N directed graph games. The game G is defined as G1, G2 ,...... , GN sum, the movement rule of game G is: select a sub-game GI and move the Above pawn. SD-Grundy theorem is: G (G) = g (G1) ^ g (G2) ^... ^ G (GN ). That is to say, the SG function value of the game's sum is the exclusive or of the SG function value of all its sub-games.
Consider one sentence at the beginning of this article: any ICG can be abstracted into a directed graph game. Therefore, the concepts of "SG function" and "game sum" are not limited to directed graph games. We define the SG value for each position of each ICG, or the sum of n icg. So when we face a game composed of N game groups, we only need to find the SG value for each game to find its situation, we can regard all these SG values as the NIM stone heap, and then follow the method of finding Nim's winning strategy to find the winning strategy of this game!
Go back to the question at the beginning of this article. There are n piles of stones, each time you can get 1, 2 or 3 from 1st piles of stones, you can get an odd number from 2nd piles of stones, you can get any stone from 3rd piles or later ...... We can think of it as three sub-games, with 1st sub-games containing only one pile of stones. Each time we get one, two, three, it is easy to see that the SG value of X stones is X % 4. The 2nd sub-game also has only one pile of stones. Each time it can take an odd number of stones, you can know through simple drawing that the SG value of this game when there are X stones is X % 2. 3rd games with N-2 heap stones, is a nim game. For each situation of the original game, the SG value of the three sub-games is different or the SG value of the entire game is obtained, then you can determine whether there is a winning strategy and make a decision based on the SG value. In fact, it is regarded as three sub-games, and it is simply regarded as n sub-games. Among them, 1st and 2 sub-games are described above, and 3rd and later sub-games are all "1 pile of stones, each time a few pieces can be retrieved, it is called "any stone game". The SG value of X stones in this super simple game is obviously X. In fact, isn't the NIM game of N heap stones itself the sum of N "any stone games?
Therefore, for us, the concept of SG functions and "game's sum" does not allow us to combine and create strange games, instead, we try to divide some of the seemingly complex games into several sub-game scenes, and find its SG function for each sub-game that is much simpler than the original game, then, the original game's SG function is obtained in an exclusive way to solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.