简体   繁体   English

当元素数量不同时,嵌套以提高循环复杂度

[英]Nested for loop complexity when number of elements differ

Say I had a set containing n entries, and a set containing m entries, and the following nested for loop structure: 假设我有一个包含n个条目的集合,一个包含m个条目的集合,以及以下嵌套的for循环结构:

for 1 to n

  for 1 to n

    for 1 to m

What would the time complexity be? 时间的复杂度是多少? I want to say O(mn^2) but can this be simplified anymore so we just have a single variable? 我想说O(mn ^ 2),但是可以再简化一下吗,所以我们只有一个变量?

Thanks. 谢谢。

Given your case,the answer seems obvious that the complexity would be O(mn^2)... 给定您的情况,答案似乎显而易见,复杂度为O(mn ^ 2)...

But,as you mentioned that n and m represent number of vertices and edges respectively,then also, there doesn't exist any direct relationship between no. of edges 但是,正如您提到的,n和m分别代表顶点和边的数量,因此,在no. of edges和n之间不存在任何直接关系no. of edges no. of edges and no. of vertices no. of edgesno. of vertices no. of vertices for a general graph. 一般图形no. of vertices

So,then also,the complexity would be O(mn^2)... 因此,复杂度也将是O(mn ^ 2)...

如果n是图形中的顶点数,并且m是该图形中的边数,则m为O( n ^ 2),因此您的算法将为O( n ^ 4)。

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

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