Advertisement
Oppenheimer

DFS (with in out time)

Aug 19th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. // O(E + V )
  2.  
  3. unordered_map<int,vector<int>> adj; // graph represented as an adjacency list
  4. int n; // number of vertices
  5.  
  6. vector<int> color;
  7.  
  8. vector<int> time_in, time_out;
  9. int dfs_timer = 0;
  10.  
  11. void dfs(int v) {
  12.     time_in[v] = dfs_timer++;
  13.     color[v] = 1;
  14.     for (int u : adj[v])
  15.         if (color[u] == 0)
  16.             dfs(u);
  17.     color[v] = 2;
  18.     time_out[v] = dfs_timer++;
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement