Advertisement
smatskevich

Seminar6

Mar 20th, 2023 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. // =================================================================
  2. // Graph.hpp
  3.  
  4. #pragma once
  5.  
  6. #include <vector>
  7.  
  8. struct IGraph {
  9.   virtual ~IGraph() = default;
  10.  
  11.   // Добавление ребра от from к to.
  12.   virtual void AddEdge(size_t from, size_t to) = 0;
  13.  
  14.   virtual size_t VerticesCount() const = 0;
  15.  
  16.   virtual std::vector<int> FindAllAdjacentIn(size_t vertex) const = 0;
  17.   virtual std::vector<int> FindAllAdjacentOut(size_t vertex) const = 0;
  18. };
  19.  
  20. // =================================================================
  21. // ListGraph.hpp
  22.  
  23. #include "Graph.hpp"
  24.  
  25. #include <vector>
  26.  
  27. class ListGraph : public IGraph {
  28.  public:
  29.   explicit ListGraph(int vertices_count);
  30.   // Конструктор "копирования" по другому графу
  31.   explicit ListGraph(const IGraph* other);
  32.  
  33.   // Dtor уже тут автоматически добавлен компилятором и уже является виртуальным.
  34.   // ~ListGraph() {}
  35.   // Добавление ребра от from к to.
  36.   void AddEdge(size_t from, size_t to) override;
  37.  
  38.   size_t VerticesCount() const override;
  39.  
  40.   std::vector<int> FindAllAdjacentIn(size_t vertex) const override;
  41.   std::vector<int> FindAllAdjacentOut(size_t vertex) const override;
  42.  
  43.  private:
  44.   std::vector<std::vector<int>> in_edges_;
  45.   std::vector<std::vector<int>> out_edges_;
  46. };
  47.  
  48. // =================================================================
  49. // ListGraph.cpp
  50.  
  51. #include "ListGraph.hpp"
  52.  
  53. ListGraph::ListGraph(int vertices_count) : in_edges_(vertices_count), out_edges_(vertices_count) {
  54. }
  55.  
  56. ListGraph::ListGraph(const IGraph* other) : in_edges_(other->VerticesCount()), out_edges_(other->VerticesCount()) {
  57. //  for (int i = 0; i < other->VerticesCount(); ++i) {  // virtual call too long
  58.   for (int i = 0; i < in_edges_.size(); ++i) {
  59.     in_edges_[i] = other->FindAllAdjacentIn(i);
  60.     out_edges_[i] = other->FindAllAdjacentOut(i);
  61.   }
  62. }
  63.  
  64. void ListGraph::AddEdge(size_t from, size_t to) {
  65.   in_edges_[to].push_back(from);
  66.   out_edges_[from].push_back(to);
  67. }
  68.  
  69. size_t ListGraph::VerticesCount() const {
  70.   return in_edges_.size();
  71. }
  72.  
  73. std::vector<int> ListGraph::FindAllAdjacentIn(size_t vertex) const {
  74.   return in_edges_[vertex];
  75. }
  76. std::vector<int> ListGraph::FindAllAdjacentOut(size_t vertex) const {
  77.   return out_edges_[vertex];
  78. }
  79.  
  80. // =================================================================
  81. // MatrixGraph.hpp
  82.  
  83. #include "Graph.hpp"
  84.  
  85. class MatrixGraph : public IGraph {
  86.  public:
  87.   explicit MatrixGraph(int vertices_count);
  88.   // Конструктор "копирования" по другому графу
  89.   explicit MatrixGraph(IGraph* other);
  90.  
  91.   // Dtor уже тут автоматически добавлен компилятором и уже является виртуальным.
  92.   // ~ListGraph() {}
  93.   // Добавление ребра от from к to.
  94.   void AddEdge(size_t from, size_t to) override;
  95.  
  96.   size_t VerticesCount() const override;
  97.  
  98.   std::vector<int> FindAllAdjacentIn(size_t vertex) const override;
  99.   std::vector<int> FindAllAdjacentOut(size_t vertex) const override;
  100.  
  101.  private:
  102.   std::vector<std::vector<bool>> matrix;
  103. };
  104.  
  105. // =================================================================
  106. // MatrixGraph.hpp
  107.  
  108. //#include "Main.hpp"
  109.  
  110. #include <iostream>
  111. #include <vector>
  112.  
  113. #include "ListGraph.hpp"
  114.  
  115. class Furniture {
  116.  public:
  117. //  Furniture() { std::cout << "Furniture ctor. " << Caller() << std::endl; }
  118.   virtual ~Furniture() = default;
  119.   virtual int Calories() const = 0;
  120.   virtual int Caller() const { return Calories(); }
  121. };
  122.  
  123. class Sofa : public Furniture {
  124.  public:
  125.   int Calories() const override { return 100000; }
  126.   std::pair<int, int> Coord;
  127.   std::vector<int> Uses = {2, 4, 5};
  128. };
  129.  
  130. class IronChair : public Furniture {
  131.  public:
  132.   int Calories() const override { return 0; }
  133. };
  134.  
  135. void Burn(Furniture& f) {
  136.   std::cout << "OO, furniture is in fire, heats on " << f.Calories() << std::endl;
  137. }
  138.  
  139. void Shift(Sofa& s, int x, int y) {
  140.   s.Coord = {s.Coord.first + x, s.Coord.second + y};
  141. }
  142.  
  143. int main1() {
  144.   Sofa s;
  145.   Furniture& f = s;
  146.   Burn(f);
  147.   Shift(dynamic_cast<Sofa&>(f), 3, 8);
  148.  
  149.   IronChair chair;
  150.   Furniture& f2 = chair;
  151.   Burn(f2);
  152.   Sofa& s2 = dynamic_cast<Sofa&>(f2);
  153.   std::cout << "s2 = " << &s2 << std::endl;
  154.   Shift(s2, 3, 8);
  155.   return 0;
  156. }
  157.  
  158. int main2() {
  159.   Sofa s;
  160.   return 0;
  161. }
  162.  
  163. Sofa* CreateSofa() {
  164.   return new Sofa();
  165. }
  166.  
  167. IronChair* CreateIronChair() {
  168.   return new IronChair();
  169. }
  170.  
  171. int main3() {
  172.   std::vector<Furniture*> furnitures;
  173.   furnitures.push_back(CreateSofa());
  174.   furnitures.push_back(CreateIronChair());
  175.  
  176.   for (Furniture* f : furnitures) {
  177.     Burn(*f);
  178.   }
  179.   for (Furniture* f : furnitures) {
  180.     delete f;
  181.   }
  182.   return 0;
  183. }
  184.  
  185. int Time = 0;
  186.  
  187. void DFS(size_t u, const IGraph* graph, std::vector<bool>& visited) {
  188.   visited[u] = true;
  189.   std::cout << "Entry " << u << ", time = " << Time++ << std::endl;
  190.   for (size_t v : graph->FindAllAdjacentOut(u)) {
  191.     if (!visited[v]) DFS(v, graph, visited);
  192.   }
  193.   std::cout << "Leave " << u << ", time = " << Time++ << std::endl;
  194. }
  195.  
  196. void MainDFS(const IGraph* graph) {
  197.   std::vector<bool> visited(graph->VerticesCount()/*, false*/);
  198.   for (size_t v = 0; v < graph->VerticesCount(); ++v) {
  199.     if (!visited[v]) DFS(v, graph, visited);
  200.   }
  201. }
  202.  
  203. int main() {
  204.   ListGraph lg(9);
  205.   lg.AddEdge(6, 0);
  206.   lg.AddEdge(3, 2);
  207.   lg.AddEdge(2, 1);
  208.   lg.AddEdge(2, 5);
  209.   lg.AddEdge(1, 5);
  210.   lg.AddEdge(3, 4);
  211.   lg.AddEdge(4, 7);
  212.   lg.AddEdge(7, 8);
  213.   lg.AddEdge(8, 4);
  214.   lg.AddEdge(5, 7);
  215.  
  216.   for (int in : lg.FindAllAdjacentIn(5)) std::cout << in << " ";
  217.   std::cout << std::endl;
  218.   for (int out : lg.FindAllAdjacentOut(5)) std::cout << out << " ";
  219.   std::cout << std::endl;
  220.  
  221.   ListGraph lg2(&lg);
  222.   MainDFS(&lg2);
  223.   return 0;
  224. }
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement