Does Tarjan's SCC algorithm give a topological sort of the SCC?

后端 未结 3 732

I\'ve been studying SCC and algorithms about them, and I\'ve seen that people almost always mention that Kosaraju\'s algorithm finds the SCC and also gives them ordered in a (re

3条回答
  •  野的像风
    2021-02-06 02:04

    Yes. As Tarjan is based on Depth First Search all you have to do is to add the vertexes to the top of a list when each vertex reach the "closed" state.(ie. its dfs/tarjan call ends for that vertex)

    Here is a C/C++/Pseudo-Code example:

    void tarjan(int u) {
        visited[u] = true;
        closed[u] = false;
        //dfs + tarjan processing
        for (vi v = G[u].begin(); v != G[u].end(); v++) {
            if (!visited[*v]) {
                dfs(*v);
            } else if (!closed[*v]) {
                // has Cycle
            }
        }
        closed[u] = true;
        //add u to the top of your list here
    }
    

提交回复
热议问题