BFS and DFS Complexity
With an adjacency list, both BFS and DFS visit every reachable vertex and inspect every relevant edge at most a constant number of times, resulting in O(V+E) time. With an adjacency matrix, scanning every possible neighbor gives O(V²) time.
Adjacency list: O(V+E) time.
Adjacency matrix: O(V²) time.
Visited array/set requires O(V) space.
BFS queue and DFS stack can require O(V) additional space.