Example of O(n!)?

后端 未结 16 2403
渐次进展
渐次进展 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:41

    There you go. This is probably the most trivial example of a function that runs in O(n!) time (where n is the argument to the function):

    void nFacRuntimeFunc(int n) {
      for(int i=0; i<n; i++) {
        nFacRuntimeFunc(n-1);
      }
    }
    
    0 讨论(0)
  • 2020-11-30 22:42

    There are problems, that are NP-complete(verifiable in nondeterministic polynomial time). Meaning if input scales, then your computation needed to solve the problem increases more then a lot.

    Some NP-hard problems are: Hamiltonian path problem( open img ), Travelling salesman problem( open img )
    Some NP-complete problems are: Boolean satisfiability problem (Sat.)( open img ), N-puzzle( open img ), Knapsack problem( open img ), Subgraph isomorphism problem( open img ), Subset sum problem( open img ), Clique problem( open img ), Vertex cover problem( open img ), Independent set problem( open img ), Dominating set problem( open img ), Graph coloring problem( open img ),

    Source: link 1, link 2

    alt text
    Source: link

    0 讨论(0)
  • 2020-11-30 22:43

    One classic example is the traveling salesman problem through brute-force search.

    If there are N cities, the brute force method will try each and every permutation of these N cities to find which one is cheapest. Now the number of permutations with N cities is N! making it's complexity factorial (O(N!)).

    0 讨论(0)
  • 2020-11-30 22:44

    Bogosort is the only "official" one I've encountered that ventures into the O(n!) area. But it's not a guaranteed O(n!) as it's random in nature.

    0 讨论(0)
  • 2020-11-30 22:46

    See the Orders of common functions section of the Big O Wikipedia article.

    According to the article, solving the traveling salesman problem via brute-force search and finding the determinant with expansion by minors are both O(n!).

    0 讨论(0)
  • 2020-11-30 22:46

    Finding the determinant with expansion by minors.

    Very good explanation here.

    # include <cppad/cppad.hpp>
    # include <cppad/speed/det_by_minor.hpp>
    
    bool det_by_minor()
    {   bool ok = true;
    
        // dimension of the matrix
        size_t n = 3;
    
        // construct the determinat object
        CppAD::det_by_minor<double> Det(n);
    
        double  a[] = {
            1., 2., 3.,  // a[0] a[1] a[2]
            3., 2., 1.,  // a[3] a[4] a[5]
            2., 1., 2.   // a[6] a[7] a[8]
        };
        CPPAD_TEST_VECTOR<double> A(9);
        size_t i;
        for(i = 0; i < 9; i++)
            A[i] = a[i];
    
    
        // evaluate the determinant
        double det = Det(A);
    
        double check;
        check = a[0]*(a[4]*a[8] - a[5]*a[7])
              - a[1]*(a[3]*a[8] - a[5]*a[6])
              + a[2]*(a[3]*a[7] - a[4]*a[6]);
    
        ok = det == check;
    
        return ok;
    }
    

    Code from here. You will also find the necessary .hpp files there.

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