递归关系

发布于 2024-08-04 02:12:26 字数 228 浏览 8 评论 0原文

为什么递归阶乘算法的递推关系是这样的?

T(n)=1 for n=0
T(n)=1+T(n-1) for n>0

为什么不是这个呢?

T(n)=1 for n=0
T(n)=n*T(n-1) for n>0

输入 n 的值,即 1,2,3,4……第二个递推关系成立(阶乘计算正确)而不是第一个。

Why is the recurrence relation of recursive factorial algorithm this?

T(n)=1 for n=0
T(n)=1+T(n-1) for n>0

Why is it not this?

T(n)=1 for n=0
T(n)=n*T(n-1) for n>0

Putting values of n i.e 1,2,3,4...... the second recurrence relation holds(The factorials are correctly calculated) not the first one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

无敌元气妹 2024-08-11 02:12:26

我们通常使用递归关系来求算法的时间复杂度。


这里,函数T(n)实际上并不是用于计算阶乘的值,而是告诉你阶乘的时间复杂度算法。


这意味着求 n 的阶乘比 n-1 的阶乘多需要 1 次运算
(即T(n)=T(n-1)+1)等等。


所以递归阶乘算法的正确递归关系是
T(n)=1(n=0)
T(n)=1+T(n-1),n>0
不是你后面提到的。


就像河内塔的重现一样
当n>0时,T(n)=2T(n-1)+1;

更新:
一般来说,它与实施没有任何关系。
但是递归可以给出编程范式的直觉,例如如果 T(n)=2*T(n/2)+n (合并排序),这给出了分而治之的直觉,因为我们将 n 分成两半。

另外,如果你要解方程,它会给你运行时间带来限制。例如大哦符号。

we generally use recurrence relation to find the time complexity of algorithm.


Here, the function T(n) is not actually for calculating the value of an factorial but it is telling you about the time complexity of the factorial algorithm.


It means for finding a factorial of n it will take 1 more operation than factorial of n-1
(i.e. T(n)=T(n-1)+1) and so on.


so correct recurrence relation for a recursive factorial algorithm is
T(n)=1 for n=0
T(n)=1+T(n-1) for n>0
not that you mentioned later.


like recurrence for tower of hanoi is
T(n)=2T(n-1)+1 for n>0;

Update:
It does not have anything to do with implementation generally.
But recurrence can give an intuition of programming paradigm for eg if T(n)=2*T(n/2)+n (merge sort) this gives kind of intuition for divide and conquer because we are diving n into half.

Also, If you will solve the equation it will give you a bound on running time.eg big oh notation.

暮倦 2024-08-11 02:12:26

看起来 T(n) 是递归阶乘算法的时间复杂度的递推关系,假设常数时间乘法。也许你误读了你的来源?

Looks like T(n) is the recurrence relation of the time complexity of the recursive factorial algorithm, assuming constant time multiplication. Perhaps you misread your source?

西瑶 2024-08-11 02:12:26

他说的不是阶乘递归,而是它的时间复杂度。
假设这是这种递归的伪代码:

1. func factorial(n)
2.   if (n == 0)
3.      return 1
4.   return n * (factorial - 1)
  • 我假设不涉及尾递归消除。

第 2 行和第 3 行花费恒定时间 c1 和 c2。
4 号线也花费固定时间。然而,它调用阶乘(n-1),这将需要一些时间T(n-1)。此外,一旦使用 T(n-1),则可以忽略将 Factorial(n-1) 乘以 n 所需的时间。
整个函数的时间只是总和:T(n) = c1 + c2 + T(n-1)。
用大 O 表示法,可以将其简化为 T(n) = 1 + T(n-1)。

正如 Diam 指出的那样,这是一个平面递归,因此它的运行时间应该是 O(n)。但它的空间复杂度将是巨大的。

What he put was not the factorial recursion, but the time complexity of it.
Assuming this is the pseudocode for such a recurrence:

1. func factorial(n)
2.   if (n == 0)
3.      return 1
4.   return n * (factorial - 1)
  • I am assuming that tail-recursion elimination is not involved.

Line 2 and 3 costs a constant time, c1 and c2.
Line 4 costs a constant time as well. However, it calls factorial(n-1) which will take some time T(n-1). Also, the time it takes to multiply factorial(n-1) by n can be ignored once T(n-1) is used.
Time for the whole function is just the sum: T(n) = c1 + c2 + T(n-1).
This, in big-o notation, is reduced to T(n) = 1 + T(n-1).

This is, as Diam has pointed out, is a flat recursion, therefore its running time should be O(n). Its space complexity will be enormous though.

弄潮 2024-08-11 02:12:26

我假设你有错误的信息。正如您所观察到的,您引用的第二个递归关系正确的。第一个仅生成自然数。

I assume that you have bad information. The second recurrence relation you cite is the correct one, as you have observed. The first one just generates the natural numbers.

放低过去 2024-08-11 02:12:26

这个问题很令人困惑......你的第一个公式不是阶乘。对于所有 n,它只是 T(n) = n + 1。 n 的阶乘是前 n 个正整数的乘积:阶乘 (1) = 1。阶乘 (n) = n * 阶乘 (n-1)。你的第二个公式基本上是正确的。

This question is very confusing... You first formula is not factorial. It is simply T(n) = n + 1, for all n. Factorial of n is the product of the first n positive integers: factorial(1) = 1. factorial(n) = n * factorial(n-1). Your second formula is essentially correct.

可是我不能没有你 2024-08-11 02:12:26

T(n) = T(n-1) + 1 是 n 阶乘的正确递推方程。
该方程为您提供了计算 n 阶乘而不是 n 阶乘值的时间。

T(n) = T(n-1) + 1 is correct recurrence equation for factorial of n.
This equation gives you the time to compute factorial of n NOT value of the factorial of n.

箜明 2024-08-11 02:12:26

你在哪里找到第一个?这是完全错误的。

无论值是多少,每次都会加 1。

Where did you find the first one ? It's completely wrong.

It's only going to add 1 each time whatever the value is .

我的奇迹 2024-08-11 02:12:26

首先,您必须找到一个基本运算,在本例中它是乘法。每次递归都会发生一次乘法。所以
T(n) = T(n-1) +1
这个+1是基本运算(本例中是乘法)
T(n-1) 是下一个递归调用。

First you have to find a basic operation and for this example it is multiplication. Multiplication happens once in every recursion. So
T(n) = T(n-1) +1
this +1 is basic operation (mutliplication for this example)
T(n-1) is next recursion call.

世界等同你 2024-08-11 02:12:26

TL;DR:您问题的答案实际上取决于您定义的递归关系的顺序。也就是说,您问题中的序列 Tn 代表的是阶乘函数还是计算阶乘函数的运行时间成本X


阶乘函数

nfn 阶乘的递归定义为:

f< sub>n = n • fn-1 对于 n > 0f0 = 1

正如您所看到的,上面的方程实际上是一个递归关系,因为它是一个方程,与初始项一起使用(即, f0 = 1),递归地定义一个序列(即阶乘函数,f<子>n)。


对计算阶乘的运行时间成本进行建模

现在,我们将找到一个模型来表示计算n 阶乘的运行时间成本。我们将Tn称为计算fn运行时间成本

查看上面阶乘函数 fn 的定义,其运行时间成本 Tn 将包括计算的运行时成本fn-1(即,此成本为Tn-1)加上执行 nfn-1 之间的乘法的运行时间成本。乘法是在恒定时间内实现的。因此我们可以说Tn = Tn-1 + 1

但是,T0 的值是多少? T0 表示计算 f0 的运行时成本。由于 f0 的值最初根据定义已知,因此计算 f0 的运行时成本为实际上是恒定的。因此,我们可以说 T0 = 1

最后,我们得到的是:

Tn = Tn-1 + 1 for n > 0T0 = 1

上面的这个方程也是一个递归关系。然而,它定义的(与初始项一起)是一个对计算阶乘函数的运行时间成本进行建模的序列。


X考虑到递归关系中的序列如何被调用(即Tn),我认为它很可能代表后者,即计算阶乘函数的运行时间成本

TL;DR: The answer to your question actually depends on what sequence your recurrence relation is defining. That is, whether the sequence Tn in your question represents the factorial function or the running-time cost of computing the factorial functionX.


The factorial function

The recursive defintion of the factorial of n, fn, is:

fn = n • fn-1 for n > 0 with f0 = 1.

As you can see, the equation above is actually a recurrence relation, since it is an equation that, together with the initial term (i.e., f0 = 1), recursively defines a sequence (i.e., the factorial function, fn).


Modelling the running-time cost of computing the factorial

Now, we are going to find a model for representing the running-time cost of computing the factorial of n. Let's call Tn the running-time cost of computing fn.

Looking at the definition above of the factorial function fn, its running-time cost Tn will consist of the running-time cost of computing fn-1 (i.e., this cost is Tn-1) plus the running-time cost of performing the multiplication between n and fn-1. The multiplication is achieved in constant time. Therefore we could say that Tn = Tn-1 + 1.

However, what is the value of T0? T0 represents the running-time cost of computing f0. Since the value of f0 is initially known by definition, the running-time cost for computing f0 is actually constant. Therefore, we could say that T0 = 1.

Finally, what we obtain is:

Tn = Tn-1 + 1 for n > 0 with T0 = 1.

This equation above is also a recurrence relation. However, what it defines (together with the initial term), is a sequence that models the running-time cost of computing the factorial function.


XTaking into account how the sequence in your recurrence relation is called (i.e., Tn), I think it very likely represents the latter, i.e., the running-time cost of computing the factorial function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文