BFS algorithm implemented