SHARE
TWEET

TopologicalSort

arsovski Jan 21st, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void _recursive_topological(int from)
  2. {
  3.     visited_topo[from] = true;
  4.  
  5.     //go through neighbors
  6.     for(auto element: graph[from])
  7.     {
  8.         if(!visited_topo[element.vertex_number])
  9.         {
  10.             visited_topo[element.vertex_number] = true;
  11.             _recursive_topological(element.vertex_number);
  12.         }
  13.     }
  14.  
  15.  
  16.     ordered_nodes.push_back(from);
  17.  
  18.  
  19. }
  20.  
  21. void topological_sort()
  22. {
  23.     for(int i=0; i < graph.size();i++)
  24.     {
  25.         if(!visited_topo[i])
  26.             _recursive_topological(i);
  27.     }
  28.  
  29.     ordered_nodes.reverse();
  30.  
  31.     for(auto element : ordered_nodes)
  32.     {
  33.         std::cout << element << " ";
  34.     }
  35.  
  36.     std::cout << std::endl;
  37.  
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top