我的程序在大O时间运行的时间

时间:2013-10-07 02:24:53

标签: math for-loop big-o time-complexity

有人可以帮我弄清楚这个循环的运行时间吗?我相信它是O(5nlogn)。

for(int f = 0; f < Array.length; f++) {
    F = Array[f];
    for(int e = 0; e <= f; e++) {
        E = Array[e];
        for(int d = 0; d <= e; d++) {
            D = Array[d];
            for(int c = 0; c <= d; c++) {
                C = Array[c];
                for(int b = 0; b <= c; b++) {
                    B = Array[b];
                    for(int a = 0; a <= b; a++) {
                        A = Array[a];
                    }
                }
            }
        }
    }
}

由于

2 个答案:

答案 0 :(得分:2)

答案是Θ(n 6 。我编写了一个程序来模拟内部循环并记录一系列n执行发生的次数:

static void Main(string[] args)
{
    int arrLength = 20;
    int[] arr = new int[arrLength];

    for (int f = 0; f < arrLength; f++)
    {
        for (int e = 0; e <= f; e++)
        {
            for (int d = 0; d <= e; d++)
            {
                for (int c = 0; c <= d; c++)
                {
                    for (int b = 0; b <= c; b++)
                    {
                        //for (int a = 0; a <= b; a++)
                        arr[b] = arr[b] + 1;
                    }
                }
            }
        }
    }

    for (int i = 0; i < arr.Length; i++)
    {
        Debug.WriteLine(string.Format("{0} execution: {1} time(s).", i + 1, arr[i]));
        Console.WriteLine(string.Format("{0} execution: {1} time(s).", i + 1, arr[i]));
    }

    Console.ReadLine();
}

以arrLength为1运行它会产生:

1 execution: 1 time(s).

以arrLength为2运行它会产生:

1 execution: 5 time(s).
2 execution: 1 time(s).

以arrLength为3运行它会产生:

1 execution: 15 time(s).
2 execution: 5 time(s).
3 execution: 1 time(s).

事实证明,执行时间始终遵循相同的等式。在20岁的arrLength,我们得到:

1 execution: 8855 time(s).
2 execution: 7315 time(s).
3 execution: 5985 time(s).
4 execution: 4845 time(s).
5 execution: 3876 time(s).
6 execution: 3060 time(s).
7 execution: 2380 time(s).
8 execution: 1820 time(s).
9 execution: 1365 time(s).
10 execution: 1001 time(s).
11 execution: 715 time(s).
12 execution: 495 time(s).
13 execution: 330 time(s).
14 execution: 210 time(s).
15 execution: 126 time(s).
16 execution: 70 time(s).
17 execution: 35 time(s).
18 execution: 15 time(s).
19 execution: 5 time(s).
20 execution: 1 time(s).

将其插入到真棒Online Encyclopedia of Integer Sequences中,我们得到二项式系数二项式(n,4),就是这个(序列从偏移量4开始):

enter image description here

binomial(n,4)
n*(n-1)*(n-2)*(n-3)/24
0 = 0
1 = 0
2 = 0
3 = 0
4 = 1
5 = 5
6 = 15
7 = 35
...

如果我们查看上面程序输出的执行模式,我们可以使用求和和二项式序列重写它。对于介于1和n之间的每个整数i,我们在(n - i + 4)th序列中有binomial(n,4)个数字,然后乘以i作为执行总数。这表示如下:

First Equation

代替j = n - i + 1,并意识到j从n downto 1开始,我们可以将此等式重写为:

enter image description here

依靠Wolfram Alpha计算出这个等式,我插入了sum (n-j+1)(j+3)(j+2)(j+1)*j/24, j = 1 to n,它提出了:

enter image description here

这非常明显是Θ(n 6 ),所以这是我们的答案。

最后的等式实际上是binomial(n,6),因此对于m个循环,最内层循环的执行次数可能是binomial(n,m)。对于给定数量的m个循环,我们有:

enter image description here

答案 1 :(得分:1)

这样做的一个好方法是考虑你正在迭代的空间。如果你考虑一下,循环将迭代(a,b,c,d,e,f)的非负整数值

  

n&gt; f≥e≥d≥c≥b≥a

这些迭代中的每一个都执行O(1)工作(所有循环只分配一个变量,这需要O(1)工作),因此问题是有多少可能的值满足上述公式。我将声称它是Θ(n 6 ,并将尝试用我的其余答案来证明这一点。

首先,请注意该值当然不会超过O(n 6 )。 a,b,c,d,e和f的全部范围在0和n-1之间,因此每个最多n个不同的值。因此,它们可以具有的最大可能值是n 6 。这不是一个严格的约束,但它肯定是一个上限。这使我们运行时最多为O(n 6 )。

如果我们想要更严格的约束,我们必须更加努力。为此,我将使用以下事实:

  

1 k + 2 k + 3 k + ... + n k =Θ(n ķ

这是几何系列的总和,它就是它的来源。

这意味着

sum(f from 0 to n-1)
   sum (e from 0 to f)
      sum (d from 0 to e)
          sum (c from 0 to d)
              sum (b from 0 to c)
                  sum (a from 0 to b)
                      1

=  sum(f from 0 to n-1)
     sum (e from 0 to f)
        sum (d from 0 to e)
            sum (c from 0 to d)
                sum (b from 0 to c)
                    Theta(b)

=  sum(f from 0 to n-1)
     sum (e from 0 to f)
        sum (d from 0 to e)
            sum (c from 0 to d)
                Theta(c^2)

=  sum(f from 0 to n-1)
     sum (e from 0 to f)
        sum (d from 0 to e)
            Theta(d^3)

=  sum(f from 0 to n-1)
     sum (e from 0 to f)
        Theta(e^4)

=  sum(f from 0 to n-1)
     Theta(f^5)

= Theta(n^6)

希望这有帮助!