Advertisement
AdrianMadajewski

Untitled

Apr 23rd, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>   // for std::vector
  3. #include <stack>    // for std::stack
  4. #include <queue>    // for std::queue
  5.  
  6. class Graph {  
  7.     int V;
  8.     int E;
  9.     int** adj;
  10.  
  11. public:
  12.     Graph(int v_size, int e_size) {
  13.         V = v_size;
  14.         E = e_size;
  15.  
  16.         adj = new int*[v_size];
  17.         for (int row = 0; row < V; ++row) {
  18.             adj[row] = new int[V];
  19.             for (int col = 0; col < V; ++col) {
  20.                 adj[row][col] = 0;
  21.             }
  22.         }
  23.     }
  24.  
  25.     void addEdge(int first, int second) {
  26.         adj[first - 1][second - 1] = 1;
  27.     }
  28.  
  29.     void DFS(int start, std::vector<bool>& visited) {
  30.         std::cout << start << ' ';
  31.         visited[start] = true;
  32.  
  33.         for (int i = 0; i < V; ++i) {
  34.  
  35.             if (adj[start][i] == 1 && (!visited[i])) {
  36.                 DFS(i, visited);
  37.             }
  38.         }
  39.     }
  40.  
  41.     void print_matrix() {
  42.         for (int row = 0; row < V; ++row) {
  43.             for (int col = 0; col < V; ++col) {
  44.                 std::cout << adj[row][col] << ' ';
  45.             }
  46.             std::cout << '\n';
  47.         }
  48.     }
  49.  
  50.     void topological_sort_util_dfs(int v, std::vector<bool> &visited, std::stack<int>& stack) {
  51.         visited[v] = true;
  52.  
  53.         for (int i = 0; i < V; ++i) {
  54.             if (adj[v][i] == 1 && (!visited[i])) {
  55.                 topological_sort_util_dfs(i, visited, stack);
  56.             }
  57.         }
  58.        
  59.         // Vertex from 0 to V-size
  60.         stack.push(v + 1);
  61.     }
  62.  
  63.     void topological_sort_DFS() {
  64.         std::stack<int> stack;
  65.         std::vector<bool> visited(V, false);
  66.        
  67.         for (int i = 0; i < V; ++i) {
  68.             if (!visited[i]) {
  69.                 topological_sort_util_dfs(i, visited, stack);
  70.             }
  71.         }
  72.  
  73.         while (!stack.empty()) {
  74.             std::cout << stack.top() << ' ';
  75.             stack.pop();
  76.         }
  77.         std::cout << '\n';
  78.     }
  79.  
  80.     void topological_sort_del() {
  81.         // Create a vector to store indegrees of all vertices
  82.         std::vector<int> in_degree(V, 0);
  83.  
  84.         for (int row = 0; row < V; ++row) {
  85.             for (int col = 0; col < V; col++) {
  86.                 if (adj[row][col] == 1) {
  87.                     ++in_degree[col];
  88.                 }
  89.             }
  90.         }
  91.  
  92.         // Create an queue and enqueue all vertices with indegree 0
  93.         std::queue<int> queue;
  94.         for (int vertex = 0; vertex < V; ++vertex) {
  95.             if (in_degree[vertex] == 0) {
  96.                 queue.push(vertex);
  97.             }
  98.         }
  99.  
  100.         int count_visisted_vertices = 0;
  101.         std::vector<int> top_order;
  102.  
  103.         while (!queue.empty()) {
  104.             int vertex = queue.front();
  105.             queue.pop();
  106.             top_order.push_back(vertex);
  107.  
  108.             // Iterate through the neibhbours and decrease their in-degree
  109.             for (int j = 0; j < V; ++j) {
  110.                 if (adj[vertex][j]) {
  111.                     --in_degree[j];
  112.  
  113.                     if (in_degree[j] == 0) {
  114.                         queue.push(j);
  115.                     }
  116.                 }
  117.             }
  118.  
  119.             ++count_visisted_vertices;
  120.         }
  121.  
  122.         if (count_visisted_vertices != V) {
  123.             std::cout << "There is a cycle in the graph." << '\n';
  124.             std::cout << "Can't do topological sort." << '\n';
  125.             return;
  126.         }
  127.  
  128.         for (const auto& vertex : top_order) {
  129.             std::cout << vertex + 1 << ' ';
  130.         }
  131.         std::cout << '\n';
  132.     }
  133. };
  134.  
  135. int main()
  136. {
  137.     int v = 10;
  138.     int e = 20;
  139.  
  140.     Graph G(v, e);
  141.     /*
  142.     G.addEdge(1, 4);
  143.     G.addEdge(1, 6);
  144.     G.addEdge(2, 4);
  145.     G.addEdge(2, 5);
  146.     G.addEdge(3, 5);
  147.     G.addEdge(3, 8);
  148.     G.addEdge(4, 6);
  149.     G.addEdge(4, 7);
  150.     G.addEdge(5, 7);
  151.     G.addEdge(5, 8);
  152.     */
  153.     G.addEdge(5, 2);
  154.     G.addEdge(5, 7);
  155.     G.addEdge(1, 2);
  156.     G.addEdge(7, 8);
  157.     G.addEdge(6, 9);
  158.     G.addEdge(7, 6);
  159.     G.addEdge(7, 9);
  160.     G.addEdge(2, 3);
  161.     G.addEdge(10, 6);
  162.     G.addEdge(3, 4);
  163.     G.addEdge(10, 5);
  164.     G.addEdge(2, 7);
  165.     G.addEdge(1, 10);
  166.     G.addEdge(8, 4);
  167.     G.addEdge(8, 6);
  168.     G.addEdge(8, 9);
  169.  
  170.     G.print_matrix();
  171.    
  172.     G.topological_sort_DFS();
  173.     G.topological_sort_del();
  174.  
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement