简体   繁体   English

排列的时间复杂度 function

[英]Time Complexity of permutation function

Given a collection of distinct numbers, return all possible permutations.给定一组不同的数字,返回所有可能的排列。

For example, [1,2,3] have the following permutations:例如,[1,2,3] 有以下排列:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

My Iterative Solution is:我的迭代解决方案是:

public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>());
        for(int i=0;i<nums.length;i++)
        {
            List<List<Integer>> temp = new ArrayList<>();
            for(List<Integer> a: result)
            {
                for(int j=0; j<=a.size();j++)
                {
                    a.add(j,nums[i]);
                    List<Integer> current = new ArrayList<>(a);
                    temp.add(current);
                    a.remove(j);
                }
            }
            result = new ArrayList<>(temp);
        }
        return result;
    }

My Recursive Solution is:我的递归解决方案是:

public List<List<Integer>> permuteRec(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        makePermutations(nums, result, 0);
        return result;
    }


void makePermutations(int[] nums, List<List<Integer>> result, int start) {
    if (start >= nums.length) {
        List<Integer> temp = convertArrayToList(nums);
        result.add(temp);
    }
    for (int i = start; i < nums.length; i++) {
        swap(nums, start, i);
        makePermutations(nums, result, start + 1);
        swap(nums, start, i);
    }
}

private ArrayList<Integer> convertArrayToList(int[] num) {
        ArrayList<Integer> item = new ArrayList<Integer>();
        for (int h = 0; h < num.length; h++) {
            item.add(num[h]);
        }
        return item;
    }

According to me the time complexity(big-Oh) of my iterative solution is: n * n(n+1)/2~ O(n^3)根据我的说法,我的迭代解决方案的时间复杂度(大哦)是: n * n(n+1)/2~ O(n^3)
I am not able to figure out the time complexity of my recursive solution.我无法计算出我的递归解决方案的时间复杂度。
Can anyone explain complexity of both?谁能解释两者的复杂性?

The recursive solution has a complexity of O(n!) as it is governed by the equation: T(n) = n * T(n-1) + O(1) .递归解决方案的复杂度为O(n!)因为它由以下方程控制: T(n) = n * T(n-1) + O(1)

The iterative solution has three nested loops and hence has a complexity of O(n^3) .迭代解决方案具有三个嵌套循环,因此具有O(n^3)的复杂性。

However, the iterative solution will not produce correct permutations for any number apart from 3 .但是,迭代解决方案不会为除3之外的任何数字生成正确的排列。

For n = 3 , you can see that n * (n - 1) * (n-2) = n!对于n = 3 ,您可以看到n * (n - 1) * (n-2) = n! . . The LHS is O(n^3) (or rather O(n^n) since n=3 here) and the RHS is O(n!) . LHS 是O(n^3) (或者更确切地说是O(n^n)因为这里n=3 ),RHS 是O(n!)

For larger values of the size of the list, say n , you could have n nested loops and that will provide valid permutations.对于列表大小的较大值,例如n ,您可以有n嵌套循环,这将提供有效的排列。 The complexity in that case will be O(n^n) , and that is much larger than O(n!) , or rather, n! < n^n在这种情况下,复杂性将是O(n^n) ,这比O(n!)大得多,或者更确切地说,是n! < n^n n! < n^n . n! < n^n There is a rather nice relation called Stirling's approximation which explains this relation.有一个相当不错的关系称为斯特林近似,它解释了这种关系。

It's the output (which is huge) matters in this problem, not the routine's implementation.在这个问题中,输出(这是巨大的)很重要,而不是例程的实现。 For n distinct items, there are n!对于n不同的项目,有n! permutations to be returned as the answer, and thus we have at least O(n!) complexity.排列作为答案返回,因此我们至少有O(n!)复杂度。

With a help of Stirling's approximation借助斯特林近似

 O(n!) = O(n^(1/2+n)/exp(n)) = O(sqrt(n) * (n/e)^n)

we can easily see, that O(n!) > O(n^c) for any constant c , that's why it doesn't matter if the implementation itself adds another O(n^3) since我们可以很容易地看到,对于任何常量cO(n!) > O(n^c) ,这就是为什么实现本身添加另一个O(n^3)并不重要,因为

 O(n!) + O(n^3) = O(n!)

In terms of number of times the method makePermutations is called, the exact time complexity would be:就调用makePermutations方法的次数而言,确切的时间复杂度为:

O( 1 + n + n(n-1) + n(n-1)(n-2) + ... )

For n = 3:对于 n = 3:

O( 1 + 3 + (3*2) + (3*2*1) ) = O(16)

This means, for n = 3, the method makePermutations will be called 16 times.这意味着,对于 n = 3,方法makePermutations将被调用 16 次。

And I think the space complexity for an optimal permutations function would be O(n * n,) because there are n.我认为最佳排列 function 的空间复杂度为 O(n * n,),因为有 n。 total arrays to return, and each of those arrays is of size n.返回的总数为 arrays,其中每个 arrays 的大小为 n。

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

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