Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLRS Topological sort algorithm reverse of DFS?

Tags:

c++

graph

I have one confusion looking at the step for topological sort i see that the reverse order of DFS is a prospective solution for Topological sort.

I also tried a small code

void graph::dfs(void)
{
    for(std::vector<vertex>::iterator iter =vertexes.begin();iter < vertexes.end();iter++ ){
        iter->visited = WHITE;  
    }   

    for(std::vector<vertex>::iterator iter =vertexes.begin();iter < vertexes.end();iter++ ){
        if(iter->visited == WHITE){
            dfs_visits(*iter);
        }
    }

    std::cout << "-----------------dfs------------------"<<std::endl;
    for(std::list<int>::reverse_iterator riter = q.rbegin() ; riter != q.rend();riter++)
        std::cout << *riter << std::endl;

    std::cout << "-----------------topological_sort------------------"<<std::endl;
    for(std::list<int>::iterator iter = q.begin() ; iter != q.end();iter++)
        std::cout << *iter << std::endl;


    q.clear();
}


void graph::dfs_visits(vertex& source){
    source.visited = GREY;
    for(std::list<edge>::iterator iter = source.list.begin();iter != source.list.end();iter++){
        if(vertexes[iter->destination_vertex].visited == WHITE){
            dfs_visits(vertexes[iter->destination_vertex]);
        }
    }
    source.visited = BLACK;
    q.push_front(source.id);
}

The graph data structure is here

#include "iostream"
#include "vector"
#include "list"

enum color{
    WHITE,
    GREY,
    BLACK
};

struct edge{
    int destination_vertex;
    edge(int ver){
        destination_vertex = ver;
    }
};

struct vertex{
    int id;
    color visited;
    std::list<edge>  list;
    vertex(int _id){
        id = _id;
    }
};


class graph
{
private:
    std::vector<vertex> vertexes;
    int next;
    std::list<int> q;
public:
    graph(void){
        next = 0;
    }
    ~graph(void){}
    void add_node(std::vector<int> edges );
    void add_node(std::vector<int> incoming_edges , std::vector<int> outgoing_edges);
    void print();
    void dfs();
    void dfs_visits(vertex& source);
    void bfs();
    static void process();
};

Here is one e.g. graph i tried

0->1,2,
1->3,
2->
3->
4->
5->4,
-----------------dfs------------------
3
1
2
0
4
5
-----------------topological_sort-----
5
4
0
2
1
3
Changing the question statement

My question is really simple.. Is topological sort is always DFS in reverse order? If not is there a counter example?

If you see my output for the particular graph the DFS output and its reverse is a correct solution for topological sort of the graph too....also reading the CLR topological sort alorithm it also looks like topological sort is the reverse of DFS?

like image 919
sethi Avatar asked Dec 09 '25 23:12

sethi


1 Answers

Yes it's always true. The reverse of a DFS on a DAG gives the topological sort.

Source: http://www.cse.ust.hk/faculty/golin/COMP271Sp03/Notes/MyL08.pdf slide 13

like image 191
eikiro-000 Avatar answered Dec 12 '25 12:12

eikiro-000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!