确定时间和空间复杂度
我发现这个主题令人困惑,而且我对这些术语很陌生。
我有一个这样的类:
public class Class1
{
private IDictionary<int, double> Dictionary1;
private List<double> pairList;
public int Func1()
{
double rand = random.Next();
int index = 0;
foreach (var pairs in Dictionary1)
{
if (something)
{
// do sth.
}
index++;
}
return 0;
}
public void Func2(List<KeyValuePair<string, string>> pairList)
{
foreach (var pair in pairList)
{
if (Dictionary1.TryGetValue(pair.Key, out double oldValue))
{
//do something
}
Dictionary1.Add(new KeyValuePair<int, double>(pair.Key, pair.Value));
}
}
}
问题 1:
时间复杂度是 O(n),对吗?如果我使用二分搜索 (O(logn)) 而不是 func1 的 foreach 循环,它仍然是 O(n),因为我们正在考虑最坏的情况。那么,这种转换在时间复杂度方面会毫无用处吗?
问题2:
这里的空间复杂度是多少?它是如何计算的?
问题 3:
我知道这很愚蠢,但我必须问: 主程序会影响这些计算吗?就像我也可以使用 for 循环或其他东西来测试 main() 函数一样,我是否也应该将它们添加到计算中?
I find this topic confusing and I am new to these terms.
I have a class like this:
public class Class1
{
private IDictionary<int, double> Dictionary1;
private List<double> pairList;
public int Func1()
{
double rand = random.Next();
int index = 0;
foreach (var pairs in Dictionary1)
{
if (something)
{
// do sth.
}
index++;
}
return 0;
}
public void Func2(List<KeyValuePair<string, string>> pairList)
{
foreach (var pair in pairList)
{
if (Dictionary1.TryGetValue(pair.Key, out double oldValue))
{
//do something
}
Dictionary1.Add(new KeyValuePair<int, double>(pair.Key, pair.Value));
}
}
}
Question 1:
The time complexity is O(n) for this, right? If I use a binary search (O(logn)) instead of the foreach loop the func1, it would still be O(n) since we're considering the worst case. So, would that transformation be useless in the terms of time complexity?
Question 2:
What is the space complexity here and how is it calculated?
Question 3:
I know it is silly but I have to ask:
Does the main program affect these calculations? Like I can also have for loops or other things for testing the main() functions, should I also add them to the calculations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案1:
是的,Func1 和 Func2 的时间复杂度都是 O(n),其中 n 分别是 Dictionary1 或pairList 中的项目数。
由于二分搜索的最坏情况是 O(log(n)),因此它会降低 Func1 的时间复杂度。请记住,您的列表必须经过排序才能进行二分搜索,因此这可能会增加额外的复杂性。
然而,如果你的 main 方法调用这两个函数,它的复杂度仍然是 O(n)。无论 Func1 有多复杂。
因此,就复杂性而言,您可能会认为它“无用”。但是 Func1 的优化仍然会减少计算 main 所需的时间。仅仅因为它不会降低您的时间复杂度,并不意味着您的优化毫无价值。
答案2:
空间复杂度描述的是计算过程中所需的内存,而不是计算时间。基本上,它由需要创建的附加变量的数量决定。
因此,要确定 Func1 的复杂性,取决于您的
// do sth.
的作用。我对此不太确定,但我认为 Func2 的空间复杂度将为 O(n),因为它会通过pairList 中的项目数量增加 Dictionary1 的大小。同样,
//do some
的内容可能会增加复杂性。答案3:
这取决于您想要计算复杂性的目的。如果你想计算 Func1 和 Func2 的复杂度,那么 main 中发生的事情并不重要。然而,如果你想计算main的复杂度,它绝对是重要的。可以说,你的 main 包含这样的内容:
由于你的循环内有复杂度为 O(n) 的函数,循环 n 次,所以 main 的时间复杂度现在是 O(n²)。但这并不影响Func1和Func2本身的复杂性。
Answer 1:
Yes, the time complexity for both Func1 and Func2 is O(n), with n being the number of items Dictionary1 or in pairList respectively.
Since the worst case of binary search is O(log(n)) it would decrease your time complexity for Func1. Keep in mind, that your list has to be sorted for a binary search to work, so that might add additional complexity.
However, if your main method calls both functions, it would still have a complexity of O(n). No matter what complexity Func1 has.
So you might consider it "useless" in terms of complexity. But an optimization of Func1 would still reduce the time it takes to compute main. Just because it doesn't reduce your time complexity, it doesn't mean, that your optimization is worthless.
Answer 2:
Space complexity describes the memory that is needed during computation, instead of the computing time. Basically, it is determined by the amount of additional variables that need to be created.
So to determine the complexity of Func1, it depends on what your
// do sth.
does.I am not sure about this, but i think the space complexity of Func2 would be O(n), because it increses the size of your Dictionary1 by the amount of items in pairList. Again, the complexity might be increased by the content of
//do something
.Answer 3:
It depends on what you want to calculate your complexity for. If you want to calculate the complexity of Func1 and Func2, it doesn't matter what happens in main. However, if you want to calculate the complexity of main, it is definitely important. Lets say, your main contains something like this:
Since you have your functions of complexity O(n) inside of a loop, that loops n times, the time complexity of main is now O(n²). But this doesn't affect the complexity of Func1 and Func2 itself.