deushiro

Untitled

Mar 26th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1.  
  2. #ifndef HOMEWORK_1_MATRIXGRAPH_H
  3. #define HOMEWORK_1_MATRIXGRAPH_H
  4.  
  5.  
  6. #include "../graph.h"
  7.  
  8. template<typename T = void>
  9. class MatrixGraph : public IGraph<T> {
  10. public:
  11.  
  12.     MatrixGraph(unsigned int vertices_count){
  13.         graph.resize(vertices_count);
  14.         for(int i = 0; i < graph.size(); ++i){
  15.             graph[i].resize(vertices_count);
  16.         }
  17.     }
  18.  
  19.     virtual void AddEdge(int from, int to, T &&element) {
  20.         int maxi = std::max(from, to) + 1;
  21.         if(graph.size() <= maxi){
  22.             graph.resize(maxi);
  23.             for(int i = 0; i < graph.size(); ++i){
  24.                 graph[i].resize(maxi);
  25.             }
  26.         }
  27.         graph[from][to] = new T(element);
  28.  
  29.     };
  30.  
  31.     virtual void GetEdges(std::vector<std::tuple<int, int, T>>& edges) const{
  32.         for(size_t i = 0; i < VerticesCount(); ++i){
  33.             for(size_t j = 0; j < VerticesCount(); ++j){
  34.                 if(graph[i][j] != nullptr){
  35.                     edges.push_back({i,j, *graph[i][j]});
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.     MatrixGraph(){
  42.         graph = std::vector<std::vector<T*>>();
  43.     }
  44.  
  45.     MatrixGraph(IGraph<T> *_oth) {
  46.         std::vector<std::tuple<int, int, T>> edges;
  47.         _oth->GetEdges(edges);
  48.         int maxi = 0;
  49.         for(std::tuple<int, int, T> edge: edges){
  50.             maxi = std::max(get<0>(edge), get<1>(edge));
  51.         }
  52.         if(graph.size() <= maxi){
  53.             graph.resize(maxi);
  54.             for(int i = 0; i < graph.size(); ++i){
  55.                 graph[i].resize(maxi);
  56.             }
  57.         }
  58.         for(std::tuple<int, int, T> edge : edges){
  59.             graph[std::get<0>(edge)][std::get<1>(edge)]= new T(std::get<2>(edge));
  60.         }
  61.     };
  62.  
  63.     virtual int VerticesCount() const { return graph.size(); };
  64.  
  65.     virtual void GetNextVertices(int vertex, std::vector<int> &vertices) const {
  66.         for(size_t i = 0; i < VerticesCount(); ++i){
  67.             if(graph[vertex][i] != nullptr) {
  68.                 vertices.push_back(i);
  69.             }
  70.         }
  71.     };
  72.  
  73.     virtual void GetPrevVertices(int vertex, std::vector<int> &vertices) const {
  74.         for(size_t i = 0; i < VerticesCount(); ++i){
  75.             if(graph[i][vertex] != nullptr) {
  76.                 vertices.push_back(i);
  77.             }
  78.         }
  79.     };
  80.  
  81.     virtual void DeepFirstSearch(int vertex, std::vector<int> &vertices) const {
  82.         std::vector<bool> used(graph.size(), false);
  83.         _dfs(vertex, vertices, used);
  84.     };
  85.  
  86.     virtual void BreadthFirstSearch(int vertex, std::vector<int> &vertices) const {
  87.         std::queue<int> order;
  88.         std::vector<bool> used(VerticesCount(), false);
  89.         used[vertex] = true;
  90.         order.push(vertex);
  91.         int current = 0;
  92.         while(!order.empty()){
  93.             current = order.front();
  94.             order.pop();
  95.             for(size_t i = 0; i < VerticesCount(); ++i){
  96.                 if(!used[i] && graph[current][i] != nullptr){
  97.                     vertices.push_back(i);
  98.                     order.push(i);
  99.                 }
  100.             }
  101.         }
  102.     };
  103.  
  104.  
  105. protected:
  106.     std::vector<std::vector<T*>> graph;
  107.  
  108.     void _dfs(int vertex, std::vector<int> &vertices, std::vector<bool> &used) const {
  109.         used[vertex] = true;
  110.         vertices.push_back(vertex);
  111.         std::cout << vertex << std::endl;
  112.         for(int i = 0; i < VerticesCount(); ++i){
  113.             if(!used[i] && graph[vertex][i] != nullptr){
  114.                 _dfs(i, vertices, used);
  115.             }
  116.         }
  117.  
  118.     }
  119.  
  120. };
  121.  
  122. #endif HOMEWORK_1_MATRIXGRAPH_H
  123.  
Add Comment
Please, Sign In to add comment