Graph g ← グラフの生成

depthFirstSearch():
    for v ← 0 to g.N-1:
        color[v] ← WHITE

    for v ← 0 to g.N-1:
        if color[v] = WHITE:
            dfs(v)

dfs(u):
    color[u] ← GRAY

    for v in g.adjLists[u]:
        if color[v] = WHITE:
            dfs(v)
        else:
            エッジ(u, v)はバッグエッジ   # 検出

    color[u] ← BLACK