简体   繁体   English

为什么在邻接表O(| E | / | V |)$中进行运算?

[英]Why are operations in an adjacency list O(|E|/|V|)$?

I'm studying for an exam I have soon. 我正在为即将参加的考试而学习。 A chart provided to me has the following algorithmic complexities summarized for an adjacency list for a graph with N nodes and E edges. 提供给我的图表具有以下算法复杂性,对于具有N个节点和E个边的图的邻接表,总结了以下算法复杂性。

  • Find edge - O(E/N) 查找边-O(E / N)

  • Insert edge - O(E/N) 插入边-O(E / N)

  • Delete edge - O(E/N) 删除边缘-O(E / N)

  • Enumerate edges for node - O(E/N) 枚举节点的边-O(E / N)

I understand what an adjacency list is - we store the vertices adjacent to each vertex by using an array of lists. 我了解邻接表是什么-我们通过使用列表数组存储与每个顶点相邻的顶点。 But why are these operations O(E/N)? 但是为什么这些运算为O(E / N)? It seems to me like, if we took a graph in which every possible edge is drawn (eg, we have n(n - 1)/2 edges if the graph is undirected), then each list in the array would have N - 1 entries to store every other node 在我看来,如果我们绘制一个绘制了每个可能边的图(例如,如果该图是无向的,则我们有n(n-1)/ 2个边),那么数组中的每个列表都将有N-1存储其他节点的条目

This, in my mind, would be the "worst case," wouldn't it? 在我看来,这将是“最坏的情况”,不是吗? I don't understand how the ratio of edges to nodes is being obtained. 我不明白如何获得边与节点的比率。

Can someone please explain? 有人可以解释一下吗?

I believe this question is very similar to this other question here on stackoverflow, please refer to it since it may answer your question already. 我相信这个问题与此处关于stackoverflow的另一个问题非常相似,请参考它,因为它可能已经回答了您的问题。 For completion sake I'll try to summarise what I understand about the topic too but I'm not authority in the subject, so feel free to correct if I'm saying anything wrong: 为了完整起见,我也会尝试总结一下我对该主题的理解,但是我不是该主题的权威,因此,如果我说错了任何内容,请随时进行纠正:

For what I could understand you are questioning why the chart says an operation is O(E/N) when it is well known that the worst case is O(N). 据我所知,您正在质疑为什么图表在已知最坏的情况是O(N)时说运算是O(E / N)。 Well, there are 2 issues here: 好吧,这里有两个问题:

  1. You're assuming that big O means "worst case input" but, by definition, we can't assume this. 您假设大O表示“最坏情况的输入”,但是根据定义,我们不能假设这一点。
  2. The chart says only O(E/N) and, as @domen commented, the text should be clearer and indicate what input case it is considering. 该图表仅显示O(E / N),并且正如@domen所评论的那样,文本应更清楚并指示其正在考虑的输入情况。

A quick answer here is that big O can be used to "talk" about both cases. 一个快速的答案是,可以使用大O来“谈论”这两种情况。 It will be O(E/N) when we are talking about the average input and it will be O(N) when we are talking about the worst input. 当我们谈论平均输入时,它将是O(E / N);当我们谈论最差的输入时,它将是O(N)。

Now let's see a longer answer addressing each of the enumerated issues: Accordingly to the book "Introduction to algorithms" we can define big O as: 现在,让我们看一个更长的答案来解决每个列举的问题:根据《算法简介》这本书我们可以将big O定义为:

O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0} O(g(n))= {f(n):存在正常数c和n0,使得所有n> = n0的0 <= f(n)<= cg(n)}

Note that the definition doesn't say anything about worst case, it just says that if we have a function f(n) and we can provide a constant c and a n0 such that 0 <= f(n) <= cg(n) for every n >= n0 then f(n) is in O(g(n)). 请注意,定义没有说最坏的情况,它只是说如果我们有一个函数f(n),我们可以提供一个常数c和一个n0,使得0 <= f(n)<= cg(n ),每n> = n0,则f(n)在O(g(n))中。 So forget about worst case here, if we can provide a function f(n), a constant c and a n0 that doesn't violate the above definition then f(n) is in O(n). 因此,在这里忘掉最坏的情况,如果我们可以提供一个函数f(n),一个常数c和一个不违反上述定义的n0,则f(n)在O(n)中。
Here we are justing talking about the upper bound for that input case only, which could be the worst input, the average input or any other input case. 在这里,我们仅讨论该输入情况的上限,它可能是最差的输入,平均输入或任何其他输入情况。
If an algorithm has "worst input" = w(n) and "average input" = a(n) where exists a c',n'0 such that 0 <= w(n) <= c'g(n) for every n >= n'0 and exists a c'',n''0 such that 0 <= a(n) <= c''g(e/n) for every n >= n''0 then we can say that the algorithm is O(n) in the worst case and O(e/n) in the average case. 如果算法具有“最差输入” = w(n)和“平均输入” = a(n),其中存在一个c',n'0,使得0 <= w(n)<= c'g(n)每n> = n'0并存在一个c'',n''0使得每n> = n''0 0 <= a(n)<= c''g(e / n)那么我们可以假设算法在最坏的情况下为O(n),在平均情况下为O(e / n)。

If the chart doesn't not specify the f(n) it is considering (worst case or average case as an example) then we cannot affirm anything, the chart must be more specific. 如果图表未指定要考虑的f(n)(以最坏情况或平均情况为例),则我们无法确认任何内容,因此图表必须更加具体。
The common behavior here is to assume that the text is referring to the worst case input and, that's probably why we relate Big O to the worst case, while most of the time this assumption is right sometimes (like the chart you mentioned) it's wrong. 此处的常见行为是假设文本是指最坏情况的输入,这可能就是为什么我们将Big O与最坏情况相关联的原因,而在大多数情况下,这种假设有时是正确的(例如您提到的图表),这是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 O(| V | + | E |)中邻接列表的逆 - inverse of adjacency list in O(|V | + |E|) 给定多图的邻接表,在 O(|V|+|E|) 时间内计算等效(简单)无向图的邻接表 - Given an adjacency list for multigraph, compute adjacency list for equivalent (simple) undirected graph in O(|V|+|E|) time 对于给定的图G =(V,E),您如何在O(E + V)时间中对其邻接列表表示进行排序? - For a given graph G = (V,E) how can you sort its adjacency list representation in O(E+V) time? 为什么BFS O(V + E)的复杂性代替O(V * E)? - Why is the complexity of BFS O(V+E) instead of O(V*E)? 为什么 Dikstra 算法运行在 O(V + E log V) 而不是 O(V ^ 2)? - Why Does The Dikstra Algorithm Run In O(V + E log V) Instead Of O(V ^ 2)? 为什么要进行拓扑排序以找到最短路径O(V + E) - Why is Topological Sorting to find the shortest path O(V+E) 为什么DFS和BFS的时间复杂度都是O(V+E) - Why is the time complexity of both DFS and BFS O( V + E ) 为什么DFS的时间复杂度检测无向图O(| V |)中的循环而不是O(| V | + | E |)? - Why is the time complexity of DFS to detect a cycle in an undirected graph O(|V|) and not O(|V| + |E|)? 为什么Graph adjacency不定义为邻接集,而是邻接表? - Why Graph adjacency is not defined as adjacency set, but rather adjacency list? O(|V| * k) 是否等于 O(|E|)? - Is O(|V| * k) equal to O(|E|)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM