the implementation of BFS