Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- #include "boost/bimap.hpp"
- using namespace std;
- namespace lab2{
- class Graph{
- public:
- Graph(int size){
- for(int i = 0; i < size; ++i){
- matrix.push_back(vector<int>(size,0));
- ItN[i] = (char)(97 + i);
- NtI[char(97 + i)] = i;
- }
- }
- auto First(char v){
- if(!matrix[NtI[v]].empty()){
- if(v >= matrix.size())
- throw runtime_error("going beyond the boundaries of the matrix");
- else{
- for(int i = 0; i < matrix[NtI[v]].size(); ++i){
- if(matrix[NtI[v]][i] != 0)
- return ItN[i];
- }
- return ' ';
- }
- } else
- throw runtime_error("height does not exist");
- }
- auto Next(char v, char i){
- if(!matrix[NtI[v]].empty()){
- if(NtI[i] == matrix[NtI[v]].size() - 1){
- return ' ';
- }
- for(int x = NtI[i] + 1; i < matrix[NtI[v]].size(); ++i){
- if(matrix[NtI[v]][x] != 0)
- return ItN[x];
- }
- }
- else
- throw runtime_error("height does not exist");
- }
- auto Vertex(char v, int i){
- if(!matrix[NtI[v]].empty()){
- if(i == 0)
- return First(v);
- char c = First(v);
- while(i != 0){
- c = Next(v, c);
- --i;
- }
- return c;
- }
- else throw runtime_error("height does not exist");
- }
- auto Add_V(char v){
- NtI[v] = matrix.size();
- ItN[matrix.size()] = v;
- matrix.push_back(vector<int>(matrix[0].size(), 0));
- for(auto& x : matrix){
- if(!x.empty())
- x.push_back(0);
- }
- }
- auto Add_E(char v, char w, int c){
- matrix[NtI[v]][NtI[w]] = c;
- }
- auto Del_V(char v){
- matrix[NtI[v]].clear();
- for(int i = 0; i < matrix.size(); ++i){
- if(!matrix[i].empty()){
- matrix[i][NtI[v]] = 0;
- }
- }
- ItN.erase(NtI[v]);
- NtI.erase(v);
- }
- auto Del_E(char v, char w){
- matrix[NtI[v]][NtI[w]] = 0;
- }
- auto Edit_V(char v, char new_v){
- NtI[new_v] = NtI[v];
- NtI.erase(v);
- ItN[NtI[new_v]] = new_v;
- }
- auto Edit_E(char v, char w, int c){
- matrix[NtI[v]][NtI[w]] = c;
- }
- auto print(){
- cout << " ";
- for(const auto x : NtI){
- cout << x.first << ' ';
- }
- cout << endl;
- for(int i = 0; i < matrix.size(); ++i){
- if(!matrix[i].empty()) {
- cout << ItN[i] << ' ';
- for (int j = 0; j < matrix[i].size(); ++j) {
- if(ItN.contains(j))
- cout << matrix[i][j] << ' ';
- }
- cout << endl;
- }
- }
- }
- private:
- vector<vector<int>> matrix;
- map<char, int> NtI;
- map<int, char> ItN;
- };
- }
- #ifndef ALGORITHMS_LAB2_H
- #define ALGORITHMS_LAB2_H
- #endif //ALGORITHMS_LAB2_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement