Advertisement
shakhawatt

DFS

Sep 24th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. // data structure to store graph edges
  8. struct Edge {
  9.     char src, dest;
  10. };
  11.  
  12.  
  13.  
  14.  
  15. // class to represent a graph object
  16. class Graph
  17. {
  18. public:
  19.     // construct a vector of vectors to represent an adjacency list
  20.     vector<vector<int>> adjList;
  21.  
  22.     // Graph Constructor
  23.     Graph(vector<Edge> const &edges, int N)
  24.     {
  25.         // resize the vector to N elements of type vector<int>
  26.         adjList.resize(N);
  27.  
  28.         // add edges to the directed graph
  29.         for (auto &edge: edges)
  30.             adjList[edge.src].push_back(edge.dest);
  31.     }
  32. };
  33.  
  34.  
  35.  
  36.  
  37.  
  38. // Function to perform DFS Traversal
  39. int DFS(Graph const &graph, int v, vector<bool> &discovered,
  40.     vector<int> &arrival, vector<int> &departure, int &time)
  41. {
  42.     // set arrival time of vertex v
  43.     arrival[v] = ++time;
  44.  
  45.     // mark vertex as discovered
  46.     discovered[v] = true;
  47.  
  48.     for (int i : graph.adjList[v])
  49.     if (!discovered[i])
  50.         DFS(graph, i, discovered, arrival, departure, time);
  51.  
  52.     // set departure time of vertex v
  53.     departure[v] = ++time;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. int main()
  60. {
  61.  
  62. #ifndef ONLINE_JUDGE
  63.     freopen("input.txt", "r", stdin);
  64.     freopen("output.txt", "w", stdout);
  65. #endif
  66.  
  67.  
  68.     // vector of graph edges as per above diagram
  69.     vector<Edge> edges = {
  70.         {a, b}, {a, h},
  71.         {b, a}, {b, h}, {b, c},
  72.         {c, b}, {c, i}, {c, f}, {c, d},
  73.         {d, c}, {d, f}, {d, e},
  74.         {e, d}, {e, f},
  75.         {f, e}, {f, d}, {f, c}, {f, g},
  76.         {g, f}, {g, i}, {g, h},
  77.         {h, i}, {h, g}, {h, b}, {h, a}
  78.        
  79.     };
  80.  
  81.  
  82.     // Number of nodes in the graph
  83.     int N = 8;
  84.  
  85.  
  86.     // create a graph from given edges
  87.     Graph graph(edges, N);
  88.  
  89.  
  90.     // vector to store arrival time of vertex
  91.     vector<int> arrival(N);
  92.  
  93.  
  94.     // vector to store departure time of vertex
  95.     vector<int> departure(N);
  96.  
  97.  
  98.     // Mark all the vertices as not discovered
  99.     vector<bool> discovered(N);
  100.  
  101.     int time = -1;
  102.  
  103.     // Do DFS traversal from all undiscovered nodes to
  104.     // cover all unconnected components of graph
  105.  
  106.     for (int i = 0; i < N; i++)
  107.     if (!discovered[i])
  108.         DFS(graph, i, discovered, arrival, departure, time);
  109.  
  110.     // print arrival and departure time of each
  111.     // vertex in DFS
  112.  
  113.     for (int i = 0; i < N; i++) {
  114.         cout << "Vertex " << i << " (" << arrival[i]
  115.              << ", " << departure[i] << ")" << endl;
  116.     }
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement