Advertisement
dimon-torchila

Untitled

Jan 5th, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #include "boost/bimap.hpp"
  3.  
  4. using namespace std;
  5.  
  6. namespace lab2{
  7.     class Graph{
  8.     public:
  9.         vector<bool> visited;
  10.         Graph(int size) : visited(size, false){
  11.             for(int i = 0; i < size; ++i){
  12.                 matrix.push_back(vector<int>(size,0));
  13.                 string s;
  14.                 cout << "Enter name v : ";
  15.                 cin >> s;
  16.                 ItN[i] = s;
  17.                 NtI[s] = i;
  18.             }
  19.  
  20.         }
  21.         auto First(string v){
  22.             if(!matrix[NtI[v]].empty()){
  23.                 if(NtI[v] >= matrix.size())
  24.                     throw runtime_error("going beyond the boundaries of the matrix");
  25.                 else{
  26.                     for(int i = 0; i < matrix[NtI[v]].size(); ++i){
  27.                         if(matrix[NtI[v]][i] != 0)
  28.                             return i;
  29.                     }
  30.                     return -1;
  31.                 }
  32.             } else
  33.                 throw runtime_error("height does not exist");
  34.         }
  35.  
  36.         auto Next(string v, int i){
  37.             if(!matrix[NtI[v]].empty()){
  38.                 if(i == matrix[NtI[v]].size() - 1){
  39.                     return -1;
  40.                 }
  41.                 for(int x = i + 1; i < matrix[NtI[v]].size(); ++i){
  42.                     if(matrix[NtI[v]][x] != 0)
  43.                         return x;
  44.                 }
  45.             }
  46.             else
  47.                 throw runtime_error("height does not exist");
  48.         }
  49.  
  50.         auto Vertex(string v, int i){
  51.             if(!matrix[NtI[v]].empty()){
  52.                 if(i == 0)
  53.                     return First(v);
  54.                 int c = First(v);
  55.                 while(i != 0){
  56.                     c = Next(v, c);
  57.                     --i;
  58.                 }
  59.                 return c;
  60.             }
  61.                 else throw runtime_error("height does not exist");
  62.  
  63.         }
  64.  
  65.         auto Add_V(string v){
  66.             NtI[v] = matrix.size();
  67.             ItN[matrix.size()] = v;
  68.             matrix.push_back(vector<int>(matrix[0].size(), 0));
  69.             for(auto& x : matrix){
  70.                 if(!x.empty())
  71.                     x.push_back(0);
  72.             }
  73.         }
  74.  
  75.         auto Add_E(string v, string w, int c){
  76.             matrix[NtI[v]][NtI[w]] = c;
  77.         }
  78.  
  79.         auto Del_V(string v){
  80.             matrix[NtI[v]].clear();
  81.             for(int i = 0; i < matrix.size(); ++i){
  82.                 if(!matrix[i].empty()){
  83.                     matrix[i][NtI[v]] = 0;
  84.                 }
  85.             }
  86.             ItN.erase(NtI[v]);
  87.             NtI.erase(v);
  88.  
  89.         }
  90.  
  91.         auto Del_E(string v, string w){
  92.             matrix[NtI[v]][NtI[w]] = 0;
  93.         }
  94.  
  95.         auto Edit_V(string v, string new_v){
  96.             NtI[new_v] = NtI[v];
  97.             NtI.erase(v);
  98.             ItN[NtI[new_v]] = new_v;
  99.         }
  100.  
  101.         auto Edit_E(string v, string w, int c){
  102.             matrix[NtI[v]][NtI[w]] = c;
  103.         }
  104.  
  105.         auto print(){
  106.             cout << endl;
  107.             for(int i = 0; i < matrix.size(); ++i){
  108.                 if(!matrix[i].empty()) {
  109.                     for (int j = 0; j < matrix[i].size(); ++j) {
  110.                         if(ItN.contains(j))
  111.                             cout << matrix[i][j] << ' ';
  112.                     }
  113.                     cout << endl;
  114.                 }
  115.  
  116.             }
  117.         }
  118.  
  119.         int count(){
  120.             return NtI.size();
  121.         }
  122.  
  123.         void dfs(lab2::Graph graph, string name){
  124.             visited[NtI[name]] = true;
  125.             cout << name << ' ';
  126.             int v = graph.First(name);
  127.             if(v == -1)
  128.                 return;
  129.             else{
  130.                 dfs(graph, ItN[v]);
  131.                 v = Next(name, v);
  132.                 while(v != -1){
  133.                     dfs(graph, ItN[v]);
  134.                     v = Next(name, v);
  135.                 }
  136.                 return;
  137.             }
  138.  
  139.         }
  140.  
  141.  
  142.     private:
  143.         vector<vector<int>> matrix;
  144.         map<string, int> NtI;
  145.         map<int, string> ItN;
  146.     };
  147. }
  148.  
  149. #ifndef ALGORITHMS_LAB2_H
  150. #define ALGORITHMS_LAB2_H
  151.  
  152. #endif //ALGORITHMS_LAB2_H
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement