this is bfs algorithm