Example of O(n!)?

后端 未结 16 2405
渐次进展
渐次进展 2020-11-30 22:20

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I\'m asking a

相关标签:
16条回答
  • 2020-11-30 22:48

    I think I'm a bit late, but I find snailsort to be the best example of O(n!) deterministic algorithm. It basically finds the next permutation of an array until it sorts it.

    It looks like this:

    template <class Iter> 
    void snail_sort(Iter first, Iter last)
    {
        while (next_permutation(first, last)) {}
    }
    
    0 讨论(0)
  • 2020-11-30 22:48

    In C#

    Wouldn't this be O(N!) in space complexity? because, string in C# is immutable.

    string reverseString(string orgString) {
        string reversedString = String.Empty;
    
        for (int i = 0; i < orgString.Length; i++) {
            reversedString += orgString[i];
        }
    
        return reversedString;
    }
    
    0 讨论(0)
  • 2020-11-30 22:50

    Add to up k function

    This is a simple example of a function with complexity O(n!) given an array of int in parameter and an integer k. it returns true if there are two items from the array x+y = k , For example : if tab was [1, 2, 3, 4] and k=6 the returned value would be true because 2+4=6

    public boolean addToUpK(int[] tab, int k) {
    
            boolean response = false;
    
            for(int i=0; i<tab.length; i++) {
    
                for(int j=i+1; j<tab.length; j++) {
    
                    if(tab[i]+tab[j]==k) {
                        return true;
                    }
    
                }
    
            }
            return response;
        }
    

    As a bonus this is a unit test with jUnit, it works fine

    @Test
        public void testAddToUpK() {
    
            DailyCodingProblem daProblem = new DailyCodingProblemImpl();
    
            int tab[] = {10, 15, 3, 7};
            int k = 17;
            boolean result = true; //expected result because 10+7=17
            assertTrue("expected value is true", daProblem.addToUpK(tab, k) == result);
    
            k = 50;
            result = false; //expected value because there's any two numbers from the list add up to 50
            assertTrue("expected value is false", daProblem.addToUpK(tab, k) == result);
        }
    
    0 讨论(0)
  • 2020-11-30 22:54

    You are right the recursive calls should take exactly n! time. here is a code like to test factorial time for n different values. Inner loop runs for n! time for different values of j, so the complexity of inner loop is Big O(n!)

    public static void NFactorialRuntime(int n)
        {
            Console.WriteLine(" N   Fn   N!");
            for (int i = 1; i <= n; i++)  // This loop is just to test n different values
            {
                int f = Fact(i);
                for (int j = 1; j <= f; j++)  // This is Factorial times
                {  ++x; }
                Console.WriteLine(" {0}   {1}   {2}", i, x, f);
                x = 0;
            }
        }
    

    Here are the test result for n = 5, it iterate exactly factorial time.

      N   Fn   N!
      1   1   1
      2   2   2
      3   6   6
      4   24   24
      5   120   120
    

    Exact function with time complexity n!

    // Big O(n!)
    public static void NFactorialRuntime(int n)
        {
            for (int j = 1; j <= Fact(i); j++) {  ++x; }
            Console.WriteLine(" {0}   {1}   {2}", i, x, f);
        }
    
    0 讨论(0)
  • 2020-11-30 22:54

    @clocksmith You are absolutely correct. This is not calculating n!. Nor is it of O(n!). I ran it collected the data in the table below. Please compare column 2 and three. (#nF is the # of calls to nFacRuntimeFunc)

    n #nF n!

    0    0      1
    1    1      1
    2    4      2
    3    15     6
    4    65     24
    5    325    120
    6    1956   720
    7    13699  5040
    

    So clearly if performs much worse than O(n!). Below is the a sample code for calculating n! recursively. you will note that its of O(n) order.

    int Factorial(int n)
    {
       if (n == 1)
          return 1;
       else
          return n * Factorial(n-1);
    }
    
    0 讨论(0)
  • 2020-11-30 22:57

    The recursive method you probably learned for taking the determinant of a matrix (if you took linear algebra) takes O(n!) time. Though I dont particularly feel like coding that all up.

    0 讨论(0)
提交回复
热议问题