The bottom up computation in the Bead Necklace game corresponds to the following top down analysis, which applies for any game that has a finite number of positions.
There are two players 0 and 1. The game is described as a game graph. The vertices are the positions and each position of the game is labelled 0 or 1 depending on which player moves at that position. The edges describe how the game can evolve from one position to another.
We label some of the positions as winning for player 0 — let this set be G. If the game ever reached G, player 0 wins. Otherwise, player 1 wins. (If the game graph is a dag, all plays will end in a sink state, so we just label the sink states as winning for 0/1, as in the Pearls game.)
Let W0 denote the set of positions from which player 0 can win in at most n moves. W0 is G, since, if the game is in a position in G, player 0 has already won.
We now calculate W1.
In general, let Wi be the set of positions from where player 0 is guaranteed to win in i moves. We can calculate W{i+1}.
Notice that W0 is included in W1 is included in W2 ... Since the game graph has only n positions, for some i, we must have Wi = W{i+1}. In particular, we must stop when we reach W{n-1} because we cannot add more than n-1 new nodes to the original set W0.
Let W the final winning set for 0 — that is, W = Wi such that Wi = W{i+1}.
Claim:
Proof:
How do we compute these quantities?
Naively, this can be done in O(nm) time — we have to compute sets W1,...,X{n-1} and we scan all edges in each iteration.
However, with some care, we can do it in O(m) time.
Hint: Keep a count at each neighbour of how many good neighbours it has. Initialize this count to 0. When you add a node to W, notify all your incoming neighbours that you are good (increment their good neighbour count).
This can be done in linear time, like BFS.
©IARCS 2012–2016
Pěstujeme web | visit: Skluzavky