# Find best path in an alternating graph

I am trying to solve the following problem: Given a directed graph, there is a robot in the start and it needs to reach the destination. Each movement takes 1 second, and every second the graph reverses each edges. The robot can stay at a node and wait for 1 second in order for the graph to reverse again. The goal is to reach the destination in the shortest time possible.

Obviously since each movement takes 1 second, we can assume that each movement doesn't cost anything, but the fact that in every second the graph reverses its edges still remains. Also, since the graph is not weighted, I think Dijkstra's algorithm is not very suitable.

My "algorithm" uses BFS in the following way: first the graph is partitioned in levels. The nodes of each level need only 1 step (second) to be reached from the previous one, but every other level has the edges reversed. So the robot starts from the root that has the initial edges, then the nodes that are directly accessible from the root have reversed edges towards the nodes that are closer to the destination etc etc. The robots calculates at every level, if there is a path that starts from the current node to the destination node. If there is, it moves to the next node in that path, since this step is the best one. If not, then it waits for 1 second for the edges to be reversed (final_time++) and then moves to that node.

I am pretty sure that my algorithm is not great and I am not sure if it would work. Also, I am thinking that I would need some type of backtracking in case it reaches a node from which the destination is unreachable.

Any comments or tips on how to construct a better solution? I am not asking for the solution, but for corrections and possibly some hints that will make me find/fix the algorithm. Sorry if some points are not very well explained, English isn't my mother tongue. I am happy to clarify if something is not clear enough.

Here's a hint: explicitly create the "mirror graph", and then you redirect all the edges so that the destination is in the other graph. Also, add the edges $$vv'$$ (and vice versa) where $$v'$$ is the mirror vertex of $$v$$.
Now you only need to do a BFS from $$s$$ to $$\{t, t'\}$$.
• @Greggs I'm not sure if you got the interpretation completely correct. As the OP says, you need to "reroute" each edge. That is, you have two copies of each node, say $v_{even}$ and $v_{odd}$, where the "even" nodes signify an even timestep and vice versa. So, for each directed edge $(u,v)$ in the original graph, you have two edges $(u_{even},v_{odd})$ and $(v_{odd},u_{even})$. In addition to the edges $(v_{even},v_{odd})$ and $(v_{even},v_{odd})$ for each $v$. This way you ensure that you must follow the correct direction of the edges at each timestep. Commented May 2 at 19:09