简体   繁体   English

O(M + N)的复杂度

[英]Complexity of O(M+N)

I've computed complexity of below algorithm as 我已经计算出以下算法的复杂度为

for i = 0 to m
    for j = 0 to n
        //Process of O(1)

Complexity: O( m * n) 复杂度: O( m * n)

This is simple example of O( m * n). 这是O(m * n)的简单示例。 But I'm not able to figure out how O(m+n) computed. 但是我不知道O(m + n)是如何计算的。 Any sample example 任何示例

O(m+n) means O(max(m,n)). O(m + n)表示O(max(m,n))。 A code example: 一个代码示例:

for i = 0 to max(m,n)
    //Process

The time complexity of this example is linear to the maximum of m and n . 此示例的时间复杂度与mn的最大值呈线性关系。

You often get O(m+n) complexity for graph algorithms. 对于图算法,通常会得到O(m+n)复杂度。 It is the complexity for example of a simple graph traversal such as BFS or DFS. 这是例如BFS或DFS之类的简单图形遍历的复杂性。 Then n = |V| 然后n = |V| stands for the number of vertices, m = |E| 代表顶点数, m = |E| for the number of edges, where the graph is G=(V,E) . 对于边的数量,其中图形为G=(V,E)

The Knuth-Morris-Pratt string-searching algorithm is an example. Knuth-Morris-Pratt字符串搜索算法就是一个示例。

http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#Efficiency_of_the_KMP_algorithm http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#Efficiency_of_the_KMP_algorithm

The string you're looking for (the needle or the pattern) is length m and the text you're searching through is length n . 您要查找的字符串(针或花样)的长度为m ,而要搜索的文本的长度为n There is preprocessing done on the pattern which is O(m) and then the search, with the preprocessed data, is O(n) , giving O(m + n) . 对模式O(m)进行了预处理,然后使用预处理后的数据进行搜索,结果为O(n) ,得出O(m + n)

for i=0 to m
 //process of O(1)
for i=0 to n
 //process of O(1)

time complexity of this procedure is O(m+n). 该过程的时间复杂度为O(m + n)。

The above example you have is a nested for loop, when you have nested loops and have 2 different inputs m and n ( considered very large in size). 上面的示例是嵌套的for循环,当您具有嵌套循环并具有2个不同的输入m和n(被认为非常大)时。 The complexity is said to be multiplicative. 据说复杂度是乘法的。 so for first for loop you write complexity O(m) and for inner for loop you write O(n) and as they are nested loop, you can write as O (m) * O(n) or O(m * n). 因此,对于第一个for循环,您可以编写复杂度O(m);对于第一个for循环,您可以编写O(n);由于它们是嵌套循环,因此可以写为O(m)* O(n)或O(m * n) 。

static void AddtiviteComplexity(int[] arr1,int[] arr2)
{
    int i = 0;
    int j = 0;

    while (i < arr1.Length)
    {
        Console.WriteLine(arr1[i]);

        while (j < arr2.Length)
        {
            Console.WriteLine(arr2[j]);
            j++;
        }

        i++;
    }           
}

similarly when have 2 loops and they are not nested and have 2 different inputs m and n ( considered very large in size), the complexity is said to be additive. 类似地,当有2个循环并且它们没有嵌套并且有2个不同的输入m和n(被认为是非常大的)时,复杂度被认为是累加的。 For the First loop, you write the complexity O(m) and for the second loop you write the complexity O(n) and as there are separate loops, you can write the complexity as O(m) + O(n) or O(m + n). 对于第一个循环,您编写复杂度O(m);对于第二个循环,您编写复杂度O(n);由于存在单独的循环,您可以将复杂度写为O(m)+ O(n)或O (m + n)。

 static void AddtiviteComplexity(int[] arr1,int[] arr2)
    {
        int i = 0;
        int j = 0;

        while(i< arr1.Length)
        {
            Console.WriteLine(arr1[i]);
            i++;
        }

        while (j < arr2.Length)
        {
            Console.WriteLine(arr2[j]);
            j++;
        }
    }

Note: the above code is for example with int array is example purpose. 注意:以上代码例如以int数组为例。 Also I have used while loop, it doesn't matter if it's a while or a for loop for calculating complexity. 另外我使用了while循环,它是一个while循环还是一个for循环来计算复杂度都没有关系。

Hope this helps. 希望这可以帮助。

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

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