简体   繁体   English

无向图复杂度上的 DFS?

[英]DFS on undirected graph complexity?

As when we traverse the undirected connected graph using DFS and mark out only edges which we travel during DFS, we end up with a DFS tree which is basically a tree and traversing a tree requires O(v) complexity where v is the number of vertices, then why it stated that complexity is O(v + e)?当我们使用 DFS 遍历无向连通图并仅标记我们在 DFS 期间行进的边时,我们最终得到一棵DFS 树,它基本上是一棵树,遍历一棵树需要 O(v) 复杂度,其中 v 是顶点数,那么为什么它说复杂度是O(v + e)?

I know it's a noob question but I am confused.我知道这是一个菜鸟问题,但我很困惑。

The DFS tree is the tree made of the edges you traversed . DFS 树是由您遍历的边组成的树。 You indeed only traverse O(V) edges.您确实只遍历O(V)边。 But in order to traverse an edge, you first examine the edge to check if it will lead to a vertex you already encountered.但是为了遍历一条边,你首先检查这条边以检查它是否会通向你已经遇到的顶点。 This means you examine more edges than you traverse .这意味着您检查的边比遍历的多。 Indeed, you examine O(E) edges.实际上,您检查了O(E)边。 So the total work is O(V+E) .所以总工作量是O(V+E)

Note: Because your graph is connected, you are sure that E > V .注意:因为您的图表是连通的,所以您确定E > V In that case the complexity can be rewritten O(E) .在这种情况下,复杂性可以重写O(E)

you find all the nodes of graph through edges so the time complexity depends upon no.您通过边找到图的所有节点,因此时间复杂度取决于否。 of edges as well that's why the O(e) is also included.边缘也是如此,这就是 O(e) 也包括在内的原因。 If you consider complete graph TC will be O(V^2).如果您考虑完整的图 TC 将是 O(V^2)。

Consider two different graphs:考虑两个不同的图表:

  1. A graph having more edges than vertices, for instance a connected graph with a high minimum degree .具有比顶点更多的边的图,例如具有高最小度连通图

    When the DFS algorithm visits a vertex, it will have to follow each of the edges that connect this vertex, repeating a DFS traversal from the neighboring vertices.当 DFS 算法访问一个顶点时,它必须沿着连接该顶点的每条边,从相邻顶点重复一次 DFS 遍历。 Of course, if that neighbor was already visited, the DFS algorithm will backtrack, but at least we can state that the edge had to be processed.当然,如果那个邻居已经被访问过,DFS 算法会回溯,但至少我们可以 state 认为必须处理边。

    This procedure will process all edges of the graph.此过程将处理图的所有边。 So in this case we can say the algorithm is O(e)所以在这种情况下,我们可以说算法是O(e)

  2. A graph having fewer edges than vertices, often a disconnected graph (in the extreme case there are no edges).边比顶点少的图,通常是不连贯的图(在极端情况下没有边)。

    When the DFS algorithm has visited all vertices that it can reach from a traversal that started in vertex A, it must still iterate over the remaining vertices, to find vertices that might not have been visited.当 DFS 算法已经访问了它可以从顶点 A 开始的遍历中到达的所有顶点时,它仍然必须遍历剩余的顶点,以找到可能没有被访问过的顶点。 These unvisted vertices do not belong to the same connected component ).这些未访问的顶点不属于同一个连通分量)。 Another DFS traversal must start from there.另一个 DFS 遍历必须从那里开始。

    This way all vertices are processed.这样所有的顶点都被处理了。 So in this case the algorithm has a O(v) time complexity所以在这种情况下,算法的时间复杂度是O(v)

So in general, the algorithm has a O(max(e, v)) time complexity.所以一般来说,该算法的时间复杂度为O(max(e, v)) You could also say that the algorithm must visit all edges and all vertices, and so the algorithm has a O(e+v) time complexity.您也可以说该算法必须访问所有边所有顶点,因此该算法具有O(e+v)时间复杂度。 Both are equivalent.两者是等价的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM