Created BFS algorithm