简体   繁体   English

在图中的每个节点上运行 DFS 的时间复杂度是多少?

[英]What would the time complexity be of running DFS on each node in a graph?

I have to write a function that performs DFS on each node in a directed graph.我必须编写一个函数,在有向图中的每个节点上执行 DFS。 I believe the run time would be O(n^2+mn) but I am not positive about this.我相信运行时间将是O(n^2+mn)但我对此并不乐观。 Would this be considered polynomial time?这会被认为是多项式时间吗?

Yes, it would be considered polynomial:是的,它会被认为是多项式:

Everything thing that has a time complexity of O( n^k ), n \\in \\N is polynomial.时间复杂度为 O( n^k ), n \\in \\N 的所有事物都是多项式的。

You have O( n^2 + mn ), m can be written as m = n u, u \\in \\R;你有 O( n^2 + mn ),m 可以写成 m = n u, u \\in \\R; Now you can insert if: n^2 + u n n = n^2 + u n^2 = ( 1 + u ) * n^2.现在你可以插入如果:n^2 + u n n = n^2 + u n^2 = ( 1 + u ) * n^2。 ( 1 + u ) is a constant value, so it is negligible. ( 1 + u ) 是一个常数值,因此可以忽略不计。

Therefore, you have the form of a polynomial time: O( n^2 ) (k=2).因此,您具有多项式时间的形式:O( n^2 ) (k=2)。

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

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