algorithm - Why is the complexity of BFS O(V+E) instead of O(V*E)? -
some pseudocode here (disregard style)
starting v1(enqueued):
function bfs(queue q) v2 = dequeue q enqueue unvisited connected nodes of v2 q bfs(q) end // maybe minor problems here
since there v vertices in graph, , these v vertices connected e edges, , visiting getting connected nodes (equivalent visiting connected edges) in inner loop (the outer loop recursion itself), seems me complexity should o(v*e) rather o(v+e). can explain me?
e not number of edges adjacent each vertex - total number of edges in graph. defining way useful because don't have same number of edges on every single vertex.
since each edge gets visited once time dfs ends, o(e) complexity part. add o(v) visiting each vertex once , o(v + e) on total.
Comments
Post a Comment