对于稀疏mxn矩阵A(n是偶数,m=3n/2-2)的三角化,我有以下(部分)代码:
y=0;
for k=1:n
if(mod(k,2)==0)
y=y+1;
end
for j=k+1:n
A(k,j)=A(k,j)-tau*U(k,k);
if (k<=n-2)
for i=n-1:n
A(i,j)=A(i,j)-tau*U(i,k);
end
end
for i=n+1:n-2+y
A(i,j)=A(i,j)-tau*U(i,k);
end
end
end
我感兴趣的是,作为m和n的函数,它的精确算法复杂度(在大O表示法中)。由于if分支,我得到了不同的结果。
发布于 2013-12-14 18:38:02
y=0;
// n times
for k=1:n
// some constant complexity stuff
// it basically sets y to floor(k/2)
if(mod(k,2)==0)
y=y+1;
end
// n-k times
for j=k+1:n
A(k,j)=A(k,j)-tau*U(k,k);
// executed everytime except the last few iteration of the outermost loop
if (k<=n-2)
// constant complexity
for i=n-1:n
A(i,j)=A(i,j)-tau*U(i,k);
end
end
// executes y-2 times
for i=n+1:n-2+y
// so we basically need to bound the number of times this is executed
A(i,j)=A(i,j)-tau*U(i,k);
end
end
end
在k
-th迭代中,中间循环迭代n-k
时间,最后一个最内部循环迭代y-2
,即floor(k/2)-2
时间(不管j
)。
因此,最内部的赋值为k=1:n
时间执行Θ(n^3)
,即Θ(n^3)
。
https://stackoverflow.com/questions/20585325
复制相似问题