Advertisement
smatskevich

Seminar7

Mar 27th, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. // ListGraph как в прошлом семинаре.
  2.  
  3. #include <iostream>
  4. #include <queue>
  5. #include <memory>
  6. #include <unordered_set>
  7. #include <vector>
  8.  
  9. #include "ListGraph.hpp"
  10.  
  11. void BFS(const IGraph& graph, int start) {
  12.   std::vector<bool> visited(graph.VerticesCount());
  13.   std::queue<int> to_go;
  14.   to_go.push(start);
  15.   to_go.push(-1);
  16.   visited[start] = true;
  17. //  int last_in_layer = start;
  18.   while (!to_go.empty()) {
  19.     int u = to_go.front(); to_go.pop();
  20.     if (u == -1) {
  21.       std::cout << "\n";
  22.       if (!to_go.empty()) to_go.push(-1);
  23.       continue;
  24.     }
  25.     std::cout << u << " ";
  26.     for (int v : graph.FindAllAdjacentOut(u)) {
  27.       if (!visited[v]) {
  28.         to_go.push(v);
  29.         visited[v] = true;
  30.       }
  31.     }
  32. //    if (last_in_layer == u) {
  33. //      if (!to_go.empty()) last_in_layer = to_go.back();
  34. //      std::cout << "\n";
  35. //    }
  36.   }
  37. }
  38.  
  39. int main1() {
  40.   ListGraph lg(10);
  41.   lg.AddEdge(6, 0);
  42.   lg.AddEdge(3, 2);
  43.   lg.AddEdge(2, 1);
  44.   lg.AddEdge(2, 5);
  45.   lg.AddEdge(1, 5);
  46.   lg.AddEdge(3, 4);
  47.   lg.AddEdge(4, 7);
  48.   lg.AddEdge(7, 8);
  49.   lg.AddEdge(8, 4);
  50.   lg.AddEdge(5, 7);
  51.   lg.AddEdge(1, 9);
  52.   lg.AddEdge(9, 3);
  53.  
  54.   lg.AddEdge(0, 6);
  55.   lg.AddEdge(2, 3);
  56.   lg.AddEdge(1, 2);
  57.   lg.AddEdge(5, 2);
  58.   lg.AddEdge(5, 1);
  59.   lg.AddEdge(4, 3);
  60.   lg.AddEdge(7, 4);
  61.   lg.AddEdge(8, 7);
  62.   lg.AddEdge(4, 8);
  63.   lg.AddEdge(7, 5);
  64.   lg.AddEdge(9, 1);
  65.   lg.AddEdge(3, 9);
  66.  
  67.   BFS(lg, 3);
  68.  
  69.   return 0;
  70. }
  71.  
  72. void DFS(size_t u, const IGraph& graph, std::vector<int8_t>& colors, std::vector<size_t>& parents) {
  73.   colors[u] = 1;  // Grey
  74.   for (size_t v : graph.FindAllAdjacentOut(u)) {
  75.     std::cout << u << " " << v << std::endl;
  76.     if (colors[v] == 0) {
  77.       parents[v] = u;
  78.       DFS(v, graph, colors, parents);
  79.     }
  80.     if (colors[v] == 1) {  // Нашли цикл
  81.       std::cout << "Success!\n";
  82.       std::cout << v << " ";
  83.       for (size_t w = u; w != v; w = parents[w]) {
  84.         std::cout << w << " ";
  85.       }
  86.       std::cout << "\n";
  87.     }
  88.   }
  89.   colors[u] = 2;  // Black
  90. }
  91.  
  92. void MainDFS(const IGraph& graph) {
  93.   std::vector<int8_t> colors(graph.VerticesCount()/*, false*/);
  94.   std::vector<size_t> parents(graph.VerticesCount(), -1);
  95.   for (size_t v = 0; v < graph.VerticesCount(); ++v) {
  96.     if (colors[v] == 0) DFS(v, graph, colors, parents);
  97.   }
  98. }
  99.  
  100. int main2() {
  101.   ListGraph lg(7);
  102.   lg.AddEdge(0, 1);
  103.   lg.AddEdge(1, 2);
  104.   lg.AddEdge(2, 3);
  105.   lg.AddEdge(6, 2);
  106.   lg.AddEdge(6, 5);
  107.   lg.AddEdge(6, 4);
  108.   lg.AddEdge(3, 6);
  109.   lg.AddEdge(4, 2);
  110.   lg.AddEdge(5, 1);
  111.  
  112.   MainDFS(lg);
  113.  
  114.   return 0;
  115. }
  116.  
  117. bool BFSForDualCheck(const IGraph& graph, size_t start, std::unordered_set<size_t>& left,
  118.                      std::unordered_set<size_t>& right) {
  119.   std::queue<size_t> to_go;
  120.   to_go.push(start);
  121.   while (!to_go.empty()) {
  122.     size_t u = to_go.front(); to_go.pop();
  123.     bool is_left = left.contains(u);
  124.     for (size_t v : graph.FindAllAdjacentOut(u)) {
  125.       if (left.contains(v)) {
  126.         if (is_left) return false;
  127.       } else if (right.contains(v)) {
  128.         if (!is_left) return false;
  129.       } else {
  130.         to_go.push(v);
  131.         (is_left ? right : left).insert(v);
  132.       }
  133.     }
  134.   }
  135.   return true;
  136. }
  137.  
  138. bool IsDual(const IGraph& graph) {
  139.   std::unordered_set<size_t> left, right;
  140.   for (size_t i = 0; i < graph.VerticesCount(); ++i) {
  141.     if (!left.contains(i) && !right.contains(i)) {
  142.       left.insert(i);
  143.       if (!BFSForDualCheck(graph, i, left, right)) return false;
  144.     }
  145.   }
  146.   return true;
  147. }
  148.  
  149. int main3() {
  150.   ListGraph lg(7);
  151.   lg.AddEdge(0, 1);
  152.   lg.AddEdge(1, 2);
  153.   lg.AddEdge(2, 3);
  154.   lg.AddEdge(6, 2);
  155.   lg.AddEdge(6, 5);
  156.   lg.AddEdge(6, 4);
  157.   lg.AddEdge(3, 6);
  158.   lg.AddEdge(4, 2);
  159.   lg.AddEdge(5, 1);
  160.  
  161.   std::cout << IsDual(lg);
  162.  
  163.   return 0;
  164. }
  165.  
  166. void DFSReverseRecursive(size_t u, const IGraph& graph,
  167.                          std::vector<bool>& visited, std::vector<size_t>& to_go) {
  168.   visited[u] = true;
  169.   for (size_t v : graph.FindAllAdjacentIn(u)) {
  170.     if (!visited[v]) DFSReverseRecursive(v, graph, visited, to_go);
  171.   }
  172.   to_go.push_back(u);
  173. }
  174.  
  175. std::vector<size_t> DFSReverse(const IGraph& graph) {
  176.   std::vector<size_t> to_go;
  177.   std::vector<bool> visited(graph.VerticesCount());
  178.   for (size_t s = 0; s < graph.VerticesCount(); ++s) {
  179.     if (!visited[s]) DFSReverseRecursive(s, graph, visited, to_go);
  180.   }
  181.   return to_go;
  182. }
  183.  
  184. std::unique_ptr<IGraph> Condense(const IGraph& graph) {
  185.   std::vector<size_t> to_go = DFSReverse(graph);
  186. //  for (int i = to_go.size() - 1; i >= 0; --i) {
  187. //    if (!visited[to_go[i]]) {
  188. //      ++current;
  189. //      DFSCondense(to_go[i], graph, visited, component, current);
  190. //    }
  191. //  }
  192.  
  193.   std::unique_ptr<IGraph> condense = std::make_unique<ListGraph>(4);
  194.  
  195.   return condense;
  196. }
  197.  
  198. int main() {
  199.   ListGraph lg(10);
  200.   lg.AddEdge(6, 0);
  201.   lg.AddEdge(3, 2);
  202.   lg.AddEdge(2, 1);
  203.   lg.AddEdge(2, 5);
  204.   lg.AddEdge(1, 5);
  205.   lg.AddEdge(3, 4);
  206.   lg.AddEdge(4, 7);
  207.   lg.AddEdge(7, 8);
  208.   lg.AddEdge(8, 4);
  209.   lg.AddEdge(5, 7);
  210.   lg.AddEdge(1, 9);
  211.   lg.AddEdge(9, 3);
  212.  
  213.   auto condense = Condense(lg);
  214.   return 0;
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement