Java 给出一个递归算法来解决递归关系,并给出最坏情况下的时间复杂度,这是正确的吗?
求时间复杂度,在最坏的情况下,函数为n=2N, N>=0 找到递推关系并求解它Java 给出一个递归算法来解决递归关系,并给出最坏情况下的时间复杂度,这是正确的吗?,java,algorithm,recursion,time-complexity,recurrence,Java,Algorithm,Recursion,Time Complexity,Recurrence,求时间复杂度,在最坏的情况下,函数为n=2N, N>=0 找到递推关系并求解它 public static void xpto(v, n){ if (n <= 1) return; n=n/2; for(i=0;i<n;i=i+1) v[i] = v[2i] + v[2i +1]; xpto(v, n); } T(1) = 1 模式发现 T(n/8) = 3(4) + 2n(4) + T(n/2^4) k的一
public static void xpto(v, n){
if (n <= 1)
return;
n=n/2;
for(i=0;i<n;i=i+1)
v[i] = v[2i] + v[2i +1];
xpto(v, n);
}
T(1) = 1
模式发现
T(n/8) = 3(4) + 2n(4) + T(n/2^4)
k的一般重现性:
T(n) = 3(k) + 2n(k) + T(n/2^k)
if T(1) = 1 and T(n/2^k) we need to change 2^k by n, this means:
2^k = n
T(n) = 3(log n) + 2n(log n) + 1
求解了递推关系
时间复杂度,在最坏的情况下是O(log(n))
问题:
- 我这样做对吗
- n=2N,n>=0的函数是什么意思
v[I]=v[2i]+v[2i+1]代码>的成本为1,其他所有内容都是免费的。(可以轻松调整,而不会影响以下计算的概念)
基于此,
T(n) = n/2 + T(n/2)
基于此,我们可以使用c=1,a=1,b=2
,并得出T(n)
在Theta(n^1)=Theta(n)
首先,如果您得到:T(n)=3(logn)+2n(logn)+1
作为最终解决方案,那么最糟糕的情况复杂性将不是logn
,而是n(logn)
因为术语2n(logn)
根据您的初始递归关系:T(n)=3+2n+T(n/2)
Assume n = 2^k and g(k) = T(n) such that:
g(k) = g(k-1) + 2*2^k + 3 (from simply substituting n=2^k and change of function)
g(k) = sum(i=1 to k) of (2*2^i + 3)
g(k) = 2 * (sum(i=1 to k) of (2^i)) + 3k
Using geometric progression, common ratio = 2:
g(k) = 2 * (2(1-2^k) / (1-2)) + 3k
g(k) = -4 + 4*2^k + 3k
Since we initially assumed n = 2^k, this means k = log n:
T(n) = -4 + 4n + 3(log n)
Hence the worst case complexity is O(n)
关于问题的第二部分:
n=2N,其中n>=0意味着n是一组偶数,因为任何正整数乘以2都是偶数。对于(i=0;我需要这个问题的正式答案,以及定理和原因。你在谈论哪些常数?@MauroAlmeidaT(n)=3+2N+T(n/2)
(3和2在2n
中)。好的,1和其他东西,这只是我说的1次的方式,我想,我在学习算法分析,不确定。n=2n,n>=0的函数意味着什么?
Assume n = 2^k and g(k) = T(n) such that:
g(k) = g(k-1) + 2*2^k + 3 (from simply substituting n=2^k and change of function)
g(k) = sum(i=1 to k) of (2*2^i + 3)
g(k) = 2 * (sum(i=1 to k) of (2^i)) + 3k
Using geometric progression, common ratio = 2:
g(k) = 2 * (2(1-2^k) / (1-2)) + 3k
g(k) = -4 + 4*2^k + 3k
Since we initially assumed n = 2^k, this means k = log n:
T(n) = -4 + 4n + 3(log n)
Hence the worst case complexity is O(n)