Proposition. Nick (the first player) wins starting from vertex v
iff this vertex belongs to every possible maximum matching of the given graph. We will prove it in two steps.
If there is a maximum matching without
v
, Nick loses.
Indeed, since the matching is maximum, there is no augmenting path fromv
. That means every simple odd-length path fromv
can be prolonged by an edge of the matching. In terms of our problem, it means the game can be continued by Peter after every Nick's move.If there is no maximum matching without
v
, Nick wins.
Consider any possible maximum matching. Move along the edge of this matching fromv
to, say,u
. Now, the initial matching minus the edgeu-v
is a maximum matching of the remaining graph which does not includeu
. As we know from step 1, the player to move now (which is Peter) is at a loss.
As for the implementation, we can first construct a maximum matching in O(VE) utilizing the straightforward algorithm (see here for an example implementation) — turns out the common name is Kuhn's augmenting paths algorithm.
After that, you maintain a maximum matching and look at every vertex. If the vertex, say v
, is not currently in the matching, Nick loses. If it is, remove the corresponding edge, say v-u
, from the matching, forbid the vertex v
temporarily and run a search for an augmenting path from u
in O(E). If you don't find such path, Nick wins, and you have to restore the edge you removed. Otherwise, Nick loses again, and the new maximum matching can be left untouched. The total running time is O(VE) again.