Steve Alpern's Overhead Slides Nim Games and Impartial Games

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Steve Alpern’s overhead slides

Nim Games and Impartial Games

This file contains my overhead slides from the first two weeks of MA300/301/402.
In response to the requests of several students, I am putting them on the course
website. They are based closely on the course notes of Prof Von Stengel. These
slides are not really meant to be read on their own, but to be seen at the same
sime as my spoken commentary and my proofs or examples presented on the
visualizer. For reading at home, the course notes should be a better option.
The Game of Nim
• Nim(a,b,c) begins with ‘heaps’ sizes a,b,c
• Each player to move removes as many marbles
as he likes from any one pile
• Player I moves first, then II, then I, etc.
• The first player who cannot move (who faces all
empty heaps) loses.
• Either I or II has a winning strategy, for any
game Nim (a,b,c) (theorem, proof later)
• Nim(a,b,c) called ‘winning’ if I has win strat.
Examples
• Try Nim(1,2,3) with your neighbour.
• Try Nim(9,9)
• Try Nim (83,47)
Directed Graph Method to Solve Nim

Start, no in edges 1,2

2 1 1,1

Label position winning


(green) if it has a losing
No out edges 0
(red) option; otherwise
Label it losing.

Directed graph with no directed cycles


Tree Version
1,2

2 1 1,1

1 0 0 1

0 0
Nim Sum
8 4 2 1
a 3 0 0 1 1
b 13 1 1 0 1
c 9 1 0 0 1

nim sum 7 0 1 1 1

To calculate nim sum, sum of a column of 0’s and 1’s is 0 if there are an
even number of 1’s, and sum is 1 if odd number of 1’s. In other words the
sum for each column is the sum modulo 2.

Write: 3 + 13 + 9 = 7, or nim sum of 3, 13, and 9


Find nim sum of 4 and 2, find nim sum of 5 and 3.
How to: unbalanced->balanced
A Nim position is ‘balanced’ if all columns have even number of 0’s,
that is, if it’s nim sum is 0. Why is Nim(m,m) balanced, m + m =0?
We can convert unbalance pos. to balanced one.
8 4 2 1
a 3 0 0 1 1
b 13 1 1 0 1
c 9 1 0 0 1

nim sum 7 0 1 1 1

10 1 0 1 0

a' 3 0 0 1 1
b' 10 1 0 1 0
c' 9 1 0 0 1

nim sum 0 0 0 0 0

Locate leftmost 1 in nim sum (in 4 column), take away from a row with a
1 in this column (second row). Change right part of this row to make sum 0
Winning & Losing Positions
• A position is winning if one of its options is
losing
• A position is losing is all of its options are
winning
• Recall the options of a position are the
positions you can leave it in after you
move
Winning unless nim sum zero
• Unbalanced positions have a balanced
option (two slides back)
• Balanced positions have only unbalanced
options (changing a single digit in a
balanced column unbalances it)
• So if you have an unbalanced position and
balance it, you will get back a balanced
position, and can never lose (the losing
position 0=0000 is balanced)
Winning and losing positions
• Unbalanced (nim sum not 0) winning
• Balanced (nim sum 0) losing, as any move
gives other player a winning position

To illustrate, I’ll play Nim(3,13,9) against the class.

Lemma: Nim(m,n) is a losing position (game) if and only if m=n.


Impartial Games
• Two players, move alternately among the
• Positions
• Rules specify options, positions you may
move to from given position
• Ending condition (no directed cycles)
• Player with no options (unable to
move)loses
• (So perfect information, no chance moves)
Directed graph, no directed cycles
Start

Immediate losing
No out arrows, no
options

Idea: Cursor or marker starts at Start, then players alternate moving


It along arrows until a player is unable to move.
Directed graph, no directed cycles
start

Immediate losing
No out arrows, no
options

Winning positions green, losing positions red


Tree version, position has history
start

Immediate losing
No out arrows, no
options

Winning positions green, losing positions red


One Heap Nim as directed graph
3

0
Nim(5,4) as directed graph

(5,4)

Arrows go left or down,


any number of steps,
So 9 options from (5,4)

(0,0)
We denote a single heap Nim game by *n.

Def: If G is a game and H is reachable from G, say H is simpler than G.

We often prove a property for a game by showing it is true if it is true


of all simpler games

Lemma 1.2: In any Game G, either player I can force a win or II can.

Proof: (already gave algorithmic proof, by labeling positions in graph)


If game has no moves I loses, so we say II can force a win.
Next assume all Lemma holds for all games simpler than G. All the option
of G are simpler games. If II wins in all of them, then II can force a win in G
Otherwise, I can force a win by moving to an option which is losing.
Sum of Two Games
• Definition: In the sum G+H of two games
G,H, a player can move in either game.
First player who can’t move in either game
loses.
• Example: a two heap Nim game is the
sum of two single heap Nim games.
• Example: we can start a directed graph
game with two markers at distinct postions
Properties of + in G+H
• G+H=H+G
• G+(H+J)=(G+H)+J=G+H+J (is well defined)
• G+*0=G

• Note G+G is a losing game, for any G


Equivalent Games
• Games G and H equivalent (G==H) if
G+J losing if and only if J+H losing

So a player would never G+J to G+H or


other way around.
Lemma 1.5: If G is losing, G==*0
Idea: if J losing, II plays in G after I does,
eventually wins in J, so J+G losing.
Inductive proof in notes.
• Lemma 1.6: G+G==0 (copy cat principle)
• Lemma 1.7: G==H iff G+H==*0
• Lemma 1.8: G==H is options of G are
equivalent to options of H.
• Lemma 1.9:(Single Heap Nim) *m==*n
implies m=n.
• Proof 1.9: By 1.7 *m==*n iff *m+*n==*0 (is
losing). But then by Lemma 1.1 , m=n.
Equivalent Nim Games
• Two Nim games are
equivalent iff they
have the same Nim
sums.
• Example G=Nim(3,4)
mex rule
• mex(S)=least non-negative integer not in
S
• Example mex {0,1,2,5,7,11}=3
• mex{1,2,3,4,5}=0
• Nimvalue of position is mex of nim values
of its options
Using mex rule

*0 *0

*0

Stronger version of algorithm used to determine winning, losing positions


Example
• Nim(1,2)==*3, with 1 0 1 (powers of 2 add nicely)

options 2 1 0
• Nim(1,0)==*1 3 1 1
• Nim(1,1)==*0
(copycat)
• Nim(2,0)==*2
• mex rule
• 3=mex{1,0,2}
Every I-Game == to Nim game

Proof. Use induction on game size. If true for all options, then true for
original game.

You might also like