Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there two listed time complexities for breadth-first search?

The Wikipedia article on breadth-first search lists two time complexities for breadth-first search over a graph: O(|E|) and O(bd). Later on the page, though, it only lists O(|E|).

Why are there two different runtimes? Which one is correct?

like image 629
templatetypedef Avatar asked Nov 04 '13 00:11

templatetypedef


People also ask

What is the time complexity of a breadth-first search?

The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges.

Why is time complexity of BFS O v E?

Defining it this way is useful because you don't necessarily have the same number of edges on every single vertex. Since each edge gets visited once by the time the DFS ends, you get O(E) complexity from that part. Then you add the O(V) for visiting each vertex once and get O(V + E) on total.

What space complexities are involved with breadth-first search and depth first search?

The space complexity for BFS is O(w) where w is the maximum width of the tree. For DFS, which goes along a single 'branch' all the way down and uses a stack implementation, the height of the tree matters. The space complexity for DFS is O(h) where h is the maximum height of the tree.


1 Answers

Both time complexities are correct, but are used in different circumstances to measure the time complexity relative to two different quantities. This has to do with the fact that the breadth-first search algorithm typically refers to two different related algorithms used in different contexts.

In one context, BFS is an algorithm that, given a graph and a start node in the graph, visits every node reachable from the start node by first visiting all nodes at distance 0, then distance 1, then distance 2, etc. until all nodes are visited. This will visit every node in the graph and in the process of doing so explore each node one and edge edge at most once (in the directed case) or twice (in the undirected case). By using queues to keep track of which nodes to explore next and using appropriate bookkeeping, the runtime will be O(|E| + |V|) (and with further optimizations, it will be O(|E|)).

In a different context, BFS is a search algorithm used to find the shortest path from some start node in a graph to a destination node in the graph. In this case, the algorithm stops running as soon as it discovers the destination node. This means that the runtime depends on how far away the destination node is from the source node. That distance in turn depends on the structure of the graph. If the graph is densely connected, the node can't be that far away, and if the graph is sparse, the node might be extremely distant. In this context, it's common to add in a parameter called the "branching factor" b, which describes the average or maximum number of edges adjacent to any node. This means that

  • There is one node at distance 0 from the start node.
  • There are at most b nodes at distance 1 from the start node.
  • There are at most b2 nodes at distance 2 from the start node.
  • ...
  • There are at most bk nodes at distance k from the start node.

If we assume that the destination node is at distance d from the start node, then BFS will visit at most b0 + b1 + ... + bd = O(bd) nodes during its search, spending O(b) time on each of them. Accordingly, the total runtime will be O(bd).

In summary:

  • The runtime of O(|E|) is typically used when discussing the algorithm when being used to explore the entire graph.
  • The runtime of O(bd) is typically used when discussing the algorithm when being used to find a specific node in the graph.

Hope this helps!

like image 140
templatetypedef Avatar answered Oct 21 '22 08:10

templatetypedef