Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of runtimes of BFS and DFS

Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in the following site

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/depthSearch.htm

like image 831
miss24 Avatar asked Jul 27 '11 19:07

miss24


People also ask

What is difference between BFS and DFS?

BFS, stands for Breadth First Search. DFS, stands for Depth First Search. BFS uses Queue to find the shortest path. DFS uses Stack to find the shortest path.

What is BFS and DFS explain with example?

BFS(Breadth First Search) uses Queue data structure for finding the shortest path. DFS(Depth First Search) uses Stack data structure. 3. Definition. BFS is a traversal approach in which we first walk through all nodes on the same level before moving on to the next level.

What is the big O runtime of BFS?

Breadth-first search has a running time of O ( V + E ) O(V + E) O(V+E) since every vertex and every edge will be checked once.

What is the runtime of DFS?

then the running time of DFS algorithm is O(n ), where n is the number of nodes. m is the number of edges and n is the number of nodes.


1 Answers

E is the set of all edges in the graph, as G={V,E}. So, |E| is count of all edges in the graph.

This alone should be enough to convince you that the overall complexity can't be |V| times |E|, since we are not iterating over all the edges in the graph for each vertex.

In the adjacency list representation, for each vertex v, we only traverse those nodes which are adjacent to it.

The |V| factor of the |V|+|E| seems to come from the number of queue operations done.

Note that the complexity of the algorithm depends on the data structure used. effectively we are visiting each piece of information present in the representation of the graph, which is why for matrix representation of the graph, complexity becomes V squared.

Quoting from Cormen,

"The operations of enqueuing and dequeuing take O(1) time, so the total time devoted to queue operations is O( V). Because the adjacency list of each vertex is scanned only when the vertex is dequeued, each adjacency list is scanned at most once. Since the sum of the lengths of all the adjacency lists is Θ(E), the total time spent in scanning adjacency lists is O( E). The overhead for initialization is O( V), and thus the total running time of BFS is O( V + E)."

like image 126
Abhijeet Kashnia Avatar answered Sep 17 '22 13:09

Abhijeet Kashnia