Time complexity of BFS and DFS: O(V + E)
Time complexity of BFS and DFS: O(V + E)
Breadth-First Search (BFS) and Depth-First Search (DFS) are fundamental graph traversal algorithms used to explore nodes and edges in a graph.
BFS explores the graph level by level, starting from a given node. It uses a queue to keep track of the nodes to be explored next. As it visits each node, it adds its unvisited neighbors to the queue. The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is because BFS visits each vertex once and each edge once.
DFS explores the graph by going as deep as possible along each branch before backtracking. It uses a stack (either explicitly or through recursion) to keep track of the nodes to be explored next. Similar to BFS, DFS visits each vertex once and each edge once. Therefore, the time complexity of DFS is also O(V + E).
Example
Consider a graph with 5 vertices and 7 edges. Using BFS or DFS, the algorithms will visit each of the 5 vertices once and each of the 7 edges once, resulting in a total of 12 operations.
Understanding the time complexity of BFS and DFS is crucial for analyzing the efficiency of algorithms used in graph-related problems, such as finding connected components, paths, and cycles in a graph.
Computational complexity of matrix multiplication
O(n³) naive matrix multiplication
Dijkstra's algorithm
Dijkstra's algorithm time complexity: O((V+E) log V)
non-convex loss landscapes are hard: many local minima and saddle points
Non-convex loss landscapes are hard due to many local minima and saddle points
BFS vs DFS: BFS finds shortest path in unweighted graphs, DFS uses less memory
BFS finds shortest path in unweighted graphs; DFS uses less memory
Binary search
Time complexity of binary search: O(log n) — halves search space each step
merge sort: O(n log n) always
Merge sort consistently performs at O(n log n) time complexity for any input size
One email a day: 5 concepts + the 5 stories that matter →
Swipe through 100 ML concepts daily
Open TickerNews