The most trivial implementation of BFS would simply store the last frontier (open list), build the new frontier, and then replace the last frontier with the new (like in the example python code below). But almost universally, the first algorithm shown for BFS in textbooks is the queue-based implementation.
What's the disadvantage of the simple frontier-storing approach? I can see it might use slightly more memory (effectively storing the new frontier while the old is still in memory), but usually textbooks only care about big-O complexity. Is there any other problem with it?
# example of a graph
g = {1: [2, 3], 2: [1, 3], 3: [1, 2, 4], 4: []}
def bfs(g, root):
yield root
boundary = [root]
visited = {root}
while boundary:
new_boundary = []
for v in boundary:
for w in g[v]:
if w not in visited:
yield w
visited.add(w)
new_boundary.append(w)
boundary = new_boundary