game theory (i): Nim Games
Key conclusion: For a Nim game situation (A1,a2,..., an), it is p-position when and only A1^a2^...^an=0, where ^ represents a bitwise XOR or (XOR) operation.
Nim game is one of the most classic models in game theory. , it has a very simple rule and a very beautiful conclusion, from the game began to understand the game theory is probably the most appropriate.
Nim game is a combination game (combinatorial Games) of a kind, accurately, belong to the "impartial Combinatorial Games" (hereinafter referred to as ICG). Games that meet the following criteria are ICG (perhaps less rigorous): 1, there are two players, 2, two players alternate to the game Movement (move), each step, the player can be (in general) a limited number of legitimate moving a set of a move; 3, for any possible situation of the game, The legitimate move set depends only on the situation itself, it does not depend on the player's turn, any previous operation, the number of dice, or any other factor; 4. If a competitor moves, and the legal move set of the situation is empty (that is, it cannot be moved at this time), the player is negative. According to this definition, many of the day-to-day games are not ICG. For example, chess does not meet the conditions of 3, because the red can only move the red son, Black can only move sunspots, the legitimate move set depends on the turn of the player to operate.
The usual NIM game is defined as: There are a number of pebbles, each with a finite number of stones, the legal move is to "pick a bunch of stones and take a few (without)", and if it's time for someone to have all the gravel heaps empty, it's a negative (because he doesn't have any legal moves at the moment).
The game looks a bit complicated, start with a simple case. If it is your turn, only a pile of stones, then the winning strategy at this time must be to take the whole pile of stone is not left to the opponent, and then the opponent loses. If two piles of unequal stones are left, the strategy is to make the two piles equal by taking more than one pile of stones, and if the opponent takes several in a certain pile, you can hold the same number of numbers in the other pile until the victory. If you are faced with two piles of equal stones, then you do not have any strategy to win, but the opponents can follow the above strategy to ensure victory. If it is three piles of stones ... It seems difficult to analyze, it seems that we have to use some other useful (preferably stylized) analysis methods, or, we should be able to design a win strategy when we can find a winning strategy algorithm.
Define P-position and N-position, where p represents previous,n on behalf of next. Intuitively speaking, the last move people have a winning strategy of the situation is p-position, that is, "the next can be guaranteed to win" or "the first time will fail", now turn to move people have a winning strategy is the situation is n-position, that is, "the tempo can guarantee victory." A more rigorous definition is: 1. The situation where no movement can be carried out (i.e. terminal position) is P-position 2. The situation that can move to P-position is n-position;3. All movement leads to the n-position situation is p-position.
According to this definition, if the situation is not possible to reproduce, or the positions set can be topological sorted, then each position either p-position or n-position, and can be computed by definition.
Take the Nim game as an example to do a calculation. For example, I just said that when there are only two piles of gravel and the number of stones equal to a win strategy, that is, this is a p-position, we rely on the definition to prove (3,3) is a p is a p-position. First (the 3,3) (i.e. the situation that can be caused by legal movement) has (0,3) (1,3) (2,3) (apparently swapping the position of the gravel heap does not affect its nature, so x,y and (y,x) are the same situation, you just have to figure out the nature of the three scenarios. (0,3) (0,0), (0,1), (0,2), in which (0,0) is obviously p-position, so (0,3) is n-position (as long as you find a child of p-position can be described as n-position). The successor (1,1) of (1,3) is p-position (because the only child (0,1) of (1,1) is n-position), so (1,3) is also n-position. The same can be proved (2,3) is n-position. So all the 3,3 are n-position, it's p-position. By a simple mathematical generalization, it can be strictly proved that "when there are two heaps of stones, the situation is p-position when and only when the number of stones is equal".
According to the above process, we can get a recursive algorithm-for the current situation, recursive calculation of the nature of all its sub-situation, if there is a child situation is p-position, then the move to this child situation is a win strategy. Of course, you may be acutely aware of a large number of overlapping problems, so you can use DP or memory search methods to improve efficiency (simple game problem think of this step). The problem is, using this algorithm, for a NIM game (A1,a2,..., an), to determine its nature and to find a winning strategy, it is necessary to calculate the nature of the O (a1*a2*...*an) Situation, no matter how memory can not reduce the complexity of the time. So we need a more efficient way of judging the nature of the Nim game situation.
Just say the conclusion well. (Bouton ' s theorem) for the situation of a Nim game (A1,a2,..., an), it is p-position when and only if a1^a2^...^an=0, where ^ represents an XOR operation. How, is not very magical. When I saw it, I thought it was magical, and it was completely unreasonable to relate to XOR or operation. But the proof of this theorem is not complex, basically is according to two kinds of position proof.
According to the definition, it is proved that a method of judging the nature of position can only prove three propositions: 1, this judgment will all terminal position as P-POSITION;2, The situation that was judged to be n-position on the basis of this judgment must be moved to a certain p-position;3, according to which the p-position situation could not be moved to a p-position.
The first proposition is clearly that terminal position is only one, that is, 0, different or still 0.
The second proposition, for a situation (A1,A2,..., an), if a1^a2^...^an!=0, there must be some legal movement, the AI changed to Ai ' after A1^a2^...^ai ' ^...^an=0. May be set a1^a2^...^an=k, there must be an AI, its binary representation at the highest level of K is 1 (otherwise k the highest bit that 1 is how to get). At this time ai^k< AI must be established. Then we can change the AI into Ai ' =ai^k, at this time A1^a2^...^ai ' ^...^an=a1^a2^...^an^k=0.
The third proposition, for a situation (A1,A2,..., an), if a1^a2^...^an=0, there must be no legal move, the AI changed to Ai ' after A1^a2^...^ai ' ^...^an=0. Because the XOR or operation satisfies the elimination rate, by A1^a2^...^an=a1^a2^...^ai ' ^...^an can get Ai=ai '. So changing AI to AI is not a legitimate move. Certificate of Completion.
According to this theorem, we can judge the nature of a nim's situation in O (n) time, and if it is n-position, we can find all the winning strategies in O (n) time. The NIM problem was basically solved perfectly. game Theory (ii): Sprague-grundy function
In the previous installment we looked at the Nim game and learned how to find a winning strategy. But if you change Nim's rules a few ways, can you quickly find a strategy to win? For example: There are n heaps of stones, every time from the 1th heap of stones to take 1, 2 or 3, can be from the 2nd heap of stones to take odd, you can from the 3rd heap and after the stone to take any of the stars ... It seems that the problem is a lot more complicated, but I believe that if you have mastered the content of this section, a similar variety of problems are not a problem.
Now we're going to look at a game that seems more general: given a forward-free graph and a piece on a starting vertex, the two-player alternately moves the piece along a forward edge and cannot be judged by the mover. In fact, this game can be considered as an abstract model for all impartial combinatorial games. That is to say, any ICG can be abstracted into this "graph game" by taking each situation as a vertex, and by having a forward side to each situation and its sub situation. Below we define the Sprague-garundy function on the vertex of a direction-free graph.
First defines the MEX (minimal excludant) operation, which is an operation exerted on a set, representing the smallest nonnegative integer that does not belong to this set. such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0.
For a given direction-free graph, the Sprague-garundy function for each vertex of the graph is defined as follows: g (x) =mex{g (y) | Y is the successor of X.
Take a look at the nature of the SG function. First of all, the vertex of all terminal position, that is, the vertex with no margin, has a SG value of 0 because its successor is the empty set. Then for a g (x) =0 vertex x, all subsequent y of it satisfies the G (Y)!=0. For a vertex of a G (x)!=0, there must be a successor Y satisfying g (y) = 0.
The above three sentences indicate that the postion represented by vertex X is p-position when and only if G (x) =0 (which corresponds to the three sentences defined by p-positioin/n-position). By calculating the SG value of each vertex of a direction-free graph, we can find a strategy to win each situation. But the purpose of the SG function is far less simple. If there will be more complex to map game, for example, there is not only a piece of the map, but there are n pieces, each time you can choose one to move, at this time, how to find a win strategy.
Let's consider the significance of the SG value of the vertex. When g (x) =k, it is shown that for any 0<=i< K, a successor Y of X is present to satisfy the G (Y) =i. In other words, when the SG value of a piece is k, we can turn it into 0, turn it into a 1 、......、 become k-1, but never keep k unchanged. I don't know if you can think of this as a NIM game, the rules of Nim's game are: each time you choose a bunch of pebbles, you can turn it into 0, turn it into a 1 、......、 become k-1, but never keep k unchanged. This indicates that if the SG value of the vertex of the N-pieces is considered to be the corresponding number of stones, then each strategy of the NIM game will correspond to the winning strategy of the original n pieces.
For n pieces, the SG values for their corresponding vertices are (a1,a2,..., an), respectively. A1,A2,..., an) when the Nim game a winning strategy is to turn the AI into K, then the original game of a winning strategy is to move the piece I pieces to a SG value of the vertex of K. It sounds a little too magical--how to circle around and back to the Nim game.
In fact, we still just have to prove that the situation of the p-position of this multiple chess game is that the SG function of the position of all the pieces is different or 0. This proof is almost exactly the same as the Bouton ' s theorem in the previous section, and it just needs to be replaced with a few nouns.
Just now, in order to make the problem seem easier, I think n pieces are moving on a graph. But if it is not on a forward graph, but on each piece on a map, you can choose one piece at a time (that is, optionally a direction graph) to move, which will not bring any changes to the conclusion.
So we can define the and (Sum of graph Games) of the graph game: Set G1, G2 、......、 gn is n a graph game, defined game G is G1, G2 、......、 gn and (Sum), game G's Move rule is: Choose a child game GI and move the top pieces. Sprague-grundy theorem is: g (g) =g (G1) ^g (G2) ^...^g (Gn). In other words, the value of the SG function of the game is the XOR of the value of the SG function of all its sub games.
Think again at the beginning of this article: any ICG can be abstracted into a graph game. So the concept of "SG function" and "game and" is not limited to a graph game. We define the SG value for each position of each ICG, and we can also define N ICG. So when we face a game made up of N games, the only way to find the SG value of each of the games for each game is to see all of the SG values as Nim's gravel heap, and then find the game's winning strategy in the way of the Nim's win strategy.
Back to the question at the beginning of this article. There are n heaps of stones, each can take 1 from the 1th pile of stones, 2 or 3, can be from the 2nd heap of stones to take odd, can be from the 3rd heap and after the stone to take any of the stars ... We can think of it as 3 games, the 1th child game only a pile of stones, each can take 1, 2, 3, it is easy to see the situation of X stone SG value is x%4. The 2nd child game is also only a pile of stones, each can take odd, after a simple drawing can know that the game has X stone when the SG value is x%2. The 3rd game has a n-2 heap of stones, is a NIM game. For each situation of the original game, the SG value of the three games is given the SG value of the whole game, and then the SG value can be used to determine whether there is a win strategy and make a decision. In fact, as a 3-child game or conservative, simply as n child games, one of the 1th, 2 child games as mentioned above, the 3rd and subsequent child games are "1 heap of stones, take several pieces each time," called "Any Stone game", this super simple game has x stone of the SG value is obviously x. In fact, n heap of Stone Nim game itself is not n "any stone game" and it.
So, for us, the SG function and the "game and" concept is not to let us mix and create bizarre games, but to the encounter of the seemingly complex game tried to divide into a number of sub games, for each game than the original games to simplify a lot of games to find its SG function, Then all different or up to get the original game of the SG function, you can solve the original game
SG function template
Play table:
void SG (int s[], int len, int n)//s array is a value that can be fetched at a time, Len is the length of the s array
{
memset (SG, 0, sizeof SG);
BOOL Vis[n];
for (int i = 1; I <= n; i++)
{
memset (vis, 0, sizeof vis);
for (int j = 0; J < Len; j +)
if (i >= s[j]) vis[sg[i-s[j]] = true;
for (int j = 0; J <= N; j +)
if (!vis[j])
{
sg[i] = j; break;
}
}}
Dfs
int SG (int x)
{
if (sg[x]!=-1) return sg[x];
BOOL Vis[n];
memset (Vis, 0, sizeof vis);
for (int i = 0; i < len; i++)
if (x >= s[i]) VIS[SG (x-s[i])] = true;
for (int i = 0; I <= N; i++)
if (!vis[i])
{
sg[x] = i; break;
}
}