Queue Q = CreateQueue(graph->nv); for (v = 0; v < graph->nv; ++v) // 初始化indegree[] indegree[v] = 0; for (v = 0; v < graph->nv; ++v) // 遍历图 得到indegree[] for (w = graph->g[v].firstEdge; w; w = w->next) indegree[w->adjv]++; for (v = 0; v < graph->nv; ++v) if (indegree[v] == 0) // 将入度为0的顶点入队 AddQ(Q, v);
cnt = 0; while (!IsEmpty(Q)){ v = Delete(Q); // 弹出一个入度为0的顶点 topOrder[cnt++] = v; for (w = graph->g[v].firstEdge; w; w = w->next) if (--indegree[w->adjv] == 0) // 将删除v使得w->adjv入度为0 AddQ(Q, w->adjv); }