graph - Why is time complexity for BFS/DFS not simply O(E) instead of O(E+V)? -
graph - Why is time complexity for BFS/DFS not simply O(E) instead of O(E+V)? -
i know there's similar question in stack overflow, 1 person has asked, why time complexity of bfs/dfs not o(v).
the appropriate reply given e can big v^2 in case of finish graph, , hence valid include e in time complexity.
but, if v cannot greater e+1. so, in case not having v in time complexity, should work?
if given e = kv + c
, real constants k
, c
then, o(e + v) = o(kv + c + v) = o(v) = o(e)
, argument correct.
an illustration of trees.
in general, however, e = o(v^2)
, , cannot improve o(v^2)
.
why not write o(e) always? note there might cases when there no edges in graph @ (i.e. o(e) ~ o(1)
). such cases, we'll have go each of vertex (o(v)
), cannot finish in o(1)
time.
thus, writing o(e)
won't in general.
graph time-complexity depth-first-search breadth-first-search
Comments
Post a Comment