Why is the complexity of BFS O(V+E) instead of O(V*E)?
Some pseudocode here (disregard my style)
Starting from v1(enqueued):
function BFS(queue Q)
v2 = dequeue Q
enqueue all unvisited connected nodes of v2 into Q
BFS(Q)
end // maybe minor problems here
Since there are V vertices in the graph, and these V vertices are
connected to E edges, and visiting getting connected nodes (equivalent to
visiting connected edges) is in the inner loop (the outer loop is the
recursion itself), it seems to me that the complexity should be O(V*E)
rather than O(V+E). Can anyone explain this for me?
No comments:
Post a Comment