2

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
Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
max
  • 381
  • 1
  • 10

1 Answers1

3

Your analysis is correct. There is no major disadvantage of the two-list approach you outline.

One nice thing about the queue-based formulation of BFS is that it makes it easier to see the relationship to DFS and Dijkstra's algorithm. Start with the queue-based formulation, and replace the queue with a stack, and you get DFS. Replace the queue with a priority queue, and you get Dijkstra's algorithm. This similarity is aesthetically pleasing and might help understand the algorithm better.

A slight disadvantage of your two-list based scheme is that it has somewhat higher memory consumption: only a constant factor higher, so maybe not a big deal, but it is somewhat higher. This is because when you're traversing the current frontier, you keep around the items in the list that you've already traversed even after you've finished traversing them. In contrast, the queue-based method doesn't do that.

Nonetheless, in the big picture, you are absolutely right; both methods are pretty similar and there's not an overwhelming reason to prefer one over the other.

D.W.
  • 167,959
  • 22
  • 232
  • 500