algorithm - Solving a graph game -
i've struggled time problem programming contest (andrew stankevich contest 21) game goes follows:
nick , peter play following game [...]. draw undirected bipartite graph g on sheet of paper, , put token 1 of vertices. after make moves in turn. nick moves first.
a move consists of moving token along graph edge. after vertex token before move, edges incident it, removed graph. player has no valid moves loses game.
the graph given , task find given start node, whether starting player wins or loses if both players play optimally. summarize
- bipartite graph
- we given start node (say on left side)
- we move in turns, move consists of following edge, can't visit node visited
- the player cannot move loses
since graph bipartite, nick (the first player) remove node left side , peter remove node right side.
the graph can have 1000 nodes (at 500 @ each side) , 50000 edges, polynomial time algorithm needed (the time limit here 2 seconds solve starting positions, think can share lot of information between different starting positions).
i'm pretty sure can reduced kind of vertex covering or packing problem, because graph bipartite, can't find strategy correlates of these.
i know solution special case: let's sides have n1 , n2 vertices, respectively. if there matching of size min(n1, n2) , if player on smaller side begins, there exists winning strategy: has follow matched edges , automatically wins.
any ideas?
proposition. nick (the first player) wins starting vertex v
iff vertex belongs every possible maximum matching of given graph. prove in 2 steps.
if there maximum matching without
v
, nick loses.
indeed, since matching maximum, there no augmenting pathv
. means every simple odd-length pathv
can prolonged edge of matching. in terms of our problem, means game can continued peter after every nick's move.if there no maximum matching without
v
, nick wins.
consider possible maximum matching. move along edge of matchingv
to, say,u
. now, initial matching minus edgeu-v
maximum matching of remaining graph not includeu
. know step 1, player move (which peter) @ loss.
as implementation, can first construct maximum matching in o(ve) utilizing straightforward algorithm (see here example implementation) — turns out common name kuhn's augmenting paths algorithm.
after that, maintain maximum matching , @ every vertex. if vertex, v
, not in matching, nick loses. if is, remove corresponding edge, v-u
, matching, forbid vertex v
temporarily , run search augmenting path u
in o(e). if don't find such path, nick wins, , have restore edge removed. otherwise, nick loses again, , new maximum matching can left untouched. total running time o(ve) again.
Comments
Post a Comment