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

后端 未结 3 731

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 01:49

    it does, i've been using it and the same question came to my mind.

    I actually haven't had time to demonstrate it but every test case (lot of thousands) always returns an topological sorted condensed graph.

    0 讨论(0)
  • 2021-02-06 01:55

    Yes, it does. Tarjan's SCC algorithm works by performing a DFS on a graph and keeping track of the roots of the SCCs on a stack. One method of finding a topological sort is performing a DFS on a graph and keeping track of the exit order. The exit order of these nodes in Tarjan's SCC algorithm provide a topological sort.

    Donald Knuth even mentions it in an interview when talking about Tarjan's SCC algorithm, which he says is one of his favorite:

    The data structures that he devised for this problem fit together in an amazingly beautiful way, so that the quantities you need to look at while exploring a directed graph are always magically at your fingertips. And his algorithm also does topological sorting as a byproduct.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题