邻接列表中列表的计数长度的时间复杂性?
假设我有一个邻接列表,例如:
A1: b1 b2 b3
A2: b3 b4
A3: b4
A4: b1 b3 b4
在整个邻接列表中找到每个“ sublist”的长度的时间复杂是什么?输出为:
[A1: 3, A2: 2, A3: 1, A4: 3]
由于它是邻接列表,所以我认为它是O(E+V)。但是,既然我们正在迭代所有内容,那么它实际上是o(e*v),就像嵌套的循环吗?
到处都在寻找,我仍然在挣扎,谢谢您的帮助!
Let's say I have an adjacency list, for example:
A1: b1 b2 b3
A2: b3 b4
A3: b4
A4: b1 b3 b4
What would be the time complexity to find the length of each 'sublist' in the whole adjacency list. The output being:
[A1: 3, A2: 2, A3: 1, A4: 3]
Since it is an adjacency list, I thought it would be O(E+V). But since we're kind of iterating through everything, is it actually O(E*V), like a nested for loop?
Been looking everywhere and I'm still struggling, thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,您正在做的是通过边缘迭代。但是,如果有比边缘更多的顶点,那么您仍然迭代所有顶点。您没有做的是遍历每个顶点的图表中的所有边缘。
o(e + v)只是说 o(max(e,v))的另一种方式。因此,该算法是线性的,这只是意味着,如果您有1个边缘,但是 n 顶点,它仍然是 o(n),而不是 o(1)。
In the end, what you are doing is iterate through the edges. But if there are more vertices than edges, you still iterate through all the vertices. What you are not doing is iterate through all the edges in the graph FOR EVERY vertex.
O(E + V) is just another way of saying O(max(E, V)). So the algorithm is linear, it just means that if you have 1 edge but n vertices, it's still O(n) and not O(1).