Advertisement
Guest User

a

a guest
Apr 11th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include "Node.h"
  5. #include <istream>
  6. #include <ostream>
  7. #include <set>
  8.  
  9. using namespace std;
  10.  
  11. template<class T>
  12. class node
  13. {
  14. public:
  15.     ~node(){}
  16.     node(T num): number(num){}
  17.     std::set<T> adjacencyList;
  18.     T number;
  19. };
  20.  
  21.  
  22. template<class T>
  23. class Graph
  24. {
  25.  
  26. public:
  27.     ~Graph();
  28.     bool insertEdge(T from,T to);
  29.     void deleteVertex(T numberV);
  30.     Graph(T *vertexs,const int &count);
  31.     Graph(const Graph &copy);
  32.     Graph(){}
  33.     void printlinkVertex(T V);
  34.     void printGraph();
  35.     bool insertVertex(T a);
  36.     vector<node<T>*> getNodes() const;
  37.     vector<T> getVertexs() const;
  38.     void DFS(T v);
  39.  
  40.     Graph& operator = (const Graph &A)
  41.     {
  42.         if(this==&A)
  43.         {
  44.             return *this;
  45.         }
  46.         countVer=A.countVer;
  47.         vertexs=A.vertexs;
  48.         nodes=A.nodes;
  49.         visited=A.visited;
  50.         return *this;
  51.     }
  52.  
  53.     friend ostream& operator <<(ostream& s, const Graph<T>& A);
  54.     friend istream& operator >>(istream& s, Graph<T>& A);
  55. /*
  56.     void operator [] (const T& v)
  57.     {
  58.         vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),v);
  59.         if(v==vertexs[indexV-vertexs.begin()])
  60.         {
  61.             for(set<T>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
  62.             {
  63.                 cout<<(*it)<<' ';
  64.             }
  65.         }
  66.     }*/
  67.     void cc()
  68.     {
  69.         cout<<"GG";
  70.     }
  71. private:
  72.     bool edge(T from,T to);
  73.     void privatDFS(T v);
  74.     int countVer=0;
  75.     vector<bool> visited;
  76.     vector<node<T>*> nodes;
  77.     vector<T> vertexs;
  78. };
  79.  
  80. template<class T>
  81. Graph<T>::~Graph()
  82. {
  83.     for(int i=0;i<nodes.size();++i)
  84.     {
  85.         delete nodes[i];
  86.     }
  87.     nodes.clear();
  88. }
  89.  
  90.  
  91. template<class T>
  92. Graph<T>::Graph(T *vertexs,const int &count)
  93. {
  94.     for(int i=0;i<count;++i)
  95.     {
  96.         node<T>* tmpNode=new node<T>(vertexs[i]);
  97.         nodes.push_back(tmpNode);
  98.         this->vertexs.push_back(vertexs[i]);
  99.         visited.push_back(false);
  100.     }
  101.     countVer=count;
  102. }
  103.  
  104. template<class T>
  105. Graph<T>::Graph(const Graph &copy)
  106. {
  107.     vertexs=copy.getVertexs();
  108.     nodes=copy.getNodes();
  109. }
  110.  
  111. template<class T>
  112. vector<T> Graph<T>::getVertexs() const
  113. {
  114.     return vertexs;
  115. }
  116.  
  117. template<class T>
  118. vector<node<T>*> Graph<T>::getNodes() const
  119. {
  120.     return nodes;
  121. }
  122.  
  123. template<class T>
  124. istream& operator >> (istream& in,Graph<T>& A)
  125. {
  126.     while(true)
  127.     {
  128.         A.vertexs.clear();
  129.         A.visited.clear();
  130.         A.nodes.clear();
  131.         int countV;
  132.         cout<<"Enter count of vertex\n";
  133.         in>>countV;
  134.         if (in.fail())
  135.         {
  136.             cout<<"Введення невірне,спробуйте знову\n";
  137.             in.clear();
  138.             in.ignore();
  139.             continue;
  140.         }
  141.         A.countVer=countV;
  142.         cout<<"Enter vertex\n";
  143.         vector<T> vertex;
  144.         for(int i=0;i<countV;++i)
  145.         {
  146.             T a;
  147.             in>>a;
  148.             vertex.push_back(a);
  149.         }
  150.         if (in.fail())
  151.         {
  152.             cout<<"Введення невірне,спробуйте знову\n";
  153.             in.clear();
  154.             in.ignore(INT_MAX,'\n');
  155.             continue;
  156.         }
  157.         vector<node<T>*> nodes;
  158.         vector<bool> visited;
  159.         for(int i=0;i<countV;++i)
  160.         {
  161.             node<T>* tmpNode=new node<T>(vertex[i]);
  162.             nodes.push_back(tmpNode);
  163.             visited.push_back(false);
  164.         }
  165.         A.nodes=nodes;
  166.         A.visited=visited;
  167.         A.vertexs=vertex;
  168.         cout<<"Вводьте звязки між ввершинами, якщо захочете припинити ввдення введіть -1\n";
  169.         while(true)
  170.         {
  171.             T a,b;
  172.             in>>a;
  173.             if(a==-1 or a=='-' or a=="-1") break;
  174.             in >> b;
  175.             if(b==-1) break;
  176.             if(a>=0 && b>=0)
  177.             {
  178.                 if(A.insertEdge(a,b))
  179.                 {
  180.                     cout<<"Ребро додано\n";
  181.                 }else
  182.                 {
  183.                     cout<<"Не вірний запис вершин спробуте ще раз\n";
  184.                 }
  185.             }
  186.         }
  187.         if (in.fail())
  188.         {
  189.             cout<<"Введення  невірне,спробуйте знову\n";
  190.             in.clear();
  191.             in.ignore();
  192.             continue;
  193.         }
  194.         return in;
  195.     }
  196. }
  197. template<class T>
  198. ostream& operator << (ostream& s,const Graph<T>& A)
  199. {
  200.     s<<"Кількість вершин= "<<A.countVer<<'\n';
  201.     s<<"Список суміжності кожної вершини\n";
  202.     for(auto V=A.vertexs.begin();V!=A.vertexs.end();++V)
  203.     {
  204.         auto indexV=find(A.vertexs.begin(),A.vertexs.end(),*V);
  205.         if(*V==A.vertexs[indexV-A.vertexs.begin()])
  206.         {
  207.             s<<*V<<':';
  208.             for(auto it=A.nodes[indexV-A.vertexs.begin()]->adjacencyList.begin();it!=A.nodes[indexV-A.vertexs.begin()]->adjacencyList.end();++it)
  209.             {
  210.                 s<<(*it)<<' ';
  211.             }
  212.         }
  213.         cout<<"\n";
  214.     }
  215.     return s;
  216. }
  217. template<class T>
  218. void Graph<T>::DFS(T v)
  219. {
  220.     for(int i=0;i<countVer;++i)
  221.     {
  222.         visited[i]= false;
  223.     }
  224.     privatDFS(v);
  225. }
  226.  
  227.  
  228. template<class T>
  229. void Graph<T>::privatDFS(T v)
  230. {
  231.     typename vector<T>::iterator indexV;
  232.     indexV = find(vertexs.begin(), vertexs.end(), v);
  233.     visited[indexV-vertexs.begin()] = true;
  234.  
  235.     for(auto it=vertexs.begin();it!=vertexs.end();++it)
  236.     {
  237.         if(!visited[it-vertexs.begin()] && edge(v,*it))
  238.         {
  239.             privatDFS(*it);
  240.         }
  241.     }
  242.     cout<<v<<' ';
  243. }
  244.  
  245. template<class T>
  246. bool Graph<T>::edge(T from, T to)
  247. {
  248.     typename vector<T>::iterator indexFrom;
  249.     indexFrom=find(vertexs.begin(),vertexs.end(),from);
  250.     for(auto itSet:nodes[indexFrom-vertexs.begin()]->adjacencyList)
  251.     {
  252.         if(to!=itSet)
  253.         {
  254.             return false;
  255.         }
  256.     }
  257.     return true;
  258. }
  259. template<class T>
  260. bool Graph<T>::insertEdge(T from,T to)
  261. {
  262.     typename vector<T>::iterator indexFrom,indexTo;
  263.     indexFrom=find(vertexs.begin(),vertexs.end(),from);
  264.     indexTo=find(vertexs.begin(),vertexs.end(),to);
  265.     if(from!=*indexFrom || to!=*indexTo)
  266.     {
  267.         return false;
  268.     }
  269.     nodes[indexFrom-vertexs.begin()]->adjacencyList.insert(to);
  270.     nodes[indexTo-vertexs.begin()]->adjacencyList.insert(from);
  271.     return true;
  272. }
  273. template<class T>
  274. bool Graph<T>::insertVertex(T V)
  275. {
  276.     for(int i=0;i<vertexs.size();++i)
  277.     {
  278.         if(vertexs[i]==V)
  279.         {
  280.             return false;
  281.         }
  282.     }
  283.     node<T>* tmp=new node<T>(V);
  284.     nodes.push_back(tmp);
  285.     return true;
  286. }
  287. template<class T>
  288. void Graph<T>::printlinkVertex(T V)
  289. {
  290.     typename vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),V);
  291.     if(V==vertexs[indexV-vertexs.begin()])
  292.     {
  293.         for(set<int>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
  294.         {
  295.             cout<<(*it)<<' ';
  296.         }
  297.     }
  298.  
  299. }
  300. template<class T>
  301. void Graph<T>::printGraph()
  302. {
  303.     for(typename vector<T>::iterator V=vertexs.begin();V!=vertexs.end();++V)
  304.     {
  305.         typename vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),*V);
  306.         if(*V==vertexs[indexV-vertexs.begin()])
  307.         {
  308.             cout<<*V<<':';
  309.             for(typename set<T>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
  310.             {
  311.                 cout<<(*it)<<' ';
  312.             }
  313.         }
  314.         cout<<"\n";
  315.     }
  316. }
  317.  
  318.  
  319.  
  320. int main()
  321. {
  322.     Graph<int> G;
  323.     cin>>G;
  324.  
  325.  
  326.     cout<<G;
  327.     return 0;
  328. }
  329.  
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement