Posts

Showing posts with the label Breadth First Search

Breadth First Search Time Complexity Analysis

Image
Answer : I hope this is helpful to anybody having trouble understanding computational time complexity for Breadth First Search a.k.a BFS. Queue graphTraversal.add(firstVertex); // This while loop will run V times, where V is total number of vertices in graph. while(graphTraversal.isEmpty == false) currentVertex = graphTraversal.getVertex(); // This while loop will run Eaj times, where Eaj is number of adjacent edges to current vertex. while(currentVertex.hasAdjacentVertices) graphTraversal.add(adjacentVertex); graphTraversal.remove(currentVertex); Time complexity is as follows: V * (O(1) + O(Eaj) + O(1)) V + V * Eaj + V 2V + E(total number of edges in graph) V + E I have tried to simplify the code and complexity computation but still if you have any questions let me know. Considering the following Graph we see how the time complexity is O(|V|+|E|) but not O(V*E). Adjacency List V E v0:{v1,v2} v1:{v3} v2:{v3} v3:{} Ope...