深度优先搜索有一种经典的应用:把一个有向图分解为各强连通分支。很多有关有向图的算法都是从这种步骤开始的。(算法导论P338,觉得简洁而精妙,分享下)
STRONGLY-CONNECTED-COMPONENTS(G)
1 call DFS(G) to compute finishing times f[u] for each vertex u
2 compute GT
3 call DFS(GT). but in the main loop of DFS, consider the vertices in order of decreasing f[u](as computed in line 1)
4 output the vertices of each tree in the depth-first forest formed in line3 as a separate strongly connected component
DFS(G ):深度遍历,GT为图G的转置,f[u]为第二时间戳,深度遍历时当顶点u第一次被发现时,记录下第一时间戳d[u],当检查u的邻接表结束时,记录第二时