You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/game_theory/sprague-grundy-nim.md
+38-39Lines changed: 38 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,20 +3,20 @@
3
3
4
4
## Introduction
5
5
6
-
This theorem describes the so-called **impartial** two-player games,
7
-
that is those in which the available moves and winning/losing depends only on the state of the game.
6
+
This theorem describes the so-called **impartial** two-player game,
7
+
i.e. those in which the available moves and winning/losing depends only on the state of the game.
8
8
In other words, the only difference between the two players is that one of them moves first.
9
9
10
-
Additionally, we assume that the game has **perfect information**, i.e. no information is hidden from the players (they know the rules and the possible move).
10
+
Additionally, we assume that the game has **perfect information**, i.e. no information is hidden from the players (they know the rules and the possible moves).
11
11
12
-
It is assumed that the game is **finite**, i.e. sooner or later a player will come to a losing position — from which he can't move to another position.
12
+
It is assumed that the game is **finite**, i.e. after a certain number of moves, one of the players will end up in a losing position — from which they can't move to another position.
13
13
On the other side, the player who set up this position for the opponent wins.
14
14
Understandably, there are no draws in this game.
15
15
16
-
Such games can be completely described by an*directed acyclic graph*: the vertices are game states, the edges are transitions (moves).
17
-
A vertex without outgoing edges is a losing one (a player who must make a move from this vertex loses).
16
+
Such games can be completely described by a*directed acyclic graph*: the vertices are game states and the edges are transitions (moves).
17
+
A vertex without outgoing edges is a losing vertex (a player who must make a move from this vertex loses).
18
18
19
-
Since there are no draws, we can classify all game states as either **winning or losing**.
19
+
Since there are no draws, we can classify all game states as either **winning** or **losing**.
20
20
Winning states are those from which there is a move that causes inevitable defeat of the other player, even with their best response.
21
21
Losing states are those from which all moves lead to winning states for the other player.
22
22
Summarizing, a state is winning if there is at least one transition to a losing state and is losing if there isn't at least one transition to a losing state.
@@ -27,8 +27,8 @@ The theory of such games was independently developed by Roland Sprague in 1935 a
27
27
28
28
## Nim
29
29
30
-
This game falls under the restrictions above.
31
-
Moreover, **any** perfect-information impartial two-player game can be reduced to the game of Nim.
30
+
This game obeys the restrictions described above above.
31
+
Moreover, *any* perfect-information impartial two-player game can be reduced to the game of Nim.
32
32
Studying this game will allow us to solve all other similar games, but more on that later.
33
33
34
34
Historically this game was popular in ancient times.
@@ -40,7 +40,7 @@ The name was given by Charles Bouton, who in 1901 published a full analysis of t
40
40
41
41
There are several piles, each with several stones.
42
42
In a move a player can take any positive number of stones from any one pile and throw them away.
43
-
A player loses if he can't make a move — all the piles are empty.
43
+
A player loses if they can't make a move, which happens when all the piles are empty.
44
44
45
45
The game state is unambiguously described by a multiset of positive integers.
46
46
A move consists of strictly decreasing a chosen integer (if it becomes zero, it is removed from the set).
@@ -54,16 +54,16 @@ The current player has a winning strategy if and only if the xor-sum of the pile
54
54
The xor-sum of a sequence $a$ is $a_1 \oplus a_2 \oplus \ldots \oplus a_n$, where $\oplus$ is the *bitwise exclusive or*.
55
55
56
56
**Proof.**
57
-
The main point of the proof is the presence of a **symmetric strategy for the opponent**.
57
+
The key to the proof is the presence of a **symmetric strategy for the opponent**.
58
58
We show that a once in a position with the xor-sum equal to zero, the player won't be able to make it non-zero in the long term —
59
-
if he transitions to a position with a non-zero xor-sum, the opponent will always have a move returning the xor-sum back to zero.
59
+
if they transition to a position with a non-zero xor-sum, the opponent will always have a move returning the xor-sum back to zero.
60
60
61
61
We will prove the theorem by mathematical induction.
62
62
63
63
For an empty Nim (where all the piles are empty i.e. the multiset is empty) the xor-sum is zero and the theorem is true.
64
64
65
65
Now suppose we are in a non-empty state.
66
-
Using the assumption of induction (and the acyclicity of the game) we believe that the theorem is proven for all states reachable from the current one.
66
+
Using the assumption of induction (and the acyclicity of the game) we assume that the theorem is proven for all states reachable from the current one.
67
67
68
68
Then the proof splits into two parts:
69
69
if for the current position the xor-sum $s = 0$, we have to prove that this state is losing, i.e. all reachable states have xor-sum $t \neq 0$.
@@ -72,33 +72,37 @@ If $s \neq 0$, we have to prove that there is a move leading to a state with $t
72
72
* Let $s = 0$ and let's consider any move.
73
73
This move reduces the size of a pile $x$ to a size $y$.
74
74
Using elementary properties of $\oplus$, we have
75
-
$$t = s \oplus x \oplus y = 0 \oplus x \oplus y = x \oplus y. $$
75
+
76
+
$$t = s \oplus x \oplus y = 0 \oplus x \oplus y = x \oplus y$$
77
+
76
78
Since $y < x$, $y \oplus x$ can't be zero, so $t \neq 0$.
77
79
That means any reachable state is a winning one (by the assumption of induction), so we are in a losing position.
78
80
79
81
* Let $s \neq 0$.
80
82
Consider the binary representation of the number $s$.
81
83
Let $d$ be the number of its leading (biggest value) non-zero bit.
82
-
Our move will be on a pile whose size's bit number $d$ is set (It must exist, otherwise the bit wouldn't be set in $s$).
84
+
Our move will be on a pile whose size's bit number $d$ is set (it must exist, otherwise the bit wouldn't be set in $s$).
83
85
We will reduce its size $x$ to $y = x \oplus s$.
84
86
All bits at positions greater than $d$ in $x$ and $y$ match and bit $d$ is set in $x$ but not set in $y$.
85
87
Therefore, $y < x$, which is all we need for a move to be legal.
86
88
Now we have:
87
-
$$ t = s \oplus x \oplus y = s \oplus x \oplus (s \oplus x) = 0. $$
89
+
90
+
$$ t = s \oplus x \oplus y = s \oplus x \oplus (s \oplus x) = 0$$
91
+
88
92
This means we found a reachable losing state (by the assumption of induction) and the current state is winning.
89
93
90
94
**Corollary.**
91
95
Any state of Nim can be replaced by an equivalent state as long as the xor-sum doesn't change.
92
96
Moreover, when analyzing a Nim with several piles, we can replace it with a single pile of size $s$.
93
97
94
-
## The equivalence of impartial games and Nim. Sprague-Grundy theorem
98
+
## The equivalence of impartial games and Nim (Sprague-Grundy theorem)
95
99
96
100
Now we will learn how to find, for any game state of any impartial game, a corresponding state of Nim.
97
101
98
-
### Lemma about Nim with increasing
102
+
### Lemma about Nim with increases
99
103
100
104
We consider the following modification to Nim: we also allow **adding stones to a chosen pile**.
101
-
The exact rules about how and when increasing is allowed **do not interest us**, however the rules should keep our game **acyclic**. In later sections example games are considered.
105
+
The exact rules about how and when increasing is allowed **do not interest us**, however the rules should keep our game **acyclic**. In later sections, example games are considered.
102
106
103
107
**Lemma.**
104
108
The addition of increasing to Nim doesn't change how winning and losing states are determined.
@@ -110,14 +114,14 @@ Since the game is acyclic, sooner or later the current player won't be able to u
110
114
111
115
### Sprague-Grundy theorem
112
116
113
-
**Sprague-Grundy theorem.**
114
-
Let's consider a state $v$ of a two-player impartial game and let $v_i$ be the states reachable from it
115
-
(where $i \in \\{ 1, 2, \dots, k \\} , k \ge 0$ ).
116
-
To this state we can assign a fully equivalent game of Nim with one pile of size $x$.
117
+
Let's consider a state $v$ of a two-player impartial game and let $v_i$ be the states reachable from it (where $i \in \{ 1, 2, \dots, k \} , k \ge 0$).
118
+
To this state, we can assign a fully equivalent game of Nim with one pile of size $x$.
117
119
The number $x$ is called the Grundy value or nim-value of state $v$.
118
120
119
121
Moreover, this number can be found in the following recursive way:
120
-
$$ x = \text{mex} \\{ x_1, \ldots, x_k \\}, $$
122
+
123
+
$$ x = \text{mex} \{ x_1, \ldots, x_k \}, $$
124
+
121
125
where $x_i$ is the Grundy value for state $v_i$ and the function $\text{mex}$ (*minimum excludant*) is the smallest non-negative integer not found in the given set.
122
126
123
127
Viewing the game as a graph, we can gradually calculate the Grundy values starting from vertices without outgoing edges.
@@ -129,18 +133,18 @@ We will use a proof by induction.
129
133
For vertices without a move, the value $x$ is the $\text{mex}$ of an empty set, which is zero.
130
134
That is correct, since an empty Nim is losing.
131
135
132
-
Now consider any other vertex.
133
-
By induction, we assume the values $x_i$ corresponding to the reachable vertices are already calculated.
136
+
Now consider any other vertex $v$.
137
+
By induction, we assume the values $x_i$ corresponding to its reachable vertices are already calculated.
134
138
135
-
Let $p = \text{mex} \\{ x_1, \ldots, x_k \\} $.
139
+
Let $p = \text{mex} \{ x_1, \ldots, x_k \}$.
136
140
Then we know that for any integer $i \in [0, p)$ there exists a reachable vertex with Grundy value $i$.
137
-
This means $v$ is **equivalent to a state of the game of Nim with increasing with one pile of size $p$**.
141
+
This means $v$ is **equivalent to a state of the game of Nim with increases with one pile of size $p$**.
138
142
In such a game we have transitions to piles of every size smaller than $p$ and possibly transitions to piles with sizes greater than $p$.
139
143
Therefore, $p$ is indeed the desired Grundy value for the currently considered state.
140
144
141
145
## Application of the theorem
142
146
143
-
Finally, we describe an algorithm determining the winning/losing status applicable to any impartial two-player game.
147
+
Finally, we describe an algorithm to determine the win/loss outcome of a game, which is applicable to any impartial two-player game.
144
148
145
149
To calculate the Grundy value of a given state you need to:
146
150
@@ -160,18 +164,18 @@ We can xor-sum them just like usual Nim according to Bouton's theorem.
160
164
161
165
## Patterns in Grundy values
162
166
163
-
Very often when solving specific tasks using Grundy values it may be beneficial to **study the table of the values** in search of patterns.
167
+
Very often when solving specific tasks using Grundy values, it may be beneficial to **study the table of the values** in search of patterns.
164
168
165
169
In many games, which may seem rather difficult for theoretical analysis,
166
170
the Grundy values turn out to be periodic or of an easily understandable form.
167
171
In the overwhelming majority of cases the observed pattern turns out to be true and can be proved by induction if desired.
168
172
169
-
However, Grundy values are far from *always* containing such regularities and for some even very simple games
170
-
the problem if those regularities exist is still open (eg. "Grundy's game").
173
+
However, Grundy values are far from *always* containing such regularities and even for some very simple games, the problem asking if those regularities exist is still open (e.g. "Grundy's game").
171
174
172
175
## Example games
173
176
174
177
### Crosses-crosses
178
+
175
179
**The rules.**
176
180
Consider a checkered strip of size $1 \times n$. In one move, the player must put one cross, but it is forbidden to put two crosses next to each other (in adjacent cells). As usual, the player without a valid move loses.
177
181
@@ -184,14 +188,9 @@ into two strips of length $i-2$ and $n-i-1$ i.e. we go to the sum of games $i-2$
184
188
For the edge case of the cross being marked on position $1$ or $n$, we go to the game $n-2$.
Copy file name to clipboardExpand all lines: src/num_methods/roots_newton.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,19 @@
2
2
3
3
# Newton's method for finding roots
4
4
5
-
This is an iterative method invented by Isaac Newton around 1664. However, sometimes this method is called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was published much earlier.
5
+
This is an iterative method invented by Isaac Newton around 1664. However, this method is also sometimes called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was published much earlier.
6
6
7
7
The task is as follows. Given the following equation:
8
8
9
9
$$f(x) = 0$$
10
10
11
-
We want to solve the equation, more precisely, to find one of its roots (it is assumed that the root exists). It is assumed that $f(x)$ is continuous and differentiable on an interval $[a; b]$.
11
+
We want to solve the equation. More precisely, we want to find one of its roots (it is assumed that the root exists). It is assumed that $f(x)$ is continuous and differentiable on an interval $[a, b]$.
12
12
13
13
## Algorithm
14
14
15
15
The input parameters of the algorithm consist of not only the function $f(x)$ but also the initial approximation - some $x_0$, with which the algorithm starts.
16
16
17
-
Suppose we have already calculated $x_i$, calculate $x_{i+1}$ as follows. Draw the tangent to the graph of the function $f(x)$ at the point $x = x_i$, and find the point of intersection of this tangent with the $x$-axis. $x_{i+1}$ is set equal to the $x$-coordinateof the point found, and we repeat the whole process from the beginning.
17
+
Suppose we have already calculated $x_i$, calculate $x_{i+1}$ as follows. Draw the tangent to the graph of the function $f(x)$ at the point $x = x_i$, and find the point of intersection of this tangent with the $x$-axis. $x_{i+1}$ is set equal to the $x$-coordinate of the point found, and we repeat the whole process from the beginning.
18
18
19
19
It is not difficult to obtain the following formula:
Copy file name to clipboardExpand all lines: src/num_methods/simpson-integration.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,18 +10,17 @@ The solution described here was published in one of the dissertations of **Thoma
10
10
11
11
## Simpson's formula
12
12
13
-
Let $n$ be some natural number. We divide the integration segment $[a; b]$ into $2n$ equal parts:
13
+
Let $n$ be some natural number. We divide the integration segment $[a, b]$ into $2n$ equal parts:
14
14
15
15
$$x_i = a + i h, ~~ i = 0 \ldots 2n,$$
16
16
$$h = \frac {b-a} {2n}.$$
17
17
18
18
Now we calculate the integral separately on each of the segments $[x_ {2i-2}, x_ {2i}]$, $i = 1 \ldots n$, and then add all the values.
19
19
20
-
21
20
So, suppose we consider the next segment $[x_ {2i-2}, x_ {2i}], i = 1 \ldots n$. Replace the function $f(x)$ on it with a parabola $P(x)$ passing through 3 points $(x_ {2i-2}, x_ {2i-1}, x_ {2i})$. Such a parabola always exists and is unique; it can be found analytically.
22
21
For instance we could construct it using the Lagrange polynomial interpolation.
23
22
The only remaining thing left to do is to integrate this polynomial.
24
-
If you do this for a general function $f$, you receive a remarkable simple expression:
23
+
If you do this for a general function $f$, you receive a remarkably simple expression:
0 commit comments