Skip to content

Commit 376b308

Browse files
AbhijeetKrishnanjakobkogler
authored andcommitted
Fix grammar + formatting errors in 4 files (cp-algorithms#494)
1 parent 7392f22 commit 376b308

4 files changed

Lines changed: 58 additions & 59 deletions

File tree

src/game_theory/sprague-grundy-nim.md

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
## Introduction
55

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.
88
In other words, the only difference between the two players is that one of them moves first.
99

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).
1111

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.
1313
On the other side, the player who set up this position for the opponent wins.
1414
Understandably, there are no draws in this game.
1515

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).
1818

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**.
2020
Winning states are those from which there is a move that causes inevitable defeat of the other player, even with their best response.
2121
Losing states are those from which all moves lead to winning states for the other player.
2222
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
2727

2828
## Nim
2929

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.
3232
Studying this game will allow us to solve all other similar games, but more on that later.
3333

3434
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
4040

4141
There are several piles, each with several stones.
4242
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.
4444

4545
The game state is unambiguously described by a multiset of positive integers.
4646
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
5454
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*.
5555

5656
**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**.
5858
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.
6060

6161
We will prove the theorem by mathematical induction.
6262

6363
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.
6464

6565
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.
6767

6868
Then the proof splits into two parts:
6969
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
7272
* Let $s = 0$ and let's consider any move.
7373
This move reduces the size of a pile $x$ to a size $y$.
7474
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+
7678
Since $y < x$, $y \oplus x$ can't be zero, so $t \neq 0$.
7779
That means any reachable state is a winning one (by the assumption of induction), so we are in a losing position.
7880

7981
* Let $s \neq 0$.
8082
Consider the binary representation of the number $s$.
8183
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$).
8385
We will reduce its size $x$ to $y = x \oplus s$.
8486
All bits at positions greater than $d$ in $x$ and $y$ match and bit $d$ is set in $x$ but not set in $y$.
8587
Therefore, $y < x$, which is all we need for a move to be legal.
8688
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+
8892
This means we found a reachable losing state (by the assumption of induction) and the current state is winning.
8993

9094
**Corollary.**
9195
Any state of Nim can be replaced by an equivalent state as long as the xor-sum doesn't change.
9296
Moreover, when analyzing a Nim with several piles, we can replace it with a single pile of size $s$.
9397

94-
## The equivalence of impartial games and Nim. Sprague-Grundy theorem
98+
## The equivalence of impartial games and Nim (Sprague-Grundy theorem)
9599

96100
Now we will learn how to find, for any game state of any impartial game, a corresponding state of Nim.
97101

98-
### Lemma about Nim with increasing
102+
### Lemma about Nim with increases
99103

100104
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.
102106

103107
**Lemma.**
104108
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
110114

111115
### Sprague-Grundy theorem
112116

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$.
117119
The number $x$ is called the Grundy value or nim-value of state $v$.
118120

119121
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+
121125
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.
122126

123127
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.
129133
For vertices without a move, the value $x$ is the $\text{mex}$ of an empty set, which is zero.
130134
That is correct, since an empty Nim is losing.
131135

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.
134138

135-
Let $p = \text{mex} \\{ x_1, \ldots, x_k \\} $.
139+
Let $p = \text{mex} \{ x_1, \ldots, x_k \}$.
136140
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$**.
138142
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$.
139143
Therefore, $p$ is indeed the desired Grundy value for the currently considered state.
140144

141145
## Application of the theorem
142146

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.
144148

145149
To calculate the Grundy value of a given state you need to:
146150

@@ -160,18 +164,18 @@ We can xor-sum them just like usual Nim according to Bouton's theorem.
160164

161165
## Patterns in Grundy values
162166

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.
164168

165169
In many games, which may seem rather difficult for theoretical analysis,
166170
the Grundy values turn out to be periodic or of an easily understandable form.
167171
In the overwhelming majority of cases the observed pattern turns out to be true and can be proved by induction if desired.
168172

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").
171174

172175
## Example games
173176

174177
### Crosses-crosses
178+
175179
**The rules.**
176180
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.
177181

@@ -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$
184188
For the edge case of the cross being marked on position $1$ or $n$, we go to the game $n-2$.
185189

186190
Thus, the Grundy value $g(n)$ has the form:
187-
$$ g(n) = \text{mex} \Bigl( \\{ g(n-2) \\} \cup \\{g(i-2) \oplus g(n-i-1) \mid 2 \leq i \leq n-1\\} \Bigr) . $$
191+
192+
$$g(n) = \text{mex} \Bigl( \{ g(n-2) \} \cup \{g(i-2) \oplus g(n-i-1) \mid 2 \leq i \leq n-1\} \Bigr) .$$
188193

189194
So we've got a $O(n^2)$ solution.
190195

191196
In fact, $g(n)$ has a period of length 34 starting with $n=52$.
192-
193-
194-
195-
196-
197-

src/num_methods/roots_newton.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
# Newton's method for finding roots
44

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.
66

77
The task is as follows. Given the following equation:
88

99
$$f(x) = 0$$
1010

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]$.
1212

1313
## Algorithm
1414

1515
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.
1616

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.
1818

1919
It is not difficult to obtain the following formula:
2020

src/num_methods/simpson-integration.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ The solution described here was published in one of the dissertations of **Thoma
1010

1111
## Simpson's formula
1212

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:
1414

1515
$$x_i = a + i h, ~~ i = 0 \ldots 2n,$$
1616
$$h = \frac {b-a} {2n}.$$
1717

1818
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.
1919

20-
2120
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.
2221
For instance we could construct it using the Lagrange polynomial interpolation.
2322
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:
2524

2625
$$\int_{x_ {2i-2}} ^ {x_ {2i}} f (x) ~dx \approx \int_{x_ {2i-2}} ^ {x_ {2i}} P (x) ~dx = \left(f(x_{2i-2}) + 4f(x_{2i-1})+(f(x_{2i})\right)\frac {h} {3} $$
2726

@@ -40,7 +39,7 @@ The error is asymptotically proportional to $(b-a)^5$. However, the above deriva
4039

4140
## Implementation
4241

43-
Here $f(x)$ is some user function.
42+
Here, $f(x)$ is some user-defined function.
4443

4544
```cpp
4645
const int N = 1000 * 1000; // number of steps (already multiplied by 2)

0 commit comments

Comments
 (0)