Game SG function

Source: Internet
Author: User

Turn from: Sprague-grundy function-sg Function--Game theory (3)

The Sprague-grundy theorem of fair play

Fair Play is a two-player game in which both parties have complete information and are not involved, and the legal operation of any state is the same for both parties.

A fair game can be represented abstractly by a forward-and-loop graph in which each point corresponds to this state, and each has a forward-side representation of a legitimate operation from one state to another.

We can imagine that a token is initially placed on a point, and then the two players take turns moving it from the current point to its successor point. The current currency moves to the meeting point when the game is over, the meeting point is a non-out point, and the last player who needs to operate is the winner.

P-and N-states

If both sides play according to the best strategy, we can use each state in the game according to whether it is the winner or the winning category.

A winning state is considered to be a N-state (because the next player is about to win), a win status is considered a P-state (because the previous player is about to win)

The P-and N-states are described in an inductive manner as follows:

A point v is a P-state when and only if all its successors are N-states

A point v is N-state when and only if some of its successors are P-states

This induction begins at the meeting point, where the meeting point is P-state because it clearly satisfies the P-state requirement.

The P-and N-state information of the game provides its winning strategy. If it's our turn and the game is in a N-state, we should move to a P-state. Then our opponent will be forced into the N-state, and so on. We will eventually move into a meeting point and win.

Game of and

If G1 and G2 are fair games, then their and G1 + G2 is another fair game, play as follows: Each turn, a player chooses G1, one of the G2 (whichever he wants) then plays it and does not touch another game. The game ends when both G1 and G2 are unable to operate.

Formally, if G1 = (V1, E1) and G2 = (V2, E2) are game graphs, then their and gsum = (Vsum, esum) are defined as:

Vsum = V1xv2,

Esum = {(V1v2, w1v2) | (v1, W1) ∈e1}∪{(V1V2, v1w2) | (v2, W2) ∈e2}.

Now, suppose we give two games G1 and G2. If we only know the P-state and N-states of individual games can we play good games and G1 + G2 correctly? The answer is in the negative. It is not difficult to see two P-states and always P-states, P-states and N-states and always N-states. But two n-states and both may be P-states and may be N-states. Therefore, it is not enough to know only the P-state and N-states of a single game.

In order to play good games correctly and we need to promote p-states and N-states, it is the Sprague-grudy function (or simply the Grundy function).

The Sprague-grundy function

Sprague-grundy function

Make n = {0, 1, 2, 3, ...} A collection of natural numbers. The Sprague-grundy function assigns a natural number to each state in the game. The Grundy value of node v equals the minimum natural number that does not appear in the subsequent Grundy value of V.

Form: Given a finite subset of S⊂n, the Mex S (minimum repulsion value) is the minimum natural number that does not appear in S

Mex s = min (N s).

Now, given a game graph g= (v,e), its sprague-grundy function g:v→n inductive is defined as

G (v) = Mex {g (W) | (V, W) ∈e}.

Starting from the meeting point of G, we know that its Grundy value is 0.

The Sprague-grundy function satisfies two important properties:

Point v is a P-state when and only if G (v) =0

If G = G1 + G2 and V = v1v2 is a state of G, then G (v) is g (V1) and G (V2) XOR in binary:

G (v) = g (v1) ⊕g (v2).

operator ⊙ is also called Nim and . For example, 3⊕5 = 011⊕101 = 110 = 6. Similarly, 3⊕6 = 5 and 5⊕6 = 3.

It is not difficult to use inductive method to prove the above two properties.

According to these properties there is V = v1v2 is P-state when and only if g (v1) = g (v2), because this is the only way to make Nim and 0.

Undoubtedly, the sum of the game is to satisfy the Exchange law and the binding law of the operation, Nim and operation is also.

Therefore, we can play any number of games correctly by knowing the Grundy function of a single game.

Our strategy is as follows: if it's our turn and the game's Grundy value gives a non-0 nim, then there must be an action in one component of the game that makes the NIM and becomes 0. We should do this, then our opponent will be forced to make NIM and not 0 again. Eventually, we will be the last person to perform the last action in the game, and finally the Nim and become 0.

The game of Nim

Nim Games

The most basic fair game is the Nim heap. A Nim heap consists of a fixed number of tokens. In each turn, a player takes 1 or more tokens from the heap to the entire heap. Take the whole pile of people to win.

This game is meaningless if it is independent: the initiator player can take all tokens and win immediately!

But if we add the NIM heaps of all sizes together, we get the famous Nim game.

N is the Grundy value of the Nim heap of size n. Therefore, the Grundy values for each state in the Nim game are Nim and for each heap size.

Games this decompose into sums of themselves

Some of the games that break up into themselves and

The most natural application of the Sprague-grundy theorem is some of the games that decompose into itself.

Consider this game: there is a chessboard of size m*n, and there is an infinite number of dominoes supplied in a particular shape. In each turn, the player places a domino on the board, and the player who cannot play the Dominoes is the loser.

During the game, the chessboard is gradually divided into different areas, and we can calculate the Grundy value separately.

For another example, consider Grundy games. A state of the game consists of a stack of tokens of different sizes, one operation consisting of just one heap and dividing it into two unequal heaps. The game ends when the size of all the heaps is only 1 and 2, because it can no longer be divided.

Makes G (n) The Grundy value for a single heap of size n. The series G (n) is as follows:

n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

g (n): 0 0 1 0 2 1 0 2 1 0 2 1 3 2 1 3 2 4 3 0

Like what:

When n equals 1, 2 o'clock satisfies the condition, that is no longer taken, there is no next situation, so G (1) ={}; so g (1) ={0,1,2,3,4 ...};

So g (1) = 0; g (2) = 0; recursion, G (3), G (4), G (5), etc.

For example: g (6) ={# (1,5), # (2,4)}={g (1) +g (5), G (2) +g (4)}=g (2,0);

So G (6) ={1,3,4,5,6 ...}, so g (6) = 1;

The method of solving this problem, see the bottom of my blog for the value of f (n): http://www.cnblogs.com/hsqdboke/archive/2012/04/20/2459796.html

Game SG function

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.