Explanation of runtimes of BFS and DFS

后端 未结 5 450
你的背包
你的背包 2021-01-30 08:42

Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in

5条回答
  •  长发绾君心
    2021-01-30 09:35

    You visit every edge at most twice. There are E edges. So there will be 2*E edge visit operations. Plus the nodes those have no edges or in other words, with degree 0. There can be at most V such nodes. So the complexity turns out to be, O(2*E + V) = O(E + V)

提交回复
热议问题