The basic algorithm for BFS:
set start vertex to visited
load it into queue
while queue not empty
for each edge incident to vertex
if its not visited
load into queue
mark vertex
So I would think the time complexity would be:
v1 + (incident edges) + v2 + (incident edges) + .... + vn + (incident edges)
where v
is vertex 1
to n
Firstly, is what I've said correct? Secondly, how is this O(N + E)
, and intuition as to why would be really nice. Thanks
O(V+E)
when using the adjacency list. Since both DFS and BFS can use this and they both maintain onevisited
structure to ensure the spanning tree with no circuits, they both have time complexityO(V+E)
. – Anchor