Gar*_*ick 5 algorithm breadth-first-search time-complexity adjacency-list
我正在通过这个链接进行邻接列表表示.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
因为,这里执行每个V
while循环,比如说d
d是每个顶点的度数.
所以,我认为时间的复杂性就像
d0 + d1 + d2 + d3 ....... +dV where di is the degree of each vertex.
Run Code Online (Sandbox Code Playgroud)
所有这些总结了O(E),但链接说时间复杂度为O(| V | + | E |)
不确定理解的问题是什么.这里需要一些帮助
这里重要的是,为了使时间复杂性有效,我们需要涵盖所有可能的情况:
如前所述,| E | 可能相当小,这样我们仍然需要考虑外循环中完成的工作.因此,我们不能简单地删除| V | 术语.