Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include "Node.h"
- #include <istream>
- #include <ostream>
- #include <set>
- using namespace std;
- template<class T>
- class node
- {
- public:
- ~node(){}
- node(T num): number(num){}
- std::set<T> adjacencyList;
- T number;
- };
- template<class T>
- class Graph
- {
- public:
- ~Graph();
- bool insertEdge(T from,T to);
- void deleteVertex(T numberV);
- Graph(T *vertexs,const int &count);
- Graph(const Graph ©);
- Graph(){}
- void printlinkVertex(T V);
- void printGraph();
- bool insertVertex(T a);
- vector<node<T>*> getNodes() const;
- vector<T> getVertexs() const;
- void DFS(T v);
- Graph& operator = (const Graph &A)
- {
- if(this==&A)
- {
- return *this;
- }
- countVer=A.countVer;
- vertexs=A.vertexs;
- nodes=A.nodes;
- visited=A.visited;
- return *this;
- }
- friend ostream& operator <<(ostream& s, const Graph<T>& A);
- friend istream& operator >>(istream& s, Graph<T>& A);
- /*
- void operator [] (const T& v)
- {
- vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),v);
- if(v==vertexs[indexV-vertexs.begin()])
- {
- for(set<T>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
- {
- cout<<(*it)<<' ';
- }
- }
- }*/
- void cc()
- {
- cout<<"GG";
- }
- private:
- bool edge(T from,T to);
- void privatDFS(T v);
- int countVer=0;
- vector<bool> visited;
- vector<node<T>*> nodes;
- vector<T> vertexs;
- };
- template<class T>
- Graph<T>::~Graph()
- {
- for(int i=0;i<nodes.size();++i)
- {
- delete nodes[i];
- }
- nodes.clear();
- }
- template<class T>
- Graph<T>::Graph(T *vertexs,const int &count)
- {
- for(int i=0;i<count;++i)
- {
- node<T>* tmpNode=new node<T>(vertexs[i]);
- nodes.push_back(tmpNode);
- this->vertexs.push_back(vertexs[i]);
- visited.push_back(false);
- }
- countVer=count;
- }
- template<class T>
- Graph<T>::Graph(const Graph ©)
- {
- vertexs=copy.getVertexs();
- nodes=copy.getNodes();
- }
- template<class T>
- vector<T> Graph<T>::getVertexs() const
- {
- return vertexs;
- }
- template<class T>
- vector<node<T>*> Graph<T>::getNodes() const
- {
- return nodes;
- }
- template<class T>
- istream& operator >> (istream& in,Graph<T>& A)
- {
- while(true)
- {
- A.vertexs.clear();
- A.visited.clear();
- A.nodes.clear();
- int countV;
- cout<<"Enter count of vertex\n";
- in>>countV;
- if (in.fail())
- {
- cout<<"Введення невірне,спробуйте знову\n";
- in.clear();
- in.ignore();
- continue;
- }
- A.countVer=countV;
- cout<<"Enter vertex\n";
- vector<T> vertex;
- for(int i=0;i<countV;++i)
- {
- T a;
- in>>a;
- vertex.push_back(a);
- }
- if (in.fail())
- {
- cout<<"Введення невірне,спробуйте знову\n";
- in.clear();
- in.ignore(INT_MAX,'\n');
- continue;
- }
- vector<node<T>*> nodes;
- vector<bool> visited;
- for(int i=0;i<countV;++i)
- {
- node<T>* tmpNode=new node<T>(vertex[i]);
- nodes.push_back(tmpNode);
- visited.push_back(false);
- }
- A.nodes=nodes;
- A.visited=visited;
- A.vertexs=vertex;
- cout<<"Вводьте звязки між ввершинами, якщо захочете припинити ввдення введіть -1\n";
- while(true)
- {
- T a,b;
- in>>a;
- if(a==-1 or a=='-' or a=="-1") break;
- in >> b;
- if(b==-1) break;
- if(a>=0 && b>=0)
- {
- if(A.insertEdge(a,b))
- {
- cout<<"Ребро додано\n";
- }else
- {
- cout<<"Не вірний запис вершин спробуте ще раз\n";
- }
- }
- }
- if (in.fail())
- {
- cout<<"Введення невірне,спробуйте знову\n";
- in.clear();
- in.ignore();
- continue;
- }
- return in;
- }
- }
- template<class T>
- ostream& operator << (ostream& s,const Graph<T>& A)
- {
- s<<"Кількість вершин= "<<A.countVer<<'\n';
- s<<"Список суміжності кожної вершини\n";
- for(auto V=A.vertexs.begin();V!=A.vertexs.end();++V)
- {
- auto indexV=find(A.vertexs.begin(),A.vertexs.end(),*V);
- if(*V==A.vertexs[indexV-A.vertexs.begin()])
- {
- s<<*V<<':';
- for(auto it=A.nodes[indexV-A.vertexs.begin()]->adjacencyList.begin();it!=A.nodes[indexV-A.vertexs.begin()]->adjacencyList.end();++it)
- {
- s<<(*it)<<' ';
- }
- }
- cout<<"\n";
- }
- return s;
- }
- template<class T>
- void Graph<T>::DFS(T v)
- {
- for(int i=0;i<countVer;++i)
- {
- visited[i]= false;
- }
- privatDFS(v);
- }
- template<class T>
- void Graph<T>::privatDFS(T v)
- {
- typename vector<T>::iterator indexV;
- indexV = find(vertexs.begin(), vertexs.end(), v);
- visited[indexV-vertexs.begin()] = true;
- for(auto it=vertexs.begin();it!=vertexs.end();++it)
- {
- if(!visited[it-vertexs.begin()] && edge(v,*it))
- {
- privatDFS(*it);
- }
- }
- cout<<v<<' ';
- }
- template<class T>
- bool Graph<T>::edge(T from, T to)
- {
- typename vector<T>::iterator indexFrom;
- indexFrom=find(vertexs.begin(),vertexs.end(),from);
- for(auto itSet:nodes[indexFrom-vertexs.begin()]->adjacencyList)
- {
- if(to!=itSet)
- {
- return false;
- }
- }
- return true;
- }
- template<class T>
- bool Graph<T>::insertEdge(T from,T to)
- {
- typename vector<T>::iterator indexFrom,indexTo;
- indexFrom=find(vertexs.begin(),vertexs.end(),from);
- indexTo=find(vertexs.begin(),vertexs.end(),to);
- if(from!=*indexFrom || to!=*indexTo)
- {
- return false;
- }
- nodes[indexFrom-vertexs.begin()]->adjacencyList.insert(to);
- nodes[indexTo-vertexs.begin()]->adjacencyList.insert(from);
- return true;
- }
- template<class T>
- bool Graph<T>::insertVertex(T V)
- {
- for(int i=0;i<vertexs.size();++i)
- {
- if(vertexs[i]==V)
- {
- return false;
- }
- }
- node<T>* tmp=new node<T>(V);
- nodes.push_back(tmp);
- return true;
- }
- template<class T>
- void Graph<T>::printlinkVertex(T V)
- {
- typename vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),V);
- if(V==vertexs[indexV-vertexs.begin()])
- {
- for(set<int>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
- {
- cout<<(*it)<<' ';
- }
- }
- }
- template<class T>
- void Graph<T>::printGraph()
- {
- for(typename vector<T>::iterator V=vertexs.begin();V!=vertexs.end();++V)
- {
- typename vector<T>::iterator indexV=find(vertexs.begin(),vertexs.end(),*V);
- if(*V==vertexs[indexV-vertexs.begin()])
- {
- cout<<*V<<':';
- for(typename set<T>::iterator it=nodes[indexV-vertexs.begin()]->adjacencyList.begin();it!=nodes[indexV-vertexs.begin()]->adjacencyList.end();++it)
- {
- cout<<(*it)<<' ';
- }
- }
- cout<<"\n";
- }
- }
- int main()
- {
- Graph<int> G;
- cin>>G;
- cout<<G;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement