fix bug when dealing with undirected graph in backward bfs.