Example of O(n!)?

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

    Finding the determinant with expansion by minors.

    Very good explanation here.

    # include 
    # include 
    
    bool det_by_minor()
    {   bool ok = true;
    
        // dimension of the matrix
        size_t n = 3;
    
        // construct the determinat object
        CppAD::det_by_minor 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 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.

提交回复
热议问题