Why does the time complexity of DFS and BFS depend on the way the graph is represented?

后端 未结 3 1492
予麋鹿
予麋鹿 2021-01-30 14:46

The site http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html describes that when an adjacency list is used then, DFS and BFS have complexity O(V+E), and if an ad

3条回答
  •  忘了有多久
    2021-01-30 15:17

    You must note that for exploring each vertex time required for exploring it is only equal to c*x where x is the indegree of the vertex.Since we are interested in finding the overall complexity, the overall time would be c1*x1+c2*x2...cnxn for n nodes.Taking max(ci)=d,we see that the overall time is <=d(sum of indegrees of all vertices)=d*2m=O(m).Here we have computed the time for not one vertex, but all the vertices taken together.But the enqueuing operation takes time O(n), so overalll O(n+m).

提交回复
热议问题