简体   繁体   English

什么是O(n * log m)+ O(m)?

[英]What is O(n*log m) + O(m)?

I am confused about addition with the big O notation. 我对大O表示法的加法感到困惑。

I'm to create an algorithm to find a MST for a graph with some other requirements for a school problem. 我要创建一种算法,以找到具有学校问题的其他一些要求的图形的MST。 It's time complexity is to be in O(E * log V) , where E is the number of edges and V the number of vertices in the graph. 它的时间复杂度为O(E * log V) ,其中E是图中的边数,而V是图中的顶点数。 I have arrived at a solution that is in O(E * log V) + O(V) . 我已经找到了O(E * log V)+ O(V)的解决方案。

Does it hold that O(E * log V) + O(V) = O(E * log V) ? 是否满足O(E * log V)+ O(V)= O(E * log V)吗?

Thank you for all the answers! 感谢您的所有答案! I am assuming this complexity on connected graphs, on graphs that are not connected, my algorithm works in O(E * log V) . 我在连接图上,在未连接图上假设这种复杂性,我的算法在O(E * log V)中工作

For any x, you can make a graph with x edges and 2ˣ (mostly disconnected) vertices. 对于任何x,您都可以制作一个具有x边和2个(大部分是断开的)顶点的图形。

For such a graph, E log V = x², so (V + E log V)/(E log V) = (2ˣ+x²)/x². 对于这样的图,E log V =x²,所以(V + E log V)/(E log V)=(2ˣ+x²)/x²。

This grows without bound as x increases, so O(E log V) + O(V) is NOT the same as O(E log V), even for graphs. 这随着x的增加而无限制地增长,因此,即使对于图形,O(E log V)+ O(V)与O(E log V)也不同。

HOWEVER, if you specify connected graphs, then you have V < E. In that case, as long as V>=2, you have V + E log V < E + E log V <= 2(E log V) 但是,如果您指定连接图,则您有V <E。在这种情况下,只要V> = 2,您就有V + E log V <E + E log V <= 2(E log V)

So O(E log V) = O(E log V) + O(V) for connected graphs. 因此,对于连通图,O(E log V)= O(E log V)+ O(V)。

O(ElogV+V) is not the same as O(ElogV). O(ElogV + V)与O(ElogV)不同。 In general V can be arbitrarily larger than ElogV, which makes the two complexity classes different. 通常,V可以比ElogV任意大,这使两个复杂度类不同。

But, assuming you have an O(ElogV + V) time algorithm for finding an MST if one exists, you can turn it into a guaranteed O(ElogV) time algorithm, assuming the graph is represented in adjacency list form. 但是,假设您有一个O(ElogV + V)时间算法来查找某个MST(如果存在),则可以将其转换为有保证的O(ElogV)时间算法,并假设该图以邻接表的形式表示。

We can determine, in O(E) time, if E>=V/2. 我们可以在O(E)时间内确定E> = V / 2。 Go through the vertices of the graph, and see if there's any edges adjacent to that vertex. 遍历图的顶点,看看该顶点附近是否有边。 If you find a vertex with no adjacent edges, the graph clearly has no MST since that vertex is not connected to the rest of the graph. 如果找到没有相邻边的顶点,则该图显然没有MST,因为该顶点未连接到图的其余部分。 If you have gone through all vertices, you know that E>=V/2. 如果遍历了所有顶点,则知道E> = V / 2。 If you find a vertex with no adjacent edges after n steps, you know you have at least (n-1)/2 edges in the graph, so this procedure takes O(E) time (even though naively it looks like it's O(V) time). 如果在n个步骤后发现没有相邻边的顶点,则知道图中至少有(n-1)/ 2个边,因此此过程需要O(E)时间(即使天真的看起来是O( V)时间)。

If E is less than V/2, the graph is disconnected (since in a connected graph, E>=V-1), and there's no MST. 如果E小于V / 2,则图形断开连接(因为在连接的图形中,E> = V-1),并且没有MST。

So: check if E>=V/2 and only if so, run your MST algorithm. 因此:检查E> = V / 2,仅在这样的情况下,运行MST算法。

This takes O(E + ElogV + V) = O(E + ElogV + 2E) = O(ElogV) time. 这需要O(E + ElogV + V)= O(E + ElogV + 2E)= O(ElogV)时间。

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

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