deushiro

list

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