09 January 2017
原文: ACM 之家 -- 《拓扑排序-图论》

dfs

visited = []      // vector  顶点访问
result  = []      // list    结果集

for each edge(u, v) in G
    if visited[u] == 0
        dfs(u)

dfs(x)
    if visited[x] == 1
        return

    visited[x] = 1

    for each y in adj(x)    // x --> y
        dfs(y)

    result.push(x)

bfs

degree = []      // vector   顶点入度
zero   = []      // queue    入度数量为 0 的顶点
result = []      // list     结果集

for each edge(u, v) in G        // u --> v
    degree[v]++

for each node(v) in G
    if degree[v] == 0
        zero.enqueue(v)

while zero.empty() != true
    curNode = zero.dequeue()

    for each v in adj(curNode)
        degree[v]--
        if degree[v] == 0
            zero.enqueue(v)

    result.push(curNode)