Monday, January 30, 2017

Sprague-Grundy values

Hello Math 450,

The purpose of this post is to explore Sprauge-Grundy values. Before dwelling into the topic I would like to remind you of features that pertain to combinatorial games:

Impartial games
An impartial game is a two-player game in which the legal moves are solely based on position and each position has the same payouts between players. The only difference between players in an ideal impartial game is who goes first.

Normal play
Under the normal play rule, the last person to play wins.

P- and N-positions
Positions in the game can be classified accordingly, depending if the current player wins or if the bystanding player wins (granted that both players play optimally). A current player win is an N-position because the next player to play will win. Likewise a P-position is if the bystanding player wins because the previous player to play will win.

Note: All terminal positions are P-positions.
Note: From every N-position there is at least one move to a P-position.

OK.. Enough of that. Onward!

We have a decent understanding of combinatorial games. Now, we want to take that understanding and display it graphically. Impartial games can be represented using directed acyclic graphs. A directed acyclic graph is a finite graph with no directed cycles. These can be displayed on an xy-plane with vertices and edges.
Image result for edges graph definition
from: http://mathinsight.org/definition/undirected_graph



From graph to game, the vertices correspond to positions and edges represent a legal move from one position to another.

Mathematically speaking: "a directed graph, G, is a pair (X, F) where X is a nonempty set of vertices (positions) and F is a function that gives for each x ∈ X a subset of X, F(x) ⊂ X. For a given x ∈ X, F(x) represents the positions to which a player may move from x (called the followers of x). If F(x) is empty, x is called a terminal position."

A two-person game may be represented using graph G=(X,F) and by following these rules:

(1) Player 1 moves first, starting at a set opening position.
(2) Players alternate moves
(3) At position x, the player whose turn it is to move chooses a position y ∈ F(x).
(4) The player who is confronted with a terminal position at his turn, and thus cannot move, loses.

An impartial game (what we are working with) requires more regulation.

As of now, from the previous definitions, this game could continue for an infinite amount of moves. To fit the rules of the games that we are working with, such as Nim, we need to impose the property that no matter what starting point x is used, there is a number n, such that every path from x has length less than or equal to n. These graphs are considered progressively bounded, this means that there are no cycles.

This image depicts a directed a cyclic graph for a game where you start with 10 pieces and can take away 1,2, or 3 pieces. Notice how there is no repetition of moves. Notice anything else?

Image result for Sprague-Grundy graph
source: http://m.blog.csdn.net/article/details?id=21039309



Using the idea of N- and P- positions we can get graphs that look like this:
                                                    
source:http://www.gabrielnivasch.org/fun/combinatorial-games/sprague-grundy

Is it a good time to talk about Sprague-Grundy values?

Two mathematicians in the 1930's, Roland Sprague and Patrick Grundy, are credited with a function that will assign each position in combinatorial games a natural number.  The Sprague-Grundy function of a graph, G=(X, F), is a function, g, defined on X and taking non-negative integer values, such that

g(x) = mex{g(y) : y ∈ F(x)}. 

g(x) is defined recursively, meaning g(x) is defined in terms of g(y) for all followers y of x.

mex is a short way of saying the minimal excludant. The minimal excludant of a non-negative integer set is the smallest non-negative integer in that set.

Ex. Let M be the set {0, 1,3,4}
mex(M)=2

Ex. Let M be the set {0,2,100}
mex(M)= 1

The end-game Grundy value is equal to 0, thus terminal positions are when g(x)=0. Also, p-positions in a game will have a Grundy value of 0.

Example:
Let there be a game where a position consists of a pile of chips, and a legal move is to remove 1, 2 or 3 chips.

Imagine a table where x is the amount of chips and g is the Grundy function:
                x   0   1   2   3   4   5   6   7   8
             g(x) 0   1   2   3   0   1    2   3   0
g(x) outputs the corresponding Grundy value's for x. Notice how there is a Grundy value of 0 for 4 chips left.

That is, g(2) = mex{0, 1} = 2, g(3) = mex{0, 1, 2} = 3, and g(4) = mex{1, 2, 3} = 0.

In this example g(x)= x mod 4.

Another Example:
A game consists of a pile of chips. A legal move from a position with n chips is to remove any positive number of chips strictly smaller than n/2 + 1.

Here are the corresponding Grundy values for x being the number of chips and g(x) being the grundy values.

x       0   1   2   3   4
g(x)  0   1   0   2   1

The Grundy values may be hard to mentally visualize, but it may be easier to comprehend if you write it out. Instead of tables we can apply those graphs we were working on earlier.
source: http://www.gabrielnivasch.org/fun/combinatorial-games/sprague-grundy

So far Grundy values haven't told us much of anything new. We could compute nim-values to find the N- and P-positions.

Fortunately the Sprague-Grundy function lets us analyze Sums of Graph Games. 

Definition: The sum of two combinatorial games G1 and G2 is that game G where, for any move, a player may choose in which of the games G1 and G2 to play. The terminal position in G is (t1,t2), where t is terminal in Gi for i ∈ {1, 2}. We will write G = G1 + G2.

Every progressively bounded combinatorial game, G, in normal play is equivalent to a nim pile.

Need help imagining G1 and G2? Think of those nim piles we love so much.

Suppose we are given G1 and G2. Can we correctly play their sum G1+G2 if we know the N- and P- positions of the seperate games?

Unfortunately the answer is no.

It can be seen through observation that the sum of P-positions is always a P-position.. And the sum of an N-position and a P-position  will be a N-position. But what happens if we add the sums of N-positions? Ambiguity happens :(, not good. The Sprague-Grundy Function takes away this ambiguity.

The Sprague-Grundy function has two important properties:
           -A vertex v is a P-positoin iff g(v)=0
           - if G= G1+G2 and v=v1v2 is a position in G, then g(v) is the bitwise XOR (nimsums) of the binary representations of g(v1) and g(v2): g(v)= g(v1) ⊕ g(v2).

These properties imply that v=v1v2 is a P-position iff g(v1)=g(v2), since this is the only way the nim-sum can come out to zero.

The sums of games is a commutative and associative operation, this means we can understand ideal play with any impartial game as long as we know the Grundy function. 


Moving forward we can think about the most efficient path for achieving a win by looking at a graph.

"The most natural application of the Sprague-Grundy theory is for games that naturally decompose into sums of themselves.
Consider the following game: There is a checkerboard of size m × n, and there is an unlimited supply of polyominoes of a certain shape. On each turn, a player places a polyomino on an empty place on the board, and the player who cannot make a move is the loser:
During the course of the game, the board will gradually split into separate regions, for which we can calculate their Grundy values independently:
"
from source 3. 

Any questions or comments would be greatly appreciated.

sources:

1. Ferguson, Thomas S., F. Thomas Bruss, and Le Cam Lucien M. Game theory, optimal stopping, probability and statistics: papers in honor of Thomas S. Ferguson. Beachwood, OH: Institute of Mathematical Statistics, 2000.

2. Peres, Yuvul. "Stat 155 Game theory." http://www.stat.berkeley.edu/~yuvalweb/gath7.pdf.

3. "Sprague-Grundy theory - Gabriel Nivasch." Gabriel Nivasch. Accessed January 30, 2017. http://www.gabrielnivasch.org/fun/combinatorial-games/sprague-grundy.

Tuesday, January 24, 2017

The Game of Nim

Hello all-

Before I get to the game of Nim, there are a few things you all will need to know in order to understand my presentation.

Review

1. Binary Numbers

For the CS majors, and probably some of the Math majors, this will be review. If so, feel free to skip. For those who don't know (or don't remember) binary numbers, you can represent any base-10 number (Example: 5, 18, 135) using only zeroes and ones. To do so, you'll need to break the base-10 number down into powers of two.  In binary, the rightmost digit is representative of 20, the second rightmost digit is 21, the third rightmost is 22, and so on.

Example:

The number 5 can be written as the sum of 4 + 1, or 22 + 20.This means, that 5 in binary is 101, or 1*22 + 0*21 + 1*20.

Example:

The number 18 can be written as the sum of 16 + 2, or 24 + 21. Then, in binary, we would represent 18 as 10010.

Example:

Finally, the number 135 can be written as the sum of 128 + 4 + 2 + 1, or 27 + 22 + 21 + 20. Hence, in binary, 135 is 10000111.

Got it? Good. If not, Google it.

2. Exclusive-Or

The second thing you'll need to remember is the exclusive or operation. A simple way to remember this is to think of it as arithmetic mod 2. For example, 0 ⊕ 0 = 0, because 0+0 = 0 mod 2. Likewise, 1 ⊕ 1 = 0, because 1+1 = 2 = 0 mod 2. Be careful! One thing to be aware of is 1 ⊕ 1 ⊕ 1 = 1, since 1+1+1 = 3 = 1 mod 2. If this is confusing, don't worry too much. I'll spend some time on this in class.

Also feel free to Google this, or leave a question in the comments- I'll try to answer it in class.

3. N and P Positions

Make sure that you're comfortable with the idea of N and P positions, as we'll use them to analyze Nim. If you're iffy on this, review Natasha's blog post below.


On to Nim!

Ok, so the way Nim works is similar to the subtraction game we played with Natasha in class.

Rules:

  1. Start with three piles of objects, call them coins.
  2. On your turn, you may remove any number of coins from any pile, but only that pile.
    • Example move: Take one coin from one pile, leaving the rest in the pile.
    • Example move 2: Take an entire pile, so that there are only two piles left.
  3. The game is over when all the coins are gone, or your three piles each have zero coins in them.
  4. The person who took the last coin (or pile of coins) wins.
You can play Nim with three piles here by clicking on Level 4. We will also probably play a game as a class during my presentation, just to clear everything up.


The Big Question: When in Nim are you in a P-Position, and when are you in an N-Position?


Before I tell you the answer, feel free to write some conjectures in the comments!

Done? Okay.

It turns out you can calculate whether or not a situation in Nim is a P-Position or N-Position, using binary numbers and exclusive or to calculate the Nim Sum.

Then what's the Nim Sum?

The nim sum is the binary representations of the number of objects in your piles, exclusive-ored together.

Huh?

So, let's say you have three piles in your game, with 13, 12, and 8 objects respectively. Then in binary, you have 1101 (13), 1100 (12), and 1000 (8) objects in each pile. To exclusive-or them, we can line them up vertically, and do our mod 2 arithmetic.


So, our answer is 1001, which in base-10, equals 9. This 9 is our nim sum!

How does that relate to P-Positions and N-Positions?

It turns out, that in any given situation, if your nim sum equals zero, you're in a P-Position. Now let's prove it.


Bouton's Theorem


Bouton's Theorem states that a position in Nim, (x1, x2, x3) is a P-Position if and only if the nim sum of its components is zero, x1 ⊕ x2 ⊕ x3 = 0.

In other words, this is the claim that we need to prove.

Proof

Just like in our Subtraction game proof, let's start our proof by building some sets, and then we'll show that they consist of all the situations that satisfy the definition of P and N Positions.

$$\textit{Let} \ P = \{(x_1, x_2, x_3)| x_1 \oplus x_2 \oplus x_3 = 0\}$$

$$\textit{Let} \ N = \{(x_1, x_2, x_3)| x_1 \oplus x_2 \oplus x_3 > 0\}$$

Also note that there is only one terminal position in Nim, (0,0,0), when the last of the coins has been taken.

Step 1: Show all terminal positions are in P

    This is fairly obvious. We have one terminal position, (0,0,0), and 0 ⊕ 0 ⊕ 0 = 0,
so it's in our set P.

Step 2: For any n ∈ N, you can move to a situation in P.

    Let (n1, n2, n3) be a situation  N. Then n1 ⊕ n2 ⊕ n3 > 0.

    Hence our nim sum is greater than zero.

    So, set up your nim sum columns, and compute your nim sum in binary. Go to the leftmost column in your sum that has a 1. We know that there is one, since our nim sum is greater than zero. Pick one of the piles (n1, n2, or n3) that also has a 1 in that column. Make that 1 a 0, and adjust the rest of the columns of that pile so that you have all 0's in your nim sum.

Note: We will do examples of this in class, because I know it's confusing to read about.

    How do we know we made a legal move? Since the leftmost column is being reduced from a 1 to a 0, we know we're taking objects away from that pile, even if we change the rest of the number to 1's. Hence, you can always take away some number of objects from one pile to move into a P-Position if your nim sum is > 0.

Step 3: For any p ∈ P, you have to move to a situation in N.

    Let (p1, p2, p3) be a situation  P. Then p1 ⊕ p2 ⊕ p3 = 0.

    Then when you set up your nim sum columns, the nim sum is comprised entirely of zeros. Remember: another way to think of exclusive or is arithmetic mod 2. This means that there is no way to change only one pile so that you keep zeros in all the columns. Since you can only change the numbers from zeros to ones and vice versa, changing only one pile will always change one of your nim sum columns from a zero to a one, creating a nim sum > 0.

    Hence, from any position in P, you must move to a position in N.


DONE!

Conclusion

What does all this mean? Basically, it means that for any situation in Nim, it is always possible to figure out whether or not you're in a P-Position or an N-Position. Why is this important? We'll discuss in upcoming classes how Nim is representative of all combinatorial impartial games, but that's beyond the scope of my presentation. Throughout this post, we've discussed and analyzed the game of Nim, using nim sums, and we've talked about how they relate to N and P Positions. We've also proved Bouton's Theorem true, using the same model discussed in class. I'll leave you with some ideas for discussion topics, either in the comments or class:

  • Misere Nim: a variation where the last person to take coins loses
  • Other variations on Nim
  • Does Bouton's Theorem apply to Nim with more than 3 piles?
  • Anything you have questions on
Citations:

Bouton, Charles L. "Nim, A Game with a Complete Mathematical Theory." The Annals of Mathematics 3.1/4 (1901): 35-39. Web.

Ferguson, Thomas S. "Game Theory." (n.d.): n. pag. Web. <http://myslu.stlawu.edu/~nkomarov/450/comb.pdf>.

"Theory of Impartial Games." The Mathematics of Toys and Games. N.p.: n.p., 2009. N. pag. Print.