递归串置换功能的复杂性递归、复杂性、功能

2023-09-11 03:39:17 作者:愤怒的少年°

从:Are有没有更好的方法做字符串置换?

什么是这个函数???

的复杂性

 无效置换(字符串elems,诠释中期,诠释完)
{
    静态诠释计数;
    如果(==中旬结束){
        COUT<< ++计数<< :&其中;&其中; elems<< ENDL;
        返回 ;
    }
    其他 {
    的for(int i =中间; I< =结束;我++){
            掉期(elems,中,I);
            置换(elems,中+ 1,结束);
            掉期(elems,中,I);
        }
    }
}
 

解决方案

忽略打印,满足递推关系是

T(N)= N * T(N-1)+ O(N)

如果 G(N)= T(N)/ N!我们得到

G(N)= G(N-1)+ O(1 /(N-1)!)

这使得 G(N)=西塔(1)

因此​​ T(N)=西塔(N!)

假设打印的情况完全相同 N!的时候,我们得到的时间复杂度为

的Theta(N * N!)

算法设计课程总结1 2 渐进分析记号,复杂性比较,递归,分治

From: Are there any better methods to do permutation of string?

what is the complexity of this function???

void permute(string elems, int mid, int end)
{
    static int count;
    if (mid == end) {
        cout << ++count << " : " << elems << endl;
        return ;
    }
    else {
    for (int i = mid; i <= end; i++) {
            swap(elems, mid, i);
            permute(elems, mid + 1, end);
            swap(elems, mid, i);
        }
    }
}

解决方案

Ignoring the print, the recurrence relation satisfied is

T(n) = n*T(n-1) + O(n)

If G(n) = T(n)/n! we get

G(n) = G(n-1) + O(1/(n-1)!)

which gives G(n) = Theta(1).

Thus T(n) = Theta(n!).

Assuming that the print happens exactly n! times, we get the time complexity as

Theta(n * n!)