【发布时间】:2017-07-08 06:56:18
【问题描述】:
我正在通过此链接进行邻接列表表示。
http://www.geeksforgeeks.org/graph-and-its-representations/
我对代码的某些部分有一个简单的疑问,如下所示:
// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
因为,这里对于每个V while 循环执行说d 次,其中 d 是每个顶点的度数。
所以,我认为时间复杂度就像
d0 + d1 + d2 + d3 ....... +dV where di is the degree of each vertex.
所有这些加起来 O(E),但是链接说时间复杂度是 O(|V|+|E|)
不知道理解有什么问题。这里需要一些帮助
【问题讨论】:
-
假设图没有边。算法需要多长时间?
-
@user2357112 我们必须检查或扫描每个顶点一次,对吗?但它是一个嵌套循环,所以时间复杂度不应该是这样吗?
标签: algorithm time-complexity breadth-first-search adjacency-list