递归串置换功能的复杂性

编程入门 行业动态 更新时间:2024-10-13 06:16:59
本文介绍了递归串置换功能的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从: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!)

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!)

更多推荐

递归串置换功能的复杂性

本文发布于:2023-11-29 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   复杂性   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!