简体   繁体   English

为什么DFS和BFS的时间复杂度取决于图表的表示方式?

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

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 adjacency matrix is used, the complexity is O(V 2 ). 网站http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html描述了当使用邻接列表时,DFS和BFS具有复杂度O(V + E),如果使用邻接矩阵,复杂度为O(V 2 )。 Why is this? 为什么是这样?

In both cases, the runtime depends on how long it takes to iterate across the outgoing edges of a given node. 在这两种情况下,运行时都取决于迭代给定节点的传出边所需的时间。 With an adjacency list, the runtime is directly proportional to the number of outgoing edges. 使用邻接列表,运行时与传出边的数量成正比。 Since each node is visited once, the cost is the number of nodes plus the number of edges, which is O(m + n). 由于每个节点被访问一次,因此成本是节点数加上边数,即O(m + n)。 With am adjacency matrix, the time required to find all outgoing edges is O(n) because all n columns in the row for a node must be inspected. 使用am邻接矩阵,找到所有输出边所需的时间是O(n),因为必须检查节点的行中的所有n列。 Summing up across all n nodes, this works out to O(n 2 ). 总结所有n个节点,这可以得到O(n 2 )。

Hope this helps! 希望这可以帮助!

你必须注意,为了探索探索所需的每个顶点时间,它只等于c * x,其中x是顶点的indegree。由于我们有兴趣找到整体复杂性,总时间将是c1 * x1 + c2 * 对于n个节点, x2 ... cn xn。看到max(ci)= d,我们看到总时间<= d (所有顶点的不一致之和)= d * 2m = O(m)。这里我们已经计算过不是一个顶点的时间,而是所有顶点合在一起的时间。但是排队操作需要时间O(n),所以总体上是O(n + m)。

The time complexity for both DFS and BFS can be computed as follows: DFS和BFS的时间复杂度可以计算如下:

Iterating every vertex once and its corresponding incident edges , so the total time complexity will be -> 迭代每个顶点一次及其相应的入射边缘 ,因此总时间复杂度将为 - >

Time Complexity = v1 + (incident_edges on v1) + v2 + (incident_edges on v2) + ...... + vn + ( incident_edges on vn) 时间复杂度= v1 +(v1上的incident_edges)+ v2 +(v2上的incident_edges)+ ...... + vn +(vn上的incident_edges)

Now this can be regrouped as -> (v1+v2+v3+.....vn) + (incident_edges on v1 + incident_edges on v2 + ..... incident_edges on vn) 现在这可以重新组合为 - >(v1 + v2 + v3 + ..... vn)+(v1上的incident_edges + v2上的incident_edges + vn上的incident_edges)

Thus total time complexity would turn out to be = (v1+v2+v3+.....vn) + (incident_edges on v1 + incident_edges on v2 + ..... incident_edges on vn) 因此,总时间复杂度将变为=(v1 + v2 + v3 + ..... vn)+(v1上的incident_edges + v2上的incident_edges + vn上的incident_edges)

(v1 + v2 + ... + vn) = V or n (Total number of vertices) (v1 + v2 + ... + vn)= V或n(顶点总数)

For adjacency list representation : 对于邻接列表表示

(incident_edges on v1 + incident_edges on v2 + ..... incident_edges on vn) = E(Total number of edges) (v1上的incident_edges + v2上的incident_edges + vn上的incident_edges)= E(边缘总数)

Thus for adjacency list representation time complexity will be O(V+E) 因此,对于邻接列表表示时间复杂度将为O(V + E)

For adjacency matrix representation : 对于邻接矩阵表示

To visit the neighbors of the corresponding node(Row) we need to iterate all the columns for the particular row which amounts to V 要访问相应节点(Row)的邻居,我们需要迭代特定行的所有列,其总计为V.

So, (incident_edges on v1 + incident_edges on v2 + ..... incident_edges on vn) = V + V + .... Vth time V) = V*V 所以,(v1上的incident_edges + v2上的incident_edges + vn上的incident_edges)= V + V + .... Vth时间V)= V * V

Thus time complexity will be O(V + V^2) = O(V^2) 因此时间复杂度将为O(V + V ^ 2)= O(V ^ 2)

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

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