简体   繁体   English

关于无向图复杂性的DFS

[英]DFS on undirected graph complexity

Let's say I have an undirected graph with V nodes and E edges.If I represent the graph with adjacency lists,if I have a representation of an edge between x and y,I must also have a representation of the edge between y and x in the adjacency list. 假设我有一个带有V节点和E边的无向图。如果我用相邻列表表示图,如果我有一个x和y之间的边的表示,我还必须表示y和x之间的边。邻接清单。

I know that DFS for directed graphs has V+E complexity.For undirected graphs doesn't it have v+2*e complexity ,because you visit each edge 2 times ?Sorry if it's a noobish question..I really want to understand this think.Thank you, 我知道有向图的DFS具有V + E复杂性。对于无向图,它没有v + 2 * e复杂度,因为你访问每个边2次?抱歉,如果这是一个noobish问题..我真的想了解这个想。谢谢,

The complexity is normally expressed O(|V| + |E|) and this isn't affected by the factor of 2. 复杂度通常表示为O(| V | + | E |),这不受因子2的影响。

But the factor of 2 is actually there. 但实际上是2的因素。 One undirected edge behaves just line 2 directed edges. 一个无向边缘仅表现为线2的有向边。 Eg the algorithm (for a connected undirected graph) is 例如,算法(用于连接的无向图)是

visit(v) {
  mark(v)
  for each unmarked w adjacent to v, visit(w)
}

The for loop will consider each edge incident to each vertex once. for循环将考虑每个顶点入射到每个顶点一次。 Since each undirected edge is incident to 2 vertices, it will clearly be considered twice! 由于每个无向边缘都入射到2个顶点,因此显然会被认为是两次!

Note the undirected DFS doesn't have to worry about restarting from all the sources. 请注意,无向DFS不必担心从所有源重新启动。 You can pick any vertex. 你可以选择任何顶点。

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

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