Algorithm 这个peudo代码的时间复杂度是多少?
我没有太多计算复杂性的知识。您能帮助估计以下伪代码的复杂性吗 算法1:Algorithm 这个peudo代码的时间复杂度是多少?,algorithm,performance,time-complexity,pseudocode,Algorithm,Performance,Time Complexity,Pseudocode,我没有太多计算复杂性的知识。您能帮助估计以下伪代码的复杂性吗 算法1: Input: V1, V2 and V3 and another vector C// Vectors of size n Output: response.. V_f = f(V1, V2, 3) // function performs simple multiplication and additions on the vector for i in range(0,n) // loop over element in
Input: V1, V2 and V3 and another vector C// Vectors of size n
Output: response..
V_f = f(V1, V2, 3) // function performs simple multiplication and additions on the vector
for i in range(0,n) // loop over element in the vector
if V_f(i) != C(i)
// sort the V1(i), V2(i) and V3(i) and retrieve the middle value
// if the middle value is in a range of certain values then launch Algorithm 2
// Over the result of Algorithm 2 (using if expressions), print the response
// end and return result
算法2
Input: Sorted
Values C{1}, C{2} and C{3} and the vector C
Output: Response:
for i in range (o,n) // loop over the elements
// According to the values of C and C{i}, perform additions (using if expressions)
// end and return result
循环中的操作只是添加或简单的测试。此外,算法2使用算法1执行,这意味着我在循环中有一个循环(对吗?):
那么这是否意味着该算法的时间复杂度是O(n^2)
?其中n是向量的大小
同样作为一个一般性问题,如果算法1和算法2并行执行,那么总体复杂度是多少?它是每个算法复杂度的总和还是最大值?Algorithm1
3*N
,这将被视为O(N)
0循环到N
。假设在每种情况下V_f(i)!=C(i)
,您必须对V1[i]
,V2[i]
,V3[i]
进行排序,这将花费恒定的O(1)
时间
for i in range(0,n) // loop over element in the vector
if V_f(i) != C(i)
在下一条语句中,您将检查上述排序元素的中间值是否在特定范围内-//如果中间值在特定值的范围内,则启动算法2
,现在,这一操作的时间复杂性取决于检查的方式和范围的大小。我假设您需要在从a
到b
的连续范围内进行检查,因此此步骤只需O(1)
。现在将调用算法2
for i in range (o,n) // loop over the elements
// According to the values of C and C{i}, perform additions (using if expressions)
// end and return result
在这里,您将再次从0循环到N
,并在每次迭代中执行一些计算,这将花费O(1)
。因此,整个算法的总时间复杂度为O(N)
那么这是否意味着这个算法的时间复杂度是O(n^2) 现在,假设是最坏的情况,您必须在中运行算法2
算法1循环内的每次迭代。正如你所说,时间复杂度是O(N^2)。请注意,这也将取决于计算的简单程度、在一定范围内检查某些值的方式以及最终时间复杂度中的常数。但是假设它们不超过
O(N)
,那么您的总体时间复杂度将是O(N^2)
,这是否回答了您的问题?“中间值”是指“中间值”吗?如果是这样,您真的需要“排序”(O(nlogn)),还是可以由O(1)算法来确定?我不确定并行化,但我认为它不能降低时间复杂度。存在相互依赖性。所以我甚至不确定这个程序是否能够并行运行。有人纠正我,如果我错了,因为我不完全确定这一点。谢谢你的回答,这意味着我的代码是坏的哈哈。是的,你是对的,这些算法不能并行运行。老实说,这只是一个一般性的问题。
for i in range(0,n) // loop over element in the vector
if V_f(i) != C(i)
for i in range (o,n) // loop over the elements
// According to the values of C and C{i}, perform additions (using if expressions)
// end and return result