我正在通过这个链接查看邻接表表示法。
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
,都会执行一个循环,假设每个顶点的度数为 d
,则循环被执行 d
次。
因此,我认为时间复杂度类似于
d0 + d1 + d2 + d3 ....... +dV where di is the degree of each vertex.
这一切总结为O(E),但链接中表明时间复杂度为O(|V|+|E|)
不确定理解上有什么问题。需要一些帮助。