Combinatorial Game Theory

Combinatorical Game Theory

Introduction

from wikipedia

Combinatorial game theory (CGT) is a branch of mathematics and theoretical computer science that typically studies sequential games with perfect information.

This is a rather niche topic in competitive programming, yet it is(arguably) one of the most interesting and there is more logic behind these combinatorial puzzles than you would have imagined.

Let’s start with a simple and typical Nim’s game:

Nim’s Game

There are several piles of stones. A player can take any positive number of stones from any one pile and throw them away in a move. A player loses if they cannot make a move i.e. when all the piles are empty.

Before we get started, notice that this particular game has the following features:

  • This is an impartial two-player game i.e. available moves and win/lose positions only depend on the current state of the game, and the only difference between the two players is that one of them starts first.

  • There is perfect information i.e. both players are rational individuals who know about the rules and set of available moves for any state of the game, and are trying to win(at least not lose) the game.

  • The game is finite i.e. the game would end after a finite number of moves. In this case, since the total number of stones is finite, we know that the game is going to end with one player losing.

Knowing all these features, we can think of this game as a directed acyclic graph (DAG) where the vertices represent different states of the game while the edges are all possible moves from one state to another. of course, there will be a vertex with no outgoing edges(outdegree=0) which represents the losing stance, since there is no available move.

Consider the scenario where we have three piles of rocks with 2,1,1 rocks respectively. We can draw out the following diagram for all the possible states in the game:

game1
We define any state to be a p − p o s i t i o n p-position pposition if the current player is losing, and a N − p o s i t i o n N-position Nposition if the current player is wining. Clearly, the bottom state(0,0,0) is a P − p o s i t i o n P-position Pposition. It is not hard to notice that which class other states fall into can be deduced recursively by the following two rules:

  • The current state is a N − p o s i t i o n N-position Nposition if it has an outward edge to a P − p o s i t i o n P-position Pposition .
  • The current state is a P − p o s i t i o n P-position Pposition if it does not have an outward edge to a P − p o s i t i o n P-position Pposition.

This is intuitive to understand. For the current player, he only loses if no matter how he moves, he always end up letting the other player win. As long as there exists one move that let him go to a losing state( P − p o s i t i o n P-position Pposition ), he would choose that particular move and proceed to win the game.

Now since this is a DAG, we can define every state to be either a N − p o s i t i o n N-position Nposition or a P − p o s i t i o n P-position Pposition according to the above two rules. And we know that the first player is going to win the game if any of the starting state is a N − p o s i t i o n N-position Nposition.

Sprague-Grundy Theorem

To conclude a winning strategy of the game, we first represent the game state by a multiset of positive integers as described earlier. A move strictly decreases a chosen integer, and the integer is removed from the set if it becomes zero.

We claim the following strategy exists:

The current player has a winning strategy if and only if the xor-sum of the pile sizes is non-zero. The xor-sum of a sequence a a a is a 1 ⊕ a 2 ⊕ … ⊕ a n a_1⊕a_2⊕…⊕a_n a1a2an, where ⊕ is the bitwise exclusive or.

We can prove this is true by mathematical induction.

When all the piles are empty (i.e. the multiset is empty) the xor-sum is zero and the theorem is true since he automatically loses the game.

Now suppose we are in a non-empty state. We firstly ssume that the the statement is true for all states reachable from the current one. This is a valid induction assumption given that all the game states from an acyclic graph.

Let the current xor sum be s s s, and the reachable state xor sum be t t t.

Case 1: s = 0 s=0 s=0. Any current move reduces the size of some pile from x x x to y y y.
Notice that t = s ⊕ y ⊕ x = 0 ⊕ y ⊕ x = y ⊕ x t=s⊕y⊕x=0⊕y⊕x=y⊕x t=syx=0yx=yx. Since y < x y<x y<x, t = y ⊕ x ≠ 0 t=y⊕x\neq0 t=yx=0. Thus, any reachable state is a winning state, and the current state is hence a losing state.

Case 2: s ≠ 0 s≠0 s=0. In this case, we let d d d be Consider the leading "1"s in the binary representation of the number s s s, let the number be m m m. We know that there is at least one pile that contributes to all these "1"s to the final xor sum, since xor sum of all other piles is going to give 0 on these digits. Suppose the current player its size from x x x to y = x ⊕ s y=x⊕s y=xs. This is a valid move since the first m m m bits is now 0, while other bits stay unchanged, giving us y < x y<x y<x. Since t = s ⊕ x ⊕ y = s ⊕ x ⊕ ( s ⊕ x ) = 0 t=s⊕x⊕y=s⊕x⊕(s⊕x)=0 t=sxy=sx(sx)=0, this is a reachable losing stateand the current state is a winning state.

Thus, if a state satisfies our theorem, all the states that can reach this state (where all the reverse edges from the state point towards) also satisfy the theorem. By induction, we can see that our theorem is true for all the game states.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值