Advertisement
ssr17

Topological Sort using DFS

Feb 7th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int v,t;
  7. int matrix[100][100];
  8. int clr[100], dis[100], fin[100], prev[100];
  9. int t_edge,b_edge,f_edge,c_edge;
  10.  
  11. list <int> tp_sort;
  12. list <int> :: iterator it;
  13.  
  14. void DFS();
  15. void DFS_visit(int s);
  16.  
  17. int main ()
  18. {
  19.     int  r, c, n, s;
  20.     int edge = 1;
  21.  
  22.     cout<<"Input no of vertices: ";
  23.     cin>>v;
  24.  
  25.     do{
  26.         cout<<"Edge "<<edge<<": ";
  27.         cin>>r>>c;
  28.          if(r>v || c>v)
  29.             cout<<"Invalid input"<<endl;
  30.         else{
  31.             matrix[r][c] = 1;
  32.             edge++;
  33.         }
  34.     }while(r != 0 && c!=0);
  35.  
  36.     DFS();
  37.  
  38.     if(b_edge > 0)
  39.         cout<<endl<<"Topological sort can't be determined!!!(Cyclic Graph)"<<endl;
  40.     else{
  41.         cout<<endl<<endl<<"Topological sort : ";
  42.         for(it = tp_sort.begin(); it != tp_sort.end(); it++)
  43.             cout<< *it << " ";
  44.     }
  45.  
  46.     cout<<endl;
  47.  
  48.     return 0;
  49. }
  50.  
  51. void DFS ()
  52. {
  53.     int i;
  54.     t =0;
  55.     t_edge = 0;
  56.     b_edge = 0;
  57.     f_edge = 0;
  58.     c_edge = 0;
  59.  
  60.     for(i=0; i<=v; i++)
  61.     {
  62.         clr[i] = 0;
  63.         dis[i] = 2000000;
  64.         fin[i] = 2000000;
  65.         prev[i] = -1;
  66.     }
  67.  
  68.     for(i=1; i<=v; i++)
  69.     {
  70.         if(clr[i] == 0)
  71.             DFS_visit(i);
  72.     }
  73.  
  74.     //printing
  75.     cout<<endl<<"vertex : ";
  76.     for(i=1; i<=v; i++)
  77.         cout<<i<<"   ";
  78.  
  79.     cout<<endl<<"d[u]   : ";
  80.     for(i=1; i<=v; i++)
  81.         cout<<dis[i]<<"   ";
  82.  
  83.     cout<<endl<<"f[u]   : ";
  84.     for(i=1; i<=v; i++)
  85.         cout<<fin[i]<<"  ";
  86.  
  87.     //edges
  88.     cout<<endl<<"No of tree edge: "<< t_edge;
  89.     cout<<endl<<"No of back edge: "<< b_edge;
  90.     cout<<endl<<"No of forward edge: "<< f_edge;
  91.     cout<<endl<<"No of cross edge: "<< c_edge;
  92. }
  93.  
  94. void DFS_visit(int s)
  95. {
  96.     int i;
  97.  
  98.     clr[s] = 1;
  99.     dis[s] = ++t;
  100.  
  101.     for(i=1; i<=v; i++)
  102.     {
  103.         if(matrix[s][i] == 1 && clr[i] == 2)
  104.         {
  105.             if (dis[s] < dis[i]){
  106.                     f_edge++;
  107.                 cout<<s<<"-"<<i<<" : Forward Edge"<<endl;
  108.             }
  109.             else{
  110.                     c_edge++;
  111.                 cout<<s<<"-"<<i<<" : Cross Edge"<<endl;
  112.             }
  113.         }
  114.         else if(matrix[s][i] == 1 && clr[i] == 1){
  115.                 b_edge++;
  116.             cout<<s<<"-"<<i<<" : Back Edge"<<endl;
  117.         }
  118.         else
  119.         {
  120.             if(matrix[s][i] == 1 && clr[i] == 0)
  121.             {
  122.                 prev[i] = s;
  123.                 cout<<s<<"-"<<i<<" : Tree Edge"<<endl;
  124.                 t_edge++;
  125.                 DFS_visit(i);
  126.             }
  127.         }
  128.     }
  129.     clr[s] = 2;
  130.     fin[s] = ++t;
  131.     tp_sort.push_front(s);
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement