finish BFS